Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android:获取通知的唯一ID_Java_Android_Notifications - Fatal编程技术网

Java Android:获取通知的唯一ID

Java Android:获取通知的唯一ID,java,android,notifications,Java,Android,Notifications,我有一个for块,看起来像这样: for(int counter = 0; counter < sList.size(); counter++){ String s = sList.get(counter); Notification notification = new NotificationCompat.Builder(this).setContentTitle("Title").setContentText(s).setSmallIc

我有一个
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);
}
    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);
for(int counter=0;counter
此块位于由alarmmanager触发的服务中。因此,在用户看到通知之前,这个块可能会执行几次。 当在sList中添加内容后重新执行此块时,它将覆盖当前通知,因为通知的ID相同。 我怎样才能防止这种情况发生?如何每次都获得唯一的ID?或者有没有可能避免整个ID部分,比如告诉android,不管ID是什么,它都必须显示通知


提前谢谢

我相信您不应该一次就为用户发送这么多通知。您应该显示一个单一的通知,以整合有关事件组的信息,例如Gmail客户端。为此,请使用
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")));
如果您仍然需要很多状态栏通知,则应将计数器的最后一个值保存在某个位置,并使用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);
}
    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);

但正如我所说的,我认为这是一个糟糕的解决方案,它会导致应用程序的用户体验不好。

您只需在通知生成器中指定一个id即可。
相同id=通知的更新
不同id=新通知

另外,两个不同的应用程序可以使用相同的通知id,它将生成两个不同的通知而不会出现问题。系统会同时查看id和它来自的应用程序

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);

它获取当前系统时间。然后我只读取它的最后4位数字。每次显示通知时,我都使用它来创建唯一的id。因此,将避免获得相同或重置通知id的可能性。

您是否介意添加一个解释,让我们了解您的代码的功能?它获取当前系统时间。然后我只读取它的最后4位数字。每次显示通知时,我都使用它来创建唯一的id。因此,获得相同或重置通知id的可能性将被避免。谢谢,我将在您的回答中添加解释。您可以改为使用:(int)((new Date().getTime()/1000L)%Integer.MAX_值),它每秒为您提供一个新的ID,并且在68年内不会重复,或者不除以1000,它每微秒为您提供一个ID,并且在24天内不会重复。使用用于生成通知的支持库,现在可以绑定(分组)通知。这里有一个简单的解决方法,同时确保Id的唯一性: