Apple push notification badge number

↘锁芯ラ 提交于 2019-12-21 04:16:16

问题


I have developed server side application to maintain the badge number as increment or decrement after receiving new notification and delete after seeing notification it works fine.

But there is some problem in showing the badge, the actual scenario is - After getting new notification on device, I am click on cancel button then badge number shows correctly but after that I will open the application and close the application badge will be removed. That means I am not sending request to the server that notification was seen by me and now you can decrement the badge by one. Then also badge removed from app icon.

My question is that when we open the application then badge number automatically removed from (application) device? or it will shows as it is until we set to zero?


回答1:


It will show until you set it to zero and you can do it with the following code:

    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]


EDIT:
It is more common to set the badge number as you receive the notification, in either application:didReceiveRemoteNotification: or application:didFinishLaunchingWithOptions: methods of your UIApplicationDelegate class.

You can read more about it in the Local and Push Notification Programming Guide




回答2:


If you want to change the icon badge automatically use the following code.

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{

    application.applicationIconBadgeNumber = 0;
    NSLog(@"userInfo %@",userInfo);

    for (id key in userInfo) {
        NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]);
    }

    [application setApplicationIconBadgeNumber:[[[userInfo objectForKey:@"aps"] objectForKey:@"badge"] intValue]];

    NSLog(@"Badge %d",[[[userInfo objectForKey:@"aps"] objectForKey:@"badge"] intValue]);

}

We also need to change the php file. So we can get the change the icon badge automatically

// Create the payload body
$body['aps'] = array(
    'alert' => $message,
    'sound' => 'default',
    'id' => '135',
    'badge' => 8
    );


来源:https://stackoverflow.com/questions/6424938/apple-push-notification-badge-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!