如何更新通知编号

如何更新通知编号

本文介绍了如何更新通知编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示在一个单一的视图中的所有通知..和希望更新的通知状态栏数...它更新所有信息,但表示数量始终为1 ..请告诉我如何解决这个问题..

  @覆盖
公共无效的onReceive(上下文的背景下,意图意图)
{
    //随机randGen =新的随机();
    // INT notify_id = randGen.nextInt();
    NotificationManager notificationManager =(NotificationManager)
        context.getSystemService(Activity.NOTIFICATION_SERVICE);
    字符串标题= intent.getStringExtra(TableUtils.KEY_TITLE);
    字符串场合= intent.getStringExtra(TableUtils.KEY_OCCASION);
    通知通知=
        新的通知(R.drawable.icon,爱Cardz
                         System.currentTimeMillis的());
    // notification.vibrate =新长[] {100,250,300,330,390,420,500};
    notification.flags | = Notification.FLAG_AUTO_CANCEL;
    notification.number + = 1;
    意图int​​ent1 =新的意图(背景下,ThemesBrowserActivity.class);
    PendingIntent活动=
        PendingIntent.getActivity(上下文,1,intent1,
                                  PendingIntent.FLAG_UPDATE_CURRENT);
    notification.setLatestEventInfo(背景,场合,标题,活动);
    notificationManager.notify(1,通知);
}
 

解决方案

您必须跟踪计数的。你可以扩展应用程序类:

 公共类AppName的扩展应用{
    私有静态诠释pendingNotificationsCount = 0;

    @覆盖
    公共无效的onCreate(){
        super.onCreate();
    }

    公共静态INT getPendingNotificationsCount(){
        返回pendingNotificationsCount;
    }

    公共静态无效setPendingNotificationsCount(INT pendingNotifications){
        pendingNotificationsCount = pendingNotifications;
    }
}
 

和您应该修改的onReceive:

  @覆盖
公共无效的onReceive(上下文的背景下,意图意图){
    ...
    INT pendingNotificationsCount = AppName.getPendingNotificationsCount()+ 1;
    AppName.setPendingNotificationsCount(pendingNotificationsCount);
    notification.number = pendingNotificationsCount;
    ...
}
 

和可以重新计数,当用户打开通知:

  AppName.setPendingNotificationsCount(0);
 

Hi i want to show all the notification in a single view .. and want to update number of notification in status bar ... its updating all info but showing number always 1.. please tell me how to solve it...

@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);
}
解决方案

You have to keep track of the count. You could extend the Application class:

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;
    }
}

And you should modify the onReceive:

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

And you could reset the count when the user open the notification:

AppName.setPendingNotificationsCount(0);

这篇关于如何更新通知编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 02:35