简体   繁体   English

Android:获取通知的唯一ID

[英]Android: get unique ID of notification

I've got an for block which looks like this: 我有一个for块,看起来像这样:

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. 当某个内容添加到sList时重新执行此块时,它会覆盖当前通知,因为通知的ID是相同的。 How can I prevent that from happening? 我怎样才能防止这种情况发生? How can I get a unique ID every time? 我怎样才能每次都获得一个唯一的ID? 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? 或者是否有可能避免整个ID部分,就像告诉android无论如何都必须显示通知,无论ID是什么?

Thanks in advance! 提前致谢!

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. 然后我只读了它的最后4位数。 I'm using it to create unique id every time notification is displayed. 我每次显示通知时都会使用它来创建唯一的ID。 So the probability of getting same or reset of notification id will be avoided. 因此,将避免获得相同或重置通知ID的可能性。

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. 您应该显示一个通知,整合有关事件组的信息,例如Gmail客户端。 Use Notification.Builder for that purpose. 为此目的使用Notification.Builder

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: 如果你仍然需要有很多状态栏通知,你应该在某个地方保存你的计数器的最后一个值,并像这样使用for循环:

    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. 但正如我所说,我认为这是一个糟糕的解决方案,导致您的应用程序的用户体验不佳。

you can simply specify an id in the notification builder. 您只需在通知构建器中指定一个ID即可。
Same id = update of the notification 相同的id =通知的更新
different id = new notification. 不同的id =新通知。

Also, two different apps can use the same notification id, it will spawn 2 different notifications without issue. 此外,两个不同的应用程序可以使用相同的通知ID,它将产生2个不同的通知没有问题。 the system look at both the id & the app where it comes from. 系统会查看id和应用程序的来源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM