我敢肯定,您不应该一次向用户发送这么多通知。您应该显示一个通知,该通知整合了有关一组事件的信息,例如 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);
但正如我所说,我认为这是一个糟糕的解决方案,会导致您的应用程序的用户体验不佳。