简体   繁体   English

Apple推送通知徽章编号

[英]Apple push notification badge number

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? 或者它会显示为直到我们设置为零?

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. 在接收通知时,更常见的是在application:didReceiveRemoteNotification:设置徽章编号application:didReceiveRemoteNotification:application:didFinishLaunchingWithOptions: UIApplicationDelegate类的方法。

You can read more about it in the Local and Push Notification Programming Guide 您可以在“ 本地和推送通知编程指南”中阅读有关它的更多信息

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. 我们还需要更改php文件。 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
    );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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