2016-11-04 113 views
0

对于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); 
} 
+0

在后台模式下启用远程通知功能 –

+0

@KevinMac我已启用 – Sharon

+0

您是否配置了UNUserNotificationCenter及其代理? @Sharon –

回答

2

我希望你按照下面的步骤。

第1步:导入“用户通知”框架。

#import <UserNotifications/UserNotifications.h> 

第2步:和你的AppDelegate确认 “UNUserNotificationCenterDelegate” 委托

@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate> 

第3步:对于iOS10,你可以要求这样

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; 
center.delegate = self; 
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){ 
    if(!error){ 
     // Permission for notifications is granted. Do the other code 
    } 
}]; 
许可

现在我认为你在代码中的问题处理 用户通知代理方法

//Called when a notification is delivered to a foreground app. 
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{ 
    NSLog(@"User Info : %@",notification.request.content.userInfo); 
    completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge); 
} 

//Called to let your app know which action was selected by the user for a given notification. 
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{ 
    NSLog(@"User Info : %@",response.notification.request.content.userInfo); 
    completionHandler(); 
} 

你的代码是缺失下面的代码

completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge); 

而且

completionHandler(); 

线在willPresentNotificationdidReceiveNotificationResponse

+0

我已添加完成处理程序代码,但仍未调用该方法 – Sharon

相关问题