我正在尝试发布本地通知,以便在收到远程通知时更改标签栏项目上的角标(Badge)编号。如果在应用程序打开时收到通知,我下面的 else 语句将完美触发。但是,如果应用程序在后台运行,我的前两个 if 语句似乎永远不会触发?
AppDelegate.m
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
if(application.applicationState == UIApplicationStateInactive) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:userInfo];
NSLog(@"Inactive - the user has tapped in the notification when app was closed or in background");
completionHandler(UIBackgroundFetchResultNewData);
}
else if (application.applicationState == UIApplicationStateBackground) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:userInfo];
NSLog(@"application Background - notification has arrived when app was in background");
NSString* contentAvailable = [NSString stringWithFormat:@"%@", [[userInfo valueForKey:@"aps"] valueForKey:@"content-available"]];
if([contentAvailable isEqualToString:@"1"]) {
NSLog(@"content-available is equal to 1");
completionHandler(UIBackgroundFetchResultNewData);
}
}
else {
[[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:userInfo];
NSLog(@"application Active - notication has arrived while app was opened");
completionHandler(UIBackgroundFetchResultNewData);
}
}
或者:我也尝试了下面的代码,同样的问题...
-(void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:userInfo];
NSLog(@"App notification received!");
// do stuff when app is active
}else{
static int i=1;
[UIApplication sharedApplication].applicationIconBadgeNumber = i++;
NSLog(@"App notification received background!");
}
}
修复(对于那些使用 Drupal 的人):将以下内容添加到 push_notifications.admin.inc(Drupal 推送通知文件)**
$payload['content-available'] = 1;
最佳答案
在您的推送通知负载中添加 content-available=1。 Further reading
关于iOS/Obj-C - didReceiveRemoteNotification 语句没有触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45220732/