xcode Apple 推送通知徽章编号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6424938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:18:45  来源:igfitidea点击:

Apple push notification badge number

objective-cxcodebadge

提问by Amit

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?

我的问题是,当我们打开应用程序时,徽章编号会自动从(应用程序)设备中删除?或者它会一直显示直到我们设置为零?

回答by Felipe Sabino

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 UIApplicationDelegateclass.


编辑:
这是比较常见的,你收到通知,在任何设置徽章号码application:didReceiveRemoteNotification:application:didFinishLaunchingWithOptions:您的方法UIApplicationDelegate类。

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

您可以在本地和推送通知编程指南中阅读更多相关信息

回答by Anit Kumar

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

我们还需要更改 php 文件。所以我们可以自动获得更改图标徽章

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