Android - 如何更新通知编号

DkP*_*hak 8 notifications android updates

嗨我想在一个视图中显示所有通知..并想要更新状态栏中的通知数...它更新所有信息但显示数字始终为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);
}
Run Code Online (Sandbox Code Playgroud)

And*_*tto 19

你必须跟踪计数.您可以扩展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;
    }
}
Run Code Online (Sandbox Code Playgroud)

你应该修改onReceive:

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

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

AppName.setPendingNotificationsCount(0);
Run Code Online (Sandbox Code Playgroud)

  • 非常荒谬的框架如何没有一个简单的`getNotifications(int id)`调用来简单地检查这个...... (13认同)
  • 不幸的是,如果应用程序被杀死,计数器将重置...可能应该保存到SharedPreference以保持持久性 (5认同)