我相信你不应该立即为用户提供这么多通知。 您应该显示一条通知,整合有关事件组的信息,例如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循环:
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);
但正如我所说,我认为这是一个糟糕的解决方案,导致您的应用程序的用户体验不佳。