1

I'm Implementing FCM PUSH notification But It is Working on only Foreground not Working in Background.

In Info.plist file already set Required background modes -> App downloads content in response to push notifications

AppDeleagte.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [FIRApp configure];
    [FIRMessaging messaging].shouldEstablishDirectChannel = YES;
    [FIRMessaging messaging].delegate = self;
    if(@available(iOS 10, *)) {
        [UNUserNotificationCenter currentNotificationCenter].delegate = self;
        UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert |
        UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
        [[UNUserNotificationCenter currentNotificationCenter]
         requestAuthorizationWithOptions:authOptions
         completionHandler:^(BOOL granted, NSError * _Nullable error) {
             // ...
         }];
    }
    else {
        UIUserNotificationType allNotificationTypes =
        (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
        [application registerUserNotificationSettings:settings];
    }
    [application registerForRemoteNotifications];
    return YES;
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    NSLog(@"%@", userInfo);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    NSLog(@"%@", userInfo);
    completionHandler(UIBackgroundFetchResultNewData);
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    [FIRMessaging messaging].APNSToken = deviceToken;
}

- (void)messaging:(FIRMessaging *)messaging didReceiveRegistrationToken:(NSString *)fcmToken {
    NSLog(@"FCM registration token: %@", fcmToken);
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
    // Print message ID.
    NSDictionary *userInfo = notification.request.content.userInfo;
    NSLog(@"Message ID: %@", userInfo[@"gcm.message_id"]);
    // Print full message.
    completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionSound);
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void (^)(void))completionHandler {
    NSLog(@"%@", response.notification.request.content);
    completionHandler();
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Bhaumik Surani
  • 1,730
  • 3
  • 19
  • 40
  • see this once may be help with you https://stackoverflow.com/questions/49423251/ios-fcm-push-notifications-not-working-in-background-state-in-swift – Anbu.Karthik Jun 11 '18 at 09:52
  • thank you @Anbu.karthik for notification send **{"notification": { "body": "Enter your message", "sound": "default" } }** payload – Bhaumik Surani Jun 11 '18 at 10:35
  • take a look [here](https://stackoverflow.com/a/38277476/5175709). Your payload needs `content_available` to be set to `true`. Also see [here](https://stackoverflow.com/questions/42275060/what-is-difference-between-remote-notification-and-silent-notification-in-ios/42302369#42302369). Make sure you've set up your project correctly. If those don't help then edit your answer and mention all the setup you've made – mfaani Jun 11 '18 at 14:53
  • issue is solved – Bhaumik Surani Jun 12 '18 at 04:41

0 Answers0