我相信你不应该一次性收到太多用户通知。你应该像 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);
但正如我所说,我认为这是一个不好的解决方案,会导致您的应用程序用户体验不佳。