[英]Remote Push Notification badge count not increasing when app is in background
[英]Objective-c badge count on app icon when notification received in background?
当应用在后台运行时收到通知时,我正在使用以下代码在应用图标上设置徽章。 就是说,在最小化应用程序的同时收到通知时,我的代码/日志从不会触发(请参阅日志:“ NSLog应用程序处于后台”),我不确定为什么吗?
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
[[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:userInfo];
NSLog(@"application Active - notication has arrived while app was opened");
completionHandler(UIBackgroundFetchResultNewData);
NSLog(@"Notification received when open");
if(application.applicationState == UIApplicationStateInactive) {
NSLog(@"Inactive - the user has tapped in the notification when app was closed or in background");
completionHandler(UIBackgroundFetchResultNewData);
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UITabBarController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"tabBarController"]; // determine the initial view controller here and instantiate it with [storyboard instantiateViewControllerWithIdentifier:<storyboard id>];
[viewController setSelectedIndex:0];
;
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
[[NSNotificationCenter defaultCenter] postNotificationName:@"myNotificationReceived" object:nil];
}
if (application.applicationState == UIApplicationStateBackground) {
NSLog(@"APP WAS IN BACKGROUND");
static int i=1;
[UIApplication sharedApplication].applicationIconBadgeNumber = i++;
}
}
- (void)applicationDidEnterBackground:(UIApplication *)application{
static int i=0;
[UIApplication sharedApplication].applicationIconBadgeNumber = i;
NSLog(@"Triggered!");
}
这是远程通知的正确行为。
当您的应用程序在后台运行时,您的应用程序将不会收到didReceiveRemoteNotification
调用, 除非用户点击通知警报 。
运作方式如下。
1)当您的应用程序在后台(或挂起)并且收到远程通知时,将显示iOS系统警报。
2)如果用户通过点击通知警报打开您的应用程序,则您的应用程序将移动前台并调用didReceiveRemoteNotification
。
3)如果用户忽略或关闭了该通知,则您的应用程序将保留在后台,并且didReceiveRemoteNotification
将不会被调用 。
也就是说,无需在代码中设置应用程序徽章。 您的推送通知有效负载可以包含一个密钥,当系统收到您的通知时,iOS会使用该密钥设置应用程序徽章。
您badge
在通知的有效负载中包含关键badge
:
{
"aps" : {
"alert" : {
"title" : "Notification title",
"body" : "Notification body"
},
"badge" : 5
}
}
我建议您看一下Apple的有关创建远程通知有效内容的文档,其中介绍了所有选项:
这一切都这样说,这是可以确保的iOS调用didReceiveRemoteNotification
当你的应用程序是在后台。
为此,您需要在有效负载中设置content-available
参数并发送“静默”通知。 静默通知意味着用户将永远不会看到警报,但是您的应用将在有限的时间内静默地出现在前台,并会调用didReceiveRemoteNotification
。
但是,这不是适合您的方案的选择。 它旨在更新应用程序的内容,而不仅仅是更新徽章。
但是,如果您对静默通知感兴趣,可以在此处查看文档:
[UIApplication sharedApplication].applicationIconBadgeNumber = 3;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.