1

I want to check if app notification is running. I have tried various illustration . It the test always returns false even when the notification is running. Implemented notification as:

NotificationManager mNotificationManager = (NotificationManager)
                    context.getSystemService(Context.NOTIFICATION_SERVICE);
            RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.zone_recharge_notification);
            remoteViews.setOnClickPendingIntent(R.id.zn_recharge, getPendingSelfIntent(context));
            remoteViews.setOnClickPendingIntent(R.id.zn_launch, getLaunchZone(context));

            NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(context)
                            .setSmallIcon(R.drawable.zone_holo_logo_48)
                            .setOngoing(true)
                            .setOnlyAlertOnce(true)
                            .setPriority(NotificationCompat.PRIORITY_MAX)
                            .setContent(remoteViews)
                            .setAutoCancel(false);


            mNotificationManager.notify(AppConstant.RECHARGE_ME_NOTIFICATION_ID, mBuilder.build());

and using the snippet below to check if it is running.

 public static boolean isNotificationVisible(Context context) {
        Intent notificationIntent = new Intent(context, WindowServiceDialog.class);
        PendingIntent test = PendingIntent.getService(context, AppConstant.RECHARGE_ME_NOTIFICATION_ID, notificationIntent, PendingIntent.FLAG_NO_CREATE);
        return test != null;
    }

read this but is was Added in API level 18 and i want something that can work on ics devices. How can i fix this ? Thanks.

edit

pending intent of each remoteView component

protected PendingIntent getPendingSelfIntent(Context context) {
        Intent intent = new Intent(context, AppReceiver.class);
        return PendingIntent.getBroadcast(context, 0, intent, 0);
    }

    protected PendingIntent getLaunchZone(Context context) {
        Intent intent = new Intent(context, WindowServiceDialog.class);
        return PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    }
Community
  • 1
  • 1
Belvi Nosakhare
  • 3,107
  • 5
  • 32
  • 65
  • How is `isNotificationVisible()` supposed to work? You are just creating a new `PendingIntent` instance. `getService` but no means implies that it gets an already existing `PendingIntent`, it creates a new instance every time. – Xaver Kapeller Oct 07 '15 at 08:57
  • And to answer your question: it is best if you design your app in a way which does not require knowing which notifications are visible. It's best to just show the notifications if one is needed and then to forget about it. Having some kind of logic later on which needs to refer to the notification is a bad thing. However if you really need to know if a notification is visible and/or what that notification is all about then you can always just persist that information yourself somewhere, for example in a database. – Xaver Kapeller Oct 07 '15 at 08:59
  • from the stackoverflow thread reference on the question,isNotificationVisible() should return true is a notification is currently runing with the specified pending intent – Belvi Nosakhare Oct 07 '15 at 09:16
  • That is simply not true. How would `getLaunchZone()` ever work if `getService()` would just return `PendingIntent`s which already existed? Always just trust the [**official documentation**](http://developer.android.com/reference/android/app/PendingIntent.html#getService) and your own common sense. `getService` cannot in one instance create new `PendingIntents` and in another return already existing ones. Like the documentation says, it just creates a `PendingIntent` which starts a specific `Service`. – Xaver Kapeller Oct 07 '15 at 09:21
  • If you want proof just look at the source code of `getService()`, it creates a new `PendingIntent` every time it is called. Just right clicking on `getService()` and selecting 'Go To' -> 'Implementation(s)' would have answered you that question in a few seconds. – Xaver Kapeller Oct 07 '15 at 09:27
  • isNotificationVisible now always return true – famfamfam Mar 03 '21 at 17:39

1 Answers1

-1

With mBuilder.setDeleteIntent(deletePendingIntent); (javadoc) you can set a pending intent for your notification that is triggered, when the notification is cleard. Then create an IntentService (or any other Intent receiver) that takes note of the deletion and stores that information somewhere (e.g. SharedPreferences). In your isNotificationVisible operation you can then check the value in the sharedPref.

Florian Barth
  • 1,392
  • 12
  • 25