如何更新通知号码

嗨,我想在一个单一的视图显示所有的通知..并要更新状态栏中的通知数…它更新所有信息,但显示数字总是1 ..请告诉我如何解决它…

@Override public void onReceive(Context context, Intent intent) { //Random randGen = new Random(); //int notify_id = randGen.nextInt(); NotificationManager notificationManager = (NotificationManager) context.getSystemService(Activity.NOTIFICATION_SERVICE); String title = intent.getStringExtra(TableUtils.KEY_TITLE); String occasion = intent.getStringExtra(TableUtils.KEY_OCCASION); Notification notification = new Notification(R.drawable.icon, "Love Cardz" , System.currentTimeMillis()); // notification.vibrate = new long[]{100,250,300,330,390,420,500}; notification.flags |= Notification.FLAG_AUTO_CANCEL; notification.number+=1; Intent intent1 = new Intent(context, ThemesBrowserActivity.class); PendingIntent activity = PendingIntent.getActivity(context, 1 , intent1, PendingIntent.FLAG_UPDATE_CURRENT); notification.setLatestEventInfo(context, occasion, title, activity); notificationManager.notify(1, notification); } 

Solutions Collecting From Web of "如何更新通知号码"

你必须跟踪计数。 你可以扩展Application类:

 public class AppName extends Application { private static int pendingNotificationsCount = 0; @Override public void onCreate() { super.onCreate(); } public static int getPendingNotificationsCount() { return pendingNotificationsCount; } public static void setPendingNotificationsCount(int pendingNotifications) { pendingNotificationsCount = pendingNotifications; } } 

你应该修改onReceive:

 @Override public void onReceive(Context context, Intent intent) { ... int pendingNotificationsCount = AppName.getPendingNotificationsCount() + 1; AppName.setPendingNotificationsCount(pendingNotificationsCount); notification.number = pendingNotificationsCount; ... } 

您可以在用户打开通知时重置计数:

 AppName.setPendingNotificationsCount(0); 

这是我的代码,它的工作原理。 我只testing过旧的Android版本。 我怀疑新版本的“数字”徽章已被隐藏,但我没有机会去testing它。

 void notifyMsgReceived(String senderName, int count) { int titleResId; String expandedText, sender; // Get the sender for the ticker text // ie "Message from <sender name>" if (senderName != null && TextUtils.isGraphic(senderName)) { sender = senderName; } else { // Use "unknown" if the sender is unidentifiable. sender = getString(R.string.unknown); } // Display the first line of the notification: // 1 new message: call name // more than 1 new message: <number of messages> + " new messages" if (count == 1) { titleResId = R.string.notif_oneMsgReceivedTitle; expandedText = sender; } else { titleResId = R.string.notif_missedCallsTitle; expandedText = getString(R.string.notif_messagesReceivedTitle, count); } // Create the target intent final Intent intent = new Intent(this, TargetActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); final PendingIntent pendingIntent = PendingIntent.getActivity(this, REQUEST_CODE_MSG_RECEIVED, intent, PendingIntent.FLAG_UPDATE_CURRENT); // Build the notification Notification notif = new Notification( R.drawable.ic_notif_msg_received, // icon getString(R.string.notif_msgReceivedTicker, sender), // tickerText System.currentTimeMillis()); // when notif.setLatestEventInfo(this, getText(titleResId), expandedText, pendingIntent); notif.number = count; notif.flags = Notification.FLAG_AUTO_CANCEL; // Show the notification mNotificationMgr.notify(NOTIF_MSG_RECEIVED, notif); } 

稍后更新通知也很容易:只需用新值再次调用该方法即可。 编号将显示在通知图标徽章中,当且仅当创build通知时大于零。

同样,如果您将号码设置为小于1的数字,数字徽章将不会被隐藏(数字将会是您)。也许在重新显示通知之前清除该通知可以解决该问题。

你必须跟踪计数。 你尝试在notif.number上执行的增量不起作用,因为该状态是不可用的(即notif.number总是0,那么你将它增加到1)。 跟踪你的应用程序中的某个地方的数字(可能是共享偏好),并在那里增加和存储它,然后当你build立通知时,设置

 notif.number = myStoredValueForWhatShouldShowInNumber; 

试试看。