美文网首页通知,角标
Android7.0 通知直接回复

Android7.0 通知直接回复

作者: 灿烂的黑土 | 来源:发表于2017-01-11 16:24 被阅读0次

    简介

    最近在看7.0的一些新特性,记录下小知识方便之后使用。今天要说的就是状态栏通知可直接回复的功能。(一定记得是7.0的新特性,在低版本上是不支持的)先来看效果图。

    notifition.gif

    步骤

    1. 创建一个RemoteInput
    2. 创建一个PendingIntent, 这个PendingIntent指当我们点击”发送”的时候调用什么
    3. 创建一个直接回复的Action
    4. 创建notification
    5. 发送通知
    6. 拿到回复的内容
    7. 处理

    实现

    1. 创建一个RemoteInput
    RemoteInput remoteInput = new RemoteInput.Builder(RESULT_KEY).setLabel("回复通知").build();
    其中RESULT_KEY是获取回复的内容,setLabel设置的值就是EditText的hint值
    
    1. 创建一个PendingIntent。(这个就不过多介绍了)
    Intent intent = new Intent(this, SendService.class);
    PendingIntent pendingIntent = PendingIntent.getService(this,1,intent,PendingIntent.FLAG_CANCEL_CURRENT);
    
    1. 创建可回复的Action
    NotificationCompat.Action action = 
    new NotificationCompat.Action.Builder(R.mipmap.ic_launcher,"回复",pendingIntent).addRemoteInput(remoteInput).build();
    其中这个Builder需要传递四个参数,第一个就是logo图片,第二个类似于标签我们要点击的。第三个就是要做的动作intent.最后把我们创建的remoteInput加入进来。
    
    1. 创建notification
     NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("请问需要银行贷款吗?")
                    .setContentText("您好,我是XX银行的XX经理, 请问你需要办理银行贷款吗?")
                    .setColor(Color.CYAN)
                    .setPriority(Notification.PRIORITY_MAX) // 设置优先级为Max,则为悬浮通知
                    .addAction(action) // 设置回复action
                    .setAutoCancel(true)
                    .setWhen(System.currentTimeMillis())
                    .setDefaults(Notification.DEFAULT_ALL) // 想要悬浮出来, 这里必须要设置
                    .setCategory(Notification.CATEGORY_MESSAGE);
    
    1. 发送通知
     NotificationManager nm = getSystemService(NotificationManager.class);
     Notification notification = buildNotification();
     nm.notify(NOTIFICATION_ID,notification);
    
    1. 拿到回复的内容
       Bundle resultsFromIntent = RemoteInput.getResultsFromIntent(intent);
        //根据key拿回复的内容
        if (null!=resultsFromIntent){
            String resultString = resultsFromIntent.getString(MainActivity.RESULT_KEY);
            //处理回复内容
            reply(resultString);
        }
    
    1. 处理
     private void reply(final String resultString) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    SystemClock.sleep(1000);
                    Logger.e(SendService.class.getSimpleName(),resultString);
                    onReply();
                }
            }).start();
        }
    
        @TargetApi(Build.VERSION_CODES.M)
        private void onReply() {
            final NotificationManager nm = getSystemService(NotificationManager.class);
            final Handler handler = new Handler(getMainLooper());
            handler.post(new Runnable() {
                @Override
                public void run() {
                    // 更新通知为“回复成功”
                    Notification notification = new NotificationCompat.Builder(SendService.this)
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setContentText("回复成功")
                            .build();
                    nm.notify(MainActivity.NOTIFICATION_ID, notification);
                }
            });
    
            // 最后将通知取消
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    nm.cancel(MainActivity.NOTIFICATION_ID);
                }
            }, 2000);
        }
    

    以上基本就是整个通知回复的内容了。代码很简单,就是基本的调用。

    最后奉上我的代码:

    https://github.com/sunshine-sz/SunshineDemo

    相关文章

      网友评论

        本文标题:Android7.0 通知直接回复

        本文链接:https://www.haomeiwen.com/subject/ykdrbttx.html