简体   繁体   中英

How to update notification badge number in iOS's home screen when received push message?

I want to iOS device can auto update the budges number in home screen (app not run). Currently, I'm using push notification and it's can receive push notifications OK. But the budges number don't auto update (it's can only update after I clicked to view detail push notice). My questions is how to reload budges number when received push message with app not run (background app) - like LINE chat app? My function to show budges number in AppDelegate like bellow:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    int Total_budges = [[userInfo objectForKey:@"badge_count"] intValue];
[UIApplication sharedApplication].applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber + Total_budges;
}

Thanks all!

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo this Method never going to be called when you application is backGround State else in-active state so just You need Manage it's Badge count from back-End side.

That from you APNS PayLoad with Key or badge must be have an Integer value. like

badge": 1 // that value (1) must be an Integer else that badge not display.

so When Push notification arrive that Apple manage automatically display that badge icon on you application icon.

For Clear count when application open used:

    [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
    [[UIApplication sharedApplication] cancelAllLocalNotifications];

Thanks everybody answered me! I was retry and now It's run OK! All steps I do: 1- Recount Total budges in server and response to Apple push message 2- Return with Key "badge"

$body['aps'] = array(
                'alert' => $message,
                'badge' => $Total_msg_unread,
                'sound' => 'default'
                );

Before read Chinttu RoxeN Ramani's reply, I set 'badge' like custom key=> it's can't run. Maybe it's my issue!

3- In Xcode convert budges value to IntValue. One again, Thanks everybody!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM