获取被解雇的通知ID

时间:2018-10-08 18:23:42

标签: java android notifications android-notifications android-database

我的应用程序在具有该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();

1 个答案:

答案 0 :(得分:1)

您需要设置要用于在传递给挂起的意图的意图中显示通知的通知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);
    }
};

然后在您的广播接收器中获取ID

{{1}}