Is it possible to check if a notification is visible or canceled?

匿名 (未验证) 提交于 2019-12-03 00:59:01

问题:

I would like to update notification data, but the only way I found is to launch a new one with the same Id.

The problem is that I don't want to raise a new one if the original has beed canceled. Is there a way to tell if a notification is visible or canceled? Or a way to update a notification only if it exists?

回答1:

This is how I solved it:

    private boolean isNotificationVisible() {     Intent notificationIntent = new Intent(context, MainActivity.class);     PendingIntent test = PendingIntent.getActivity(context, MY_ID, notificationIntent, PendingIntent.FLAG_NO_CREATE);     return test != null; } 

This is how I generate the notification:

    /**  * Issues a notification to inform the user that server has sent a message.  */ private void generateNotification(String text) {      int icon = R.drawable.notifiaction_icon;     long when = System.currentTimeMillis();     NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);     Notification notification = new Notification(icon, text, when);     String title = context.getString(R.string.app_name);     Intent notificationIntent = new Intent(context, MainActivity.class);      // set intent so it does not start a new activity     //notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);     PendingIntent intent = PendingIntent.getActivity(context, MY_ID, notificationIntent, 0);     notification.setLatestEventInfo(context, title, text, intent);      notification.flags |= Notification.FLAG_AUTO_CANCEL; //PendingIntent.FLAG_ONE_SHOT      notificationManager.notify(MY_ID, notification); } 


回答2:

Salam. If you're using API >= 23 can use this method to get active notification:

StatusBarNotification[] notifications = mNotificationManager.getActiveNotifications(); for (StatusBarNotification notification : notifications) {   if (notification.getId() == 100) {     // Do something.   } } 


回答3:

I think you can use deleteIntent of Notification class.

I remember in one of my application I use use to fire a Broadcast (custom Broadcast) when a notification is cancelled or the Notification tray was cleared.



回答4:

An alternative to the deleteIntent is the following which has proved beneficial in my own app:

Basically, you create an intent with your notification that starts an IntentService (or any other service) and in onHandleIntent you can set a flag indicating whether the notification is active.
You can set this intent to be fired when the user taps the notification (contentIntent) and/or when the user clears it from the list (deleteIntent).

To illustrate it, here is what I do in my own app. When building the notification I set

Intent intent = new Intent(this, CleanupIntentService.class); Notification n = NotificationCompat.Builder(context).setContentIntent(         PendingIntent.getActivity(this, 0, intent, 0)).build(); 

When the notification is tapped, my CleanupIntentService is launched, setting a flag (in the service that created the notification):

@Override public int onStartCommand(Intent intent, int flags, int startId) {     super.onCreate(); // If removed, onHandleIntent is not called     return super.onStartCommand(intent, flags, startId); }   @Override protected void onHandleIntent(Intent intent) {     OtherService.setNotificationFlag(false); } 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!