2016-11-04 137 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

相關問題