對於iOS 8和9,當應用程序位於後臺時,當收到推送通知時,我可以將其信息存儲在應用程序中。但是對於iOS 10,當應用程序位於後臺時,我無法檢索信息。但是,只有當我打開/點擊通知時,userNotification centerDidReceiveNotificationResponse纔會被調用。當應用程序處於後臺時,無法訪問推送通知10
對於iOS 8 & 9,這工作正常。
-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
// iOS 10 will handle notifications through other methods
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0"))
{
NSLog(@"iOS version >= 10. Let NotificationCenter handle this one.");
// set a member variable to tell the new delegate that this is background
return;
}
NSLog(@"HANDLE PUSH, didReceiveRemoteNotification: %@", userInfo);
// custom code to handle notification content
if([UIApplication sharedApplication].applicationState == UIApplicationStateInactive)
{
NSLog(@"INACTIVE");
completionHandler(UIBackgroundFetchResultNewData);
}
else if([UIApplication sharedApplication].applicationState == UIApplicationStateBackground)
{
NSLog(@"BACKGROUND");
completionHandler(UIBackgroundFetchResultNewData);
}
else
{
NSLog(@"FOREGROUND");
completionHandler(UIBackgroundFetchResultNewData);
}
}
對於iOS 10不調用該方法,
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
NSLog(@"Handle push from foreground");
// custom code to handle push while app is in the foreground
NSLog(@"%@", notification.request.content.userInfo);
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)())completionHandler
{
NSLog(@"Handle push from background or closed");
// if you set a member variable in didReceiveRemoteNotification, you will know if this is from closed or background
NSLog(@"%@", response.notification.request.content.userInfo);
}
在後臺模式下啓用遠程通知功能 –
@KevinMac我已啓用 – Sharon
您是否配置了UNUserNotificationCenter及其代理? @Sharon –