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.
NotificationManager maintains list of all notification
mNotificationList
and performscancel
orcancelAll
operations on this listI tried searching in Google source code of NotificationManager.java and NotificationManagerService.java in
notify()
,enqueueNotificationWithTag()
andcancelAllNotification()
But there is no implementation to cancel previously unshown notificationAs per your comment, if your user is deleted and his notification arrives then user is already deleted so notification has no meaning
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 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:
Schedule an Alarm with the
AlarmManager
to send a BroadCast which the Receiver above is listening for: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.
Good Luck, and be sure to register your
Receiver
in yourActivity
or Manifest.