获取被驳回的通知 ID

Getting dismissed notification id

我的应用程序使用该 ID 在数据库中创建一个通知和一个项目。 当用户取消通知时,我想获取该通知 ID,并使用它在数据库中进行研究。我已经有一个待处理的 Intent,可以让我知道通知何时被取消。 广播接收者:

 final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

           //Action to perform when notification is dismissed

            unregisterReceiver(this);
        }
    };

创建通知的代码:

 Intent intent = new Intent("NOTIFICATION_DELETED");
            PendingIntent pendintIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
            registerReceiver(receiver, new IntentFilter("NOTIFICATION_DELETED"));


            notification = new Notification.Builder(this, channelId)
                    .setContentTitle(title)
                    .setContentText(text)
                    .setSmallIcon(R.drawable.icon_notification)
                    .setAutoCancel(true)
                    .setDeleteIntent(pendintIntent)
                    .build();

    notificationManager.notify(idUnico =(int)((new

        Date().

        getTime() /1000L)%Integer.MAX_VALUE),notification );

    reminder.setText("");
    titoloE.setText("");

        saveNotif();

        checkForConsent();

如果您要在传递给未决意图的意图中显示通知,则需要设置通知。

int notifiId = (int)((new Date().getTime() /1000L)%Integer.MAX_VALUE);

Intent intent = new Intent("NOTIFICATION_DELETED");
// set id to intent
intent.putExtra("NOTIFICATION_ID", notifiId);

PendingIntent pendintIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
registerReceiver(receiver, new IntentFilter("NOTIFICATION_DELETED"));

notification = new Notification.Builder(this, channelId)
                .setContentTitle(title)
                .setContentText(text)
                .setSmallIcon(R.drawable.icon_notification)
                .setAutoCancel(true)
                .setDeleteIntent(pendintIntent)
                .build();

notificationManager.notify(notifiId, notification);

然后在你的广播接收器中你可以得到这个Id

 final BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

       //Action to perform when notification is dismissed
       if (intent.hasExtra("NOTIFICATION_ID") {
           int id = intent.getIntExtra("NOTIFICATION_ID", -1);
           // Use Id
       }

        unregisterReceiver(this);
    }
};