1

mein Code Remote-Benachrichtigung und Remote-Nachricht bekommen, das letzte Problem ist, kann nicht remote Nachricht bekommen, wenn App im Hintergrund (nur offene App dann bekam)Ziel C FCM wie Remote-Nachricht erhalten, wenn App im Hintergrund

  • Wenn ich Remote-Benachrichtigung und App im Hintergrund drücken, scheint es, als ob die Benachrichtigung Meldung wie erwarten, klicken Sie auf diese Benachrichtigung, dann bringt es mich zu meiner App. Ich weiß nicht, warum Remotemeldung nicht so ein Verhalten hat (ich benutze Postbote und "https://fcm.googleapis.com/fcm/send", um Fernnachricht zu senden)

    . Plz mir jemand helfen Hier ist mein Code

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
        // Register for remote notifications 
        if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) { 
         // iOS 7.1 or earlier. Disable the deprecation warnings. 
         UIRemoteNotificationType allNotificationTypes = 
         (UIRemoteNotificationTypeSound | 
         UIRemoteNotificationTypeAlert | 
         UIRemoteNotificationTypeBadge); 
         [application registerForRemoteNotificationTypes:allNotificationTypes]; 
    
        } else { 
         // iOS 8 or later 
         // [START register_for_notifications] 
         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 
        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]; 
         } 
    
         [[UIApplication sharedApplication] registerForRemoteNotifications]; 
         // [END register_for_notifications] 
        } 
    
        // [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]; 
    
        return YES; 
    } 
    
    // [START receive_message] 
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { 
        // 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. 
        NSLog(@"didReceiveRemoteNotification1 ID: %@", userInfo[@"gcm.message_id"]); 
    
        // Print full message. 
        NSLog(@"didReceiveRemoteNotification1 %@", userInfo); 
    } 
    
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
    fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { 
        // 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. 
        NSLog(@"didReceiveRemoteNotification2 ID: %@", userInfo[@"gcm.message_id"]); 
    
        // Print full message. 
        NSLog(@"didReceiveRemoteNotification2 %@", userInfo); 
    } 
    - (void)userNotificationCenter:(UNUserNotificationCenter *)center 
         willPresentNotification:(UNNotification *)notification 
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { 
        // Print message ID. 
        NSDictionary *userInfo = notification.request.content.userInfo; 
        NSLog(@"willPresentNotification: %@", userInfo[@"gcm.message_id"]); 
    
        // Print full message. 
        NSLog(@"willPresentNotification %@", userInfo); 
    } 
    
    - (void)userNotificationCenter:(UNUserNotificationCenter *)center 
    didReceiveNotificationResponse:(UNNotificationResponse *)response 
         withCompletionHandler:(void (^)())completionHandler { 
        NSDictionary *userInfo = response.notification.request.content.userInfo; 
        NSLog(@"didReceiveNotificationResponse: %@", userInfo); 
    } 
    
    - (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage { 
        NSLog(@"%@", [remoteMessage appData]); 
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:[NSString stringWithFormat:@"%@", [remoteMessage appData]] delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil]; 
        [alert show]; 
    } 
    - (void)tokenRefreshNotification:(NSNotification *)notification { 
    
        NSString *refreshedToken = [[FIRInstanceID instanceID] token]; 
        NSLog(@"InstanceID token: %@", refreshedToken); 
        [self connectToFcm]; 
    } 
    - (void)connectToFcm { 
        [[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error) { 
         if (error != nil) { 
          NSLog(@"Unable to connect to FCM. %@", error); 
         } else { 
          NSLog(@"Connected to FCM."); 
         } 
        }]; 
    } 
    - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { 
        NSLog(@"Unable to register for remote notifications: %@", error); 
    } 
    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 
        NSLog(@"APNs token retrieved: %@", deviceToken); 
        [[FIRMessaging messaging] setAPNSToken:deviceToken]; 
    } 
    
    - (void)applicationDidBecomeActive:(UIApplication *)application { 
        [self connectToFcm]; 
    } 
    
    - (void)applicationDidEnterBackground:(UIApplication *)application { 
        [[FIRMessaging messaging] disconnect]; 
        NSLog(@"Disconnected from FCM"); 
    } 
    

Antwort

0

Für andere Menschen, die gleiche Problem.

Endlich nach vielen Suchen fand ich die Lösung, sehr einfach, müssen Sie den Inhalt der Firebase App Nachricht folgen, um es durch Push-Benachrichtigungsdienst lesbar zu machen. U sieht here und here

diese Probe ist, um es zu Gerät machen zeigen, wenn im Hintergrundmodus :)

{ 
    "condition": "'condition1' in topics && 'condition2' in topics", 
    "notification": { 
     "category": "notification_category", 
     "title_loc_key": "notification_title", 
     "body_loc_key": "notification_body", 
     "badge": 1 
    }, 
    "data": { 
    "data_type": "notification_data_type", 
    "data_id": "111111", 
    "data_detail": "FOO", 
    "data_detail_body": "BAR" 
    } 
} 

Jetzt arbeitet alles wie erwartet, dann die zweite Aufgabe ist es nur Nachrichteninhalt ändern wie was auch immer Du willst :)

Verwandte Themen