2016-11-04 3 views
0

Für iOS 8 und 9, wenn die Anwendung im Hintergrund ist, wenn eine Push-Benachrichtigung empfangen wird, kann ich die Informationen in der App speichern. Aber für iOS 10 kann ich die Informationen nicht abrufen, wenn sich die App im Hintergrund befindet. Aber nur wenn ich die Benachrichtigung öffne/klicke, wird userNotification centerDidReceiveNotificationResponse aufgerufen.Zugriff auf Push-Benachrichtigungen nicht möglich, wenn App im Hintergrund für iOS 10 ist

Für iOS 8 & 9, das funktioniert gut.

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

Für iOS 10 die Methode nicht aufgerufen wird,

- (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

aktivieren Remote-Benachrichtigung im Hintergrund Modi Fähigkeiten –

+0

@KevinMac Ich habe aktiviert – Sharon

+0

haben Sie das UNUserNotificationCenter und seine Delegate konfiguriert? @Sharon –

Antwort

2

Ich hoffe, Sie unten beschriebenen Schritte folgen haben.

Schritt 1: Importieren "UserNotifications" Framework.

#import <UserNotifications/UserNotifications.h> 

Schritt 2: Und Ihre AppDelegate Bestätigung "UNUserNotificationCenterDelegate" delegieren

@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate> 

Schritt 3: Für iOS10, Sie können für die Erlaubnis fragen, wie diese

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 
    } 
}]; 

Jetzt denke ich, das Problem, das Sie in Ihrem Code in Handhabung Delegate Methoden für UserNotifications haben.

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

Ihr Code wird unter Leitung von Codes fehlt

completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge); 

Und

completionHandler(); 

In willPresentNotification und didReceiveNotificationResponse sind.

+0

Ich habe Completion-Handler-Code hinzugefügt, aber die Methode wird noch nicht aufgerufen – Sharon

Verwandte Themen