简体   繁体   English

当应用程序处于后台或应用程序中时,使用推送通知设置应用程序图标上的徽章

[英]Setting badge on app icon with push notifications when app is in background or app has been killed

After going through almost all the tutorials I am still not able to set my badges right. 经过几乎所有的教程后,我仍然无法正确设置我的徽章。 Situation is like that- When my app is in background i want my badge to get increased by 1. So I am using below line of code 情况就像那样 - 当我的应用程序处于后台时,我希望我的徽章增加1.所以我使用下面的代码行

NSLog(@"In didReceiveRemoteNotification badge number is %ld",(long)[UIApplication sharedApplication].applicationIconBadgeNumber);
[UIApplication sharedApplication].applicationIconBadgeNumber += [[[userInfo objectForKey:@"aps"] objectForKey:@"badge"] integerValue];
NSLog(@"In didReceiveRemoteNotification badge number is %ld",(long)[UIApplication sharedApplication].applicationIconBadgeNumber);

These two NSLogs gives me values 1 and 2 respectively. 这两个NSLog分别给出了值1和2。 How even before setting applicationIconBadgeNumber it gets a 1? 甚至在设置applicationIconBadgeNumber之前它得到1? And therefore it shows 2 as badge on receiving single notification. 因此它在接收单个通知时显示2作为徽章。

To make my question more clear I am posting whole code done in - didReceiveRemoteNotification 为了使我的问题更清楚,我发布完整的代码 - didReceiveRemoteNotification

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler{

if ((application.applicationState ==  UIApplicationStateBackground)) {

    NSLog(@"===========================");
    NSLog(@"App was in BACKGROUND...");

    NSLog(@"In didReceiveRemoteNotification badge number is %ld",(long)[UIApplication sharedApplication].applicationIconBadgeNumber);
    [UIApplication sharedApplication].applicationIconBadgeNumber += [[[userInfo objectForKey:@"aps"] objectForKey:@"badge"] integerValue];
    NSLog(@"In didReceiveRemoteNotification badge number is %ld",(long)[UIApplication sharedApplication].applicationIconBadgeNumber);
    [[UIApplication sharedApplication] presentLocalNotificationNow:localNotif];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:userInfo];}
if (application.applicationState ==  UIApplicationStateActive) {
    NSLog(@"===========================");
    NSLog(@"App was ACTIVE");

    NSString *str = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];
    UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"Notification Received" message:str delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    alertview.tag = 600;
    [alertview show];
}

NSLog(@"%@",userInfo);
handler(UIBackgroundFetchResultNewData);}

My second question is when my app is killed i go for below code in didFinishLaunchingWithOptions: 我的第二个问题是,当我的应用程序被杀死时,我会在didFinishLaunchingWithOptions中找到以下代码:

UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (localNotif) {
    [UIApplication sharedApplication].applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber + [[[localNotif valueForKey:@"aps"] valueForKey:@"badge"] integerValue];}

At first it didnt work at all but after reading an answer at Stackoverflow i removed UIRemoteNotificationTypeNewsstandContentAvailability and it started showing up. 起初它根本没有用,但在阅读Stackoverflow的答案后,我删除了UIRemoteNotificationTypeNewsstandContentAvailability并开始显示。 But still no increment was shown. 但仍然没有显示增量。 Badge value remained 1. 徽章价值仍为1。

Any help would be appreciated. 任何帮助,将不胜感激。

You should not need to set the badge manually upon receiving a remote notification as iOS will set it automatically for you using the value in aps -> badge. 您不需要在收到远程通知时手动设置徽章,因为iOS会使用aps - >徽章中的值自动为您设置徽章。 This will work out of the box whether your app is active, inactive, in background or not even running (killed). 无论您的应用处于活动状态,非活动状态,背景状态还是不运行(已杀死),这都可以开箱即用。

However iOS doesn't use the value to increment the badge, but to set it. 但是,iOS不会使用该值来增加徽章,而是设置它。 So if your badge value is n in the push notification, regardless of the current badge value of your application, iOS will set it to n (not current + n). 因此,如果推送通知中的徽章值为n,则无论应用程序的当前徽章值如何,iOS都会将其设置为n(非当前+ n)。 All the applications where the badge apparently increments when receiving a notification actually manage the badge count on their server and send the appropriate incremented value in the notification. 接收通知时徽章显然增加的所有应用程序实际上管理其服务器上的徽章计数,并在通知中发送适当的递增值。

If thats the behavior that you want, this is what you should do. 如果这就是你想要的行为,这就是你应该做的。 Typically you have a badgeCount property associated with each device on your server and you increment it every time you send a push. 通常,您有一个与服务器上的每个设备关联的badgeCount属性,并且每次发送推送时都会增加该属性。 Then when the user opens your app you send a reset call to your server that resets this value to 0, and subsequent notifications will start back at 1. 然后,当用户打开您的应用程序时,您向服务器发送重置调用,将此值重置为0,随后的通知将从1开始。

Badge count is managed by iOS itself it is totally dependent on OS when app is in background or killed state. 徽章计数由iOS本身管理,当应用程序处于后台或被杀死状态时,它完全依赖于操作系统。

The server side will handle the count and do the calculation. 服务器端将处理计数并进行计算。

Please take a look at this link: 请看一下这个链接:

" Update badge with push notification while app in background " 在后台应用时通过推送通知更新徽章

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

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