2016-11-04 10 views
0

iOS 8と9の場合、アプリケーションがバックグラウンドのときにプッシュ通知を受け取ったときに、アプリに情報を保存できます。しかし、iOS 10では、アプリがバックグラウンドにあるときに情報を取得できません。しかし、通知を開く/クリックした場合にのみ、userNotification centerDidReceiveNotificationResponseが呼び出されます。iOS 10のアプリがバックグラウンドのときにプッシュ通知にアクセスできません

iOS 8 & 9の場合、これは問題なく動作します。メソッドが呼び出されていないiOSの10のために

-(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); 
    } 
} 

- (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とその代理人を設定しましたか? @シャロン –

答えて

2

私はあなたが下記の手順と思っています。

ステップ1:UserNotifications」フレームワークをインポートします。

#import <UserNotifications/UserNotifications.h> 

ステップ2:そして、あなたのAppDelegate

@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate> 

"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(); 
} 

あなたのコードは、それぞれのコードwillPresentNotificationdidReceiveNotificationResponse

completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge); 

そして

completionHandler(); 

の線の下に表示されません。

+0

補完ハンドラコードを追加しましたが、まだメソッドが呼び出されていません – Sharon

関連する問題