从BroadcastReceiver获取通知标题(直接回复Android N)

时间:2017-03-25 14:58:56

标签: java android android-studio notifications android-notifications

由于我正在构建一个消息传递应用程序,并且可能有多个通知直接回复,我需要能够识别哪个人正在发送文本以便将消息发送给合适的人。我怎么能做到这一点?

我的想法是使用extras获取通知的标题,但我似乎无法在BroadcastReceiver中获取额外内容(在Reply Reciever Activity中)。附加功能仅在单击通知并被解除时才起作用(因此我可以通过额外的方式获得意图)。

以下是通知的代码:

resultIntent= new Intent(NotificationService.this, StartNAFromNS.class);
                        resultIntent.putExtra(Intent.EXTRA_TITLE, underestood_name.replace("__", " "));


                        int Unique_Code = Retrieve_int("Unique_Code", 0, this);
                        Unique_Code=Unique_Code+1;
                        Save_int("Unique_Code", Unique_Code, this);


                        TaskStackBuilder stackBuilder = TaskStackBuilder.create(NotificationService.this);
                        stackBuilder.addParentStack(SearchActivity.class);
                        stackBuilder.addNextIntent(resultIntent);
                        PendingIntent resultPendingIntent =stackBuilder.getPendingIntent(Unique_Code,PendingIntent.FLAG_UPDATE_CURRENT);
                        PendingIntent replyPendingIntent = null;
                        if (Build.VERSION.SDK_INT >= 24) {
                            replyPendingIntent = PendingIntent.getBroadcast(
                                    NotificationService.this,
                                    0,
                                    new Intent(NotificationService.this, ReplyReceiver.class),
                                    PendingIntent.FLAG_UPDATE_CURRENT
                            );
                        }
                        RemoteInput remoteInput = new RemoteInput.Builder(KEY_NOTIFICATION_REPLY)
                                .setLabel("Tapez votre réponse ici")
                                .build();
                        android.support.v4.app.NotificationCompat.Action replyAction = new android.support.v4.app.NotificationCompat.Action.Builder(
                                0, "Répondre", replyPendingIntent)
                                .addRemoteInput(remoteInput)
                                .build();
                        miBuilder = new android.support.v4.app.NotificationCompat.Builder(NotificationService.this)
                                .setGroup("Groupe01")
                                .setShowWhen(true)
                                .setSubText(summary)
                                .setSmallIcon(R.drawable.ic_chat_bubble_outline_black_24dp)
                                .setContentTitle(mess_sender)
                                .setContentText(mess_contenu)
                                .setAutoCancel(true)
                                .setContentIntent(resultPendingIntent)
                                .setColor(getResources().getColor(R.color.colorPrimaryDark))
                                .setDefaults(Notification.DEFAULT_ALL)
                                .setPriority(Notification.PRIORITY_HIGH);

                        if (Build.VERSION.SDK_INT >= 24) {
                            miBuilder.addAction(replyAction);
                        }
                        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                        notificationManager.notify(notif_id, miBuilder.build());
                        Log.wtf("Notification", "Displayed");

1 个答案:

答案 0 :(得分:0)

要从通知界面接收用户输入您在回复操作意图中声明的活动:

  1. 通过将通知操作的intent作为输入参数传递来调用getResultsFromIntent()。此方法返回包含文本响应的Bundle。

    Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
    
  2. 使用结果键(提供给RemoteInput.Builder构造函数)查询包。您可以通过创建方法来完成此过程并检索输入文本,如以下代码段所示:

    // Obtain the intent that started this activity by calling
    // Activity.getIntent() and pass it into this method to
    // get the associated string.
    
    private CharSequence getMessageText(Intent intent) {
        Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
        if (remoteInput != null) {
            return remoteInput.getCharSequence(KEY_NOTIFICATION_REPLY);
        }
        return null;
    }
    
  3. 使用您为上一个通知提供的相同通知ID构建并发出另一个通知。进度指示器从通知界面中消失,以通知用户成功回复。使用此新通知时,请使用传递给接收方onReceive()方法的上下文。

    // Build a new notification, which informs the user that the system
    // handled their interaction with the previous notification.
    Notification repliedNotification =
            new Notification.Builder(context)
                .setSmallIcon(R.drawable.ic_message)
                .setContentText(getString(R.string.replied))
                .build();
    
    // Issue the new notification.
    NotificationManager notificationManager =
        NotificationManager.from(context);
    notificationManager.notify(notificationId, repliedNotification);
    
  4. 有关详细信息,请查看:https://developer.android.com/guide/topics/ui/notifiers/notifications.html (从内联回复中检索用户输入)