How can I cancel unshown notifications in android

2019-08-13 02:32发布

问题:

Hi I am new to notifications,

I wrote this code for notifications.

PendingIntent pendingIntent = PendingIntent.getActivity(context, leadidforstatus,
    myIntent, Intent.FLAG_ACTIVITY_NEW_TASK);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
    .setSmallIcon(R.drawable.noti)
    .setContentTitle("LMS notification")
    .setContentIntent(pendingIntent)
    .setContentText(intent.getStringExtra("messagenotify"))
    .setAutoCancel(true);

i = (int) System.currentTimeMillis();
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(i, mBuilder.build());

In that I set one list of notifications. In that some displayed and some not displayed. But I want to cancel all un shown notifications.

I used mNotificationManager.cancleAll() but that is cancel only shown notifications. thanks in advance.

回答1:

I think the problem here is that The Notification is launched immediately after creating it and your not using the AlarmManager to schedule this Notification.

Solution Should be to schedule Alarms via an AlarmManager which will trigger the Notification Code above.

This requires 3 parts:

Register a BroadCastReceiver & Build a Notification when its triggered:

public class UtilityReceiver  extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
   // .. Here is where you really want to build the notification and notify
   // This will be triggered by the AlarmManager from the Code below
   PendingIntent pendingIntent = PendingIntent.getActivity(context, leadidforstatus, myIntent, Intent.FLAG_ACTIVITY_NEW_TASK);

 NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
               .setSmallIcon(R.drawable.noti)
               .setContentTitle("LMS notification")
               .setContentIntent(pendingIntent)
               .setContentText(intent.getStringExtra("messagenotify"))
               .setAutoCancel(true);

     i = (int) System.currentTimeMillis();
     NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
     mNotificationManager.notify(i, mBuilder.build());

}

Schedule an Alarm with the AlarmManager to send a BroadCast which the Receiver above is listening for:

        // Create an Intent to Launch
        Intent notificationIntent = new Intent(context, UtilityReceiver.class);

        // Use a Pending Intent to Launch the Alarm
        PendingIntent notificationPendingIntent = PendingIntent.getBroadcast(context,
                            PendingIntent.FLAG_ONE_SHOT, notificationIntent, Intent.FILL_IN_DATA);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, timeForAlarmInMillis, notificationPendingIntent);

Then when you encounter the situation when you no longer want notifications to show for your app, you tell the AlarmManager to remove them from its queue of Alarms by Rebuilding the pending Intent and using AlarmManager to Cancel it.

       // Create an Intent to Launch
        Intent notificationIntent = new Intent(context, UtilityReceiver.class);

        // Use a Pending Intent to Launch the Alarm
        PendingIntent notificationPendingIntent = PendingIntent.getBroadcast(context,
                            PendingIntent.FLAG_ONE_SHOT, notificationIntent, Intent.FILL_IN_DATA);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        // *********Important this will cancel all the Matching PendingIntents
        alarmManager.cancel(notificationPendingIntent);

Good Luck, and be sure to register your Receiver in your Activity or Manifest.



回答2:

NotificationManager maintains list of all notification mNotificationList and performs cancel or cancelAll operations on this list

I tried searching in Google source code of NotificationManager.java and NotificationManagerService.java in notify(),enqueueNotificationWithTag() and cancelAllNotification() But there is no implementation to cancel previously unshown notification

As per your comment, if your user is deleted and his notification arrives then user is already deleted so notification has no meaning

i = (int) System.currentTimeMillis();
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(i, mBuilder.build());
// i should be Id for this notification, i think you want to pass 
// time in mili second here


回答3:

I think you are searching similar to this,

How to dismiss notification after action has been clicked

Just check the answer on that post. Use the id to cancel that particular Notification

I think the id part in your code is this,

i = (int) System.currentTimeMillis();