Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: get unique ID of notification

I've got an for block which looks like this:

for(int counter = 0; counter < sList.size(); counter++){
            String s = sList.get(counter);
            Notification notification = new NotificationCompat.Builder(this).setContentTitle("Title").setContentText(s).setSmallIcon(R.drawable.ic_launcher).setContentIntent(pendingIntent).build();
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            notificationManager.notify(counter, notification);
}

This block is located in a service which is triggered by an alarmmanager. So this block may really well be executed a couple of times before the user sees the notifications. When this block is re-executed when something has been added to the sList, it overrides the current notifications, because the ID's of the notification are the same. How can I prevent that from happening? How can I get a unique ID every time? Or is there maybe possible to avoid the whole ID part, like telling android that is has to show the notification anyway, no matter what the ID is?

Thanks in advance!

like image 656
Xander Avatar asked Oct 19 '12 16:10

Xander


3 Answers

long time = new Date().getTime();
String tmpStr = String.valueOf(time);
String last4Str = tmpStr.substring(tmpStr.length() - 5);
int notificationId = Integer.valueOf(last4Str);

notificationManager.notify(notificationId, notif);

It gets current system time. Then I'm reading only last 4 digits from it. I'm using it to create unique id every time notification is displayed. So the probability of getting same or reset of notification id will be avoided.

like image 173
satyapol Avatar answered Sep 22 '22 08:09

satyapol


I'm sure you shouldn't have so much of notifications for user at once. You should show a single notification that consolidates info about group of events like for example Gmail client does. Use Notification.Builder for that purpose.

NotificationCompat.Builder b = new NotificationCompat.Builder(c);
       b.setNumber(g_push.Counter)
        .setLargeIcon(BitmapFactory.decodeResource(c.getResources(), R.drawable.list_avatar))
        .setSmallIcon(R.drawable.ic_stat_example)
        .setAutoCancel(true)
        .setContentTitle(pushCount > 1 ? c.getString(R.string.stat_messages_title) + pushCount : title)
        .setContentText(pushCount > 1 ? push.ProfileID : mess)
        .setWhen(g_push.Timestamp)
        .setContentIntent(PendingIntent.getActivity(c, 0, it, PendingIntent.FLAG_UPDATE_CURRENT))
        .setDeleteIntent(PendingIntent.getBroadcast(c, 0, new Intent(ACTION_CLEAR_NOTIFICATION), PendingIntent.FLAG_CANCEL_CURRENT))
        .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE)
        .setSound(Uri.parse(prefs.getString(
                SharedPreferencesID.PREFERENCE_ID_PUSH_SOUND_URI,
                "android.resource://ru.mail.mailapp/raw/new_message_bells")));

If you still need to have a lot of status bar notifications, you should save the last value of your counter somewhere and use for loop like this:

    int counter = loadLastCounterValue();
    for(String s : sList){
            Notification notification = new NotificationCompat.Builder(this).setContentTitle("Title").setContentText(s).setSmallIcon(R.drawable.ic_launcher).setContentIntent(pendingIntent).build();
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            notificationManager.notify(++counter, notification);
    }
    saveCounter(counter);

But as I have said I think it is bad solution that leads to bad user experience from your app.

like image 26
rus1f1kat0R Avatar answered Sep 19 '22 08:09

rus1f1kat0R


you can simply specify an id in the notification builder.
Same id = update of the notification
different id = new notification.

Also, two different apps can use the same notification id, it will spawn 2 different notifications without issue. the system look at both the id & the app where it comes from.

like image 29
Teovald Avatar answered Sep 18 '22 08:09

Teovald