关闭当前点击操作的通知

Dismiss current notification on Action clicked

我有一个带有操作按钮的自定义通知:

public class NotificationReceiver extends com.parse.ParsePushBroadcastReceiver {
    @Override
    public void onPushReceive(Context context, Intent intent) {

    ...

    NotificationActivity notification = new NotificationActivity();
    notification.createNotification(context, message, notificationId);

    Log.i("tag", "Notification Received!");
}

public class NotificationActivity {

    public int notificationId;

    public void createNotification(Context context, String message, String studentId, String notificationId){
         this.notificationId = Integer.parseInt(notificationId);

         // manager
         NotificationManager notificationManager = 
         (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

         // notification
         Notification.Builder mBuilder = new Notification.Builder(context);
         mBuilder.setContentTitle("My Title");
         mBuilder.setContentText(message);
         mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
         mBuilder.setAutoCancel(true);
         mBuilder.setStyle(new Notification.BigTextStyle()
             .bigText(message));

         // cancel intent
         Intent cancelIntent = new Intent(context, CancelNotification.class);
         Bundle extras = new Bundle();
         extras.putInt("notification_id", this.notificationId);
         cancelIntent.putExtras(extras);
         PendingIntent pendingCancelIntent = 
             PendingIntent.getBroadcast(context, 0, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT) ;
         mBuilder.addAction(R.drawable.notification_close, "Fechar", pendingCancelIntent);

        // notify
        Notification notification = mBuilder.build();
        notificationManager.notify(Integer.parseInt(notificationId), notification);
    }

    public static class CancelNotification extends BroadcastReceiver {

        private int id;

        @Override
        public void onReceive(Context context, Intent intent) {
             id = intent.getIntExtra("notification_id", 1);
             NotificationManager notificationManager =
                  (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
             notificationManager.cancel(id);
        }
    }
}

我想取消点击操作按钮的通知"Close"。

我知道我需要通知的 ID 才能取消它,但是我执行代码的方式是,当我单击 "Close" 按钮并创建扩展 BroadCastReceiver 的 class CancelNotification 我我正在获取最后一个通知的通知 ID,因此即使我单击我创建的第一个通知也会关闭最后一个通知。

我可能做错了什么?

使用通知生成器总是更好。 这是一个例子:

    NotificationCompat.Builder mBuilder=new NotificationCompat.Builder(this);
    mBuilder.setContentTitle("Your title");
    mBuilder.setOnlyAlertOnce(true);
    mBuilder.setAutoCancel(true);
    mBuilder.setContentText("main content")
    mBuilder.setSubText("subtext")

接下来您将创建一个意图,activity 您希望在点击通知时打开该意图

    intent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);

然后创建您的通知管理器

    notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

    Notification notification = mBuilder.build();
    notification.flags = Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(notificationID, notification);

notificationID 可以是任何整数值。使用此类型可让您始终遵循 android 通知规范。

找到了

你 pendingIntent 总是发送请求代码 == 0;

由于您有多个通知,因此每个通知都应使用不同的 requestCode。

所以,尝试改变:

发件人:

PendingIntent pendingCancelIntent = 
         PendingIntent.getBroadcast(context, 0, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT) ;

收件人:

PendingIntent pendingCancelIntent = 
         PendingIntent.getBroadcast(context, this.notificationId, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT) ;

我在这里测试了你的代码,在我做的更改后它可以正常工作。

在您的意图被触发后,您将需要运行以下代码来删除通知。

NotificationManagerCompat.from(this).cancel(null, notificationId);