直接回复按钮不发送广播意图

时间:2018-07-30 19:38:56

标签: android broadcastreceiver android-notifications

我正在按照https://developer.android.com/training/notify-user/build-notification#reply-action上的教程创建带有回复按钮的通知,但是当我输入消息并没有点击任何回复按钮时,创建一个可以接收来自通知意图的BroadcastReceiver时遇到了问题如果发生这种情况,则不会执行onReceive方法。

这是我的代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = "testchannel";
        String description = "testdesc";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL, name, importance);
        channel.setDescription(description);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }

    Notification.Builder builder;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        builder = new Notification.Builder(this, NOTIFICATION_CHANNEL);
    } else
        builder = new Notification.Builder(this);


    String actionString = "com.test.demo.notifications.NOTIFY";

    // intent to be launcher on reply button click
    Intent noop = new Intent(actionString,null,getApplicationContext(),MainActivity.class);

    // Build a PendingIntent for the reply action to trigger.
    PendingIntent replyPendingIntent =
            PendingIntent.getBroadcast(getApplicationContext(),
                    123,
                    noop,
                    PendingIntent.FLAG_UPDATE_CURRENT);

    RemoteInput remoteInput = new RemoteInput.Builder(KEY_TEXT_REPLY)
            .setLabel("RemoteInput")
            .build();

    Notification.Action actionReply =
            new Notification.Action.Builder(android.R.drawable.sym_action_chat, "Reply", replyPendingIntent)
                    .addRemoteInput(remoteInput)
                    .build();

    Notification.Action noReply =
            new Notification.Action.Builder(android.R.drawable.sym_action_chat, "No Reply", replyPendingIntent)
                    .addRemoteInput(remoteInput)
                    .build();

    Notification notification = builder
            .setContentTitle("Notification Test")
            .setContentText("Hello World!")
            .setSmallIcon(R.mipmap.ic_launcher_round)
            .addAction(actionReply)
            .build();


    getApplicationContext().registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // this never executes when the reply button is pressed.

            CharSequence messageText = getMessageText(intent);
            if (messageText!= null) {
                Toast.makeText(context, messageText, Toast.LENGTH_SHORT).show();
            }
            Toast.makeText(context, "Broadcast received", Toast.LENGTH_SHORT).show();
        }
    }, new IntentFilter(actionString));



    final NotificationManager systemService = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    assert systemService != null;
    systemService.notify(1, notification);

    // test broadcast intent , this broadcast is received successfully
    getApplicationContext().sendBroadcast(new Intent(actionString));
}


private CharSequence getMessageText(Intent intent) {
    Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
    if (remoteInput != null) {
        return remoteInput.getCharSequence(KEY_TEXT_REPLY);
    }
    return null;
}

0 个答案:

没有答案