2016-12-01 5 views
1

Hallo, ich bin FCM implementieren (Fire-Base-Cloud-Messaging). Ich habe die App so eingerichtet, dass sie Benachrichtigungen und Daten von der FCM-Konsole erhält. Ich habe sowohl meine Serverbenachrichtigung als auch meine Firebase-Benachrichtigung. Ich möchte zwei Arten von Benachrichtigungen (von meinem Server und von FCM) getrennt behandeln. Ich habe durch die Dokumentation gegangen und dies kann durch Umstellen der Benachrichtigungs-Handler in AppDelegateFirebase-Messaging-Methode Swizzling funktioniert nicht

zu erreichen, aber das Problem ist, ich kann nur FCM-Nachrichtendaten (IOS 10) in

-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {} 

und

-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {} 
erhalten

der Rückruf für FCM Delegaten

// The callback to handle data message received via FCM for devices running iOS 10 or above. 
- (void)applicationReceivedRemoteMessage:(nonnull FIRMessagingRemoteMessage *)remoteMessage { 

    // Print full message 
    NSLog(@"Can get here firebase?%@", [remoteMessage appData]); 
} 

der oben Rückruf ist ca nie

Wie können wir Swizzling verwenden, um die FCM-Handler-Nachricht separat einzurichten?

Hier ist meine AppDelegate didfinishlaunchingwith Option

application.applicationIconBadgeNumber = 0; 

    if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) { 
     UIUserNotificationType allNotificationTypes = 
     (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge); 
     UIUserNotificationSettings *settings = 
     [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil]; 
     [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; 
    } else { 
     // iOS 10 or later 
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 
     UNAuthorizationOptions authOptions = 
     UNAuthorizationOptionAlert 
     | UNAuthorizationOptionSound 
     | UNAuthorizationOptionBadge; 
     [[UNUserNotificationCenter currentNotificationCenter] 
     requestAuthorizationWithOptions:authOptions 
     completionHandler:^(BOOL granted, NSError * _Nullable error) { 
     } 
     ]; 

     // For iOS 10 display notification (sent via APNS) 
     [[UNUserNotificationCenter currentNotificationCenter] setDelegate:self]; 
     // For iOS 10 data message (sent via FCM) 

     [[FIRMessaging messaging] setRemoteMessageDelegate:self]; 
#endif 
    } 

    [[UIApplication sharedApplication] registerForRemoteNotifications]; 

    // [START configure_firebase] 
    [FIRApp configure]; 
    // [END configure_firebase] 
    // Add observer for InstanceID token refresh callback. 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tokenRefreshNotification:) 
               name:kFIRInstanceIDTokenRefreshNotification object:nil];  

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    self.window.backgroundColor = [UIColor whiteColor]; 

    _rootViewController = [[RootViewController alloc] initWithNibName:nil bundle:nil]; 


    [self.window setRootViewController:_rootViewController]; 
    [self.window makeKeyAndVisible]; 

    NSDictionary *remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; 
    if (remoteNotification) 
     [self handleRemoteNotification:remoteNotification shouldPrompt:NO]; 

    self.isFirstTime = true; 
    self.isJustForRefresh = 0; 

    return YES; 

Wer weiß, wie es funktioniert?

Edit: hier ist meine Beobachter Beobachtungsfunktion und didregisterRemoteNotification Funktion

// [START refresh_token] 
- (void)tokenRefreshNotification:(NSNotification *)notification { 
    // Note that this callback will be fired everytime a new token is generated, including the first 
    // time. So if you need to retrieve the token as soon as it is available this is where that 
    // should be done. 
    NSString *refreshedToken = [[FIRInstanceID instanceID] token]; 
    NSLog(@"InstanceID token: %@", refreshedToken); 

    // Connect to FCM since connection may have failed when attempted before having a token. 
    [self connectToFcm]; 

    // TODO: If necessary send token to application server. 
} 
// [END refresh_token] 

// [START connect_to_fcm] 
- (void)connectToFcm { 
    [[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error) { 
     if (error != nil) { 
      NSLog(@"Unable to connect to FCM. %@", error); 
     } else { 
      NSLog(@"Connected to FCM."); 
     } 
    }]; 
} 
// [END connect_to_fcm] 

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken { 
    NSString *sanitizedDeviceToken = [[[[deviceToken description] 
             stringByReplacingOccurrencesOfString:@"<" withString:@""] 
             stringByReplacingOccurrencesOfString:@">" withString:@""] 
             stringByReplacingOccurrencesOfString:@" " withString:@""]; 

    DLog(@"sanitized device token: %@", sanitizedDeviceToken); 
    [PushNotificationManager API_registerAPNSToken:sanitizedDeviceToken 
             onCompletion:^(BOOL success, NSError *error){}]; 

    [[FIRInstanceID instanceID]setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeSandbox]; 

} 

Aktualisiert: Ich habe diese Funktion enthalten aber immer noch nicht

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{ 
    if (userInfo[@"gcm.message_id"] != nil) { 
     NSLog(@"Should get here FCM?"); 
     [[FIRMessaging messaging]appDidReceiveMessage:userInfo]; 
    }else { 
     NSLog(@"test fetchCompletion handler: %@",userInfo.description); 
    } 

} 
+0

jemand eine ideale haben? –

+0

Sie sollten nicht Swizzling verwenden https://github.com/onmyway133/blog/issues/64 – onmyway133

Antwort

0

Sie einen Blick von dieser Antwort nehmen? Scheint, wie Sie die Linie FIRMessaging.messaging().connect(){.... verpasst

https://stackoverflow.com/a/40881754/1005570

und auch die Delegatfunktion ..

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], 
      fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { 
// If you are receiving a notification message while your app is in the background, 
// this callback will not be fired till the user taps on the notification launching the application. 
// TODO: Handle data of notification 

// Print message ID. 
print("Message ID: \(userInfo["gcm.message_id"]!)") 

// Print full message. 
print(userInfo) 

FIRMessaging.messaging().appDidReceiveMessage(userInfo) 

}

+0

Entschuldigung, der Code ist in Swift. aber ich denke du kannst es bekommen. –

+0

Hallo Update der Code, den ich bereits mit FCM verbinden und kann die Benachrichtigung erhalten, aber meine Frage verwendet die Methode Swizzling? handle FCM Benachrichtigung und meine eigene Serverbenachrichtigung getrennt (jetzt geht alles an die Willensanwesenheit und wurde wieder aufgenommen) –

+0

Und Testen für IOS 10 –

Verwandte Themen