简体   繁体   中英

Cancelling multiple notification by sending unique id to broadcast receiver

I create multiple notifications using AlarmManager . Each notification pop-ups correctly and I would like to cancel each of these notification separately. I have an AlarmReceiver that creates the notification and sets the unique id for it. I'm sending the unique id in the intent (via putExtra ). This way, I can retrieve it in the NotificationReceiver and cancel the notification. However, it is not working.

Do you know why I always get the default value when I use intent.getIntExtra("UNIQUE_ID_NAME",0) ? Is it a proper way to achieve the goal of cancelling these notifications ? Here is my code:

MainActivity class:

@Override
public void onClick(View v) {
    Intent intent = new Intent(MainActivity.this, ReminderBroadcast.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    long ctime = System.currentTimeMillis();
    long tensec = 1000 * 5;

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, ctime+tensec, 1000*10 , pendingIntent);
}

AlarmReceiver Class:

@Override
public void onReceive(Context context, Intent intent) {
    int notification_id = generateRandom();

    Intent activityIntent = new Intent(context, MainActivity.class);
    activityIntent.putExtra(NotificationReceiver.UNIQUE_ID_NAME, notification_id);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Intent bYesIntent = new Intent(context, NotifReceiver.class);
    bYesIntent.putExtra(NotificationReceiver.YES_ACTION_ID, "yesID");
    PendingIntent actionYesIntent = PendingIntent.getBroadcast(context, 0, bYesIntent, 0);

    Intent bNoIntent = new Intent(context, NotifReceiver.class);
    bNoIntent.putExtra(NotificationReceiver.NO_ACTION_ID, "noID");
    PendingIntent actionNoIntent = PendingIntent.getBroadcast(context, 0, bNoIntent, 0);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "channel1")
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setContentTitle("Notif")
            .setContentText("Text")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setContentIntent(contentIntent)
            .addAction(R.mipmap.ic_launcher, "YES", actionYesIntent)
            .addAction(R.mipmap.ic_launcher, "NO", actionNoIntent)
            .setAutoCancel(true);

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
    notificationManager.notify(notification_id, builder.build());
}

public int generateRandom(){
    Random random = new Random();
    return random.nextInt(9999 - 1000) + 1000;
}

NotificationReceiver Class:

public class NotificationReceiver extends BroadcastReceiver {
    public static String YES_ACTION_ID = "YesID";
    public static String NO_ACTION_ID = "NoID";
    public static String UNIQUE_ID_NAME = "notification_id";

    @Override
    public void onReceive(Context context, Intent intent) {
        int notification_id= intent.getIntExtra(UNIQUE_ID_NAME,0);

        System.out.println("Notif ID: " + notification_id); // here always is 0;

            String actionYes = intent.getStringExtra(YES_ACTION_ID);
            String actionNo = intent.getStringExtra(NO_ACTION_ID);

            if(actionYes!=null){
                Toast.makeText(context, "YES", Toast.LENGTH_SHORT).show();
            }

            if(actionNo!=null){
                Toast.makeText(context, "NO", Toast.LENGTH_SHORT).show();
            }

        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.cancel(notification_id);
    }
}

I solved this problem by updating current intent and request code. Notifications are reusing the same Intent so adding FLAG_UPDATE_CURRENT should be used to be able to cancel all notification separately.

For contentIntent:

    PendingIntent contentIntent = PendingIntent.getActivity(context, notification_id, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);

For actionYesIntent

    PendingIntent actionYesIntent = PendingIntent.getBroadcast(context, notification_id, bYesIntent, PendingIntent.FLAG_UPDATE_CURRENT);

For actionNoIntent:

    PendingIntent actionNoIntent = PendingIntent.getBroadcast(context, notification_id, bNoIntent, PendingIntent.FLAG_UPDATE_CURRENT);

You are not adding notification_id for cases when Yes or No button is pressed,

    Intent bYesIntent = new Intent(context, NotifReceiver.class);
    bYesIntent.putExtra(NotificationReceiver.UNIQUE_ID_NAME, notification_id);
    bYesIntent.putExtra(NotificationReceiver.YES_ACTION_ID, "yesID");
    PendingIntent actionYesIntent = PendingIntent.getBroadcast(context, 0, bYesIntent, 0);

    Intent bNoIntent = new Intent(context, NotifReceiver.class);
    bNoIntent.putExtra(NotificationReceiver.UNIQUE_ID_NAME, notification_id);
    bNoIntent.putExtra(NotificationReceiver.NO_ACTION_ID, "noID");
    PendingIntent actionNoIntent = PendingIntent.getBroadcast(context, 0, bNoIntent, 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM