tags:

views:

95

answers:

2

I have made an app that sets notifications in the drop-down status bar of Android phones. However, there is a bug in my code (sometimes the notifications are set, sometimes they are not). I want to be able TO CHECK (in the code) IF THE NOTIFICATION IS VISIBLE TO THE USER. (i.e. can the user see the notification in the status bar?).

How can I do this? (Thanks in advance).

Sample code is greatly appreciated.

+2  A: 

You need to set an id for each notification you make.

so you make a notification ..

PendingIntent pendingIntent = PendingIntent.getBroadcast(this,
         notId + selectedPosition, intent, 0);

 AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
   alarmManager.set(AlarmManager.RTC_WAKEUP, rightNow
     .getTimeInMillis()
     - offset, pendingIntent);
Notification notification = new Notification(R.drawable.icon,
        "TVGuide Υπενθύμιση", System.currentTimeMillis());
    NotificationManager manger = (NotificationManager) context
        .getSystemService(context.NOTIFICATION_SERVICE);
    notification.setLatestEventInfo(context, "Κανάλι: "
       + b.getString("channel"), "Εκπομπή: " + showname,
        pendingIntent );
    manger.notify(notId, notification);

to clear it..

PendingIntent pendingIntent = PendingIntent.getBroadcast(context,notId, intent, 0); 
pendingIntent.cancel();

and to check if active..( existAlarm returns null if no pending intent available)

 public PendingIntent existAlarm(int id) {
  Intent intent = new Intent(this, alarmreceiver.class);
  intent.setAction(Intent.ACTION_VIEW);
  PendingIntent test = PendingIntent.getBroadcast(this, id
    + selectedPosition, intent, PendingIntent.FLAG_NO_CREATE);
  return test;
 }

So everything comes down to initialize an ID for each notification and how you make it unique.

weakwire
@weakwire: none of your code snippets have anything to do with notifications.
CommonsWare
@CommonsWare: If i call a notification with a pending intent i can check if Pending intent exists then notification exists also
weakwire
@weakwire: Ah, I see where you're going. Interesting technique -- I haven't tried this or seen it elsewhere for this purpose.
CommonsWare
+3  A: 

I want to be able TO CHECK (in the code) IF THE NOTIFICATION IS VISIBLE TO THE USER. (i.e. can the user see the notification in the status bar?).

How can I do this?

You can't, sorry.

However, you can always simply cancel() it -- canceling a Notification that is not on-screen is perfectly fine. Conversely, you can always safely call notify() again for the same Notification, and it too will not cause a problem if the Notification is already on-screen.

CommonsWare
it will write again the Init text for the notification if you call notify again
weakwire
@weakwire: only if the notification was not already on-screen.
CommonsWare
@CommonsWare: didn't know that.Doesn't answer the question but serves the purpose with 2 lines of code. +1
weakwire