0

I'm using Firebase Console to send notification. When in background I received notification but when I'm in foreground I doesn't receive any notification. In the documentation, it was said to implement AppDelegate application:didReceiveRemoteNotification: so i added it, but still doesn't work

Here is my code

// [START receive_message]

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

// Print message ID.
NSLog(@"Message ID: %@", userInfo[@"gcm.message_id"]);

// Pring full message.
NSLog(@"%@", userInfo);
}
// [END receive_message]
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
natsumiyu
  • 3,217
  • 7
  • 30
  • 54
  • Perhaps this is a dumb question, but when you say it doesn't work, you mean you're reviewing the app in the device while attached to the Xcode debugger and you're not seeing the NSLog messages in your console, correct? – Todd Kerpelman Jun 03 '16 at 21:52

1 Answers1

1

While you are on foreground that you need to set UIAlertViewController and present This in didReceiveRemoteNotification. so you get alert while you reserved pushNotification.

So based on your JSON payload of userInfo you need to do following code

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

    // Print message ID.
    NSLog(@"Message ID: %@", userInfo[@"gcm.message_id"]);

    // Pring full message.
    NSLog(@"%@", userInfo);

    if (application.applicationState == UIApplicationStateActive)
    {
        UIAlertController *alertController = [UIAlertController  alertControllerWithTitle:[userInfo objectForKey:@"notification.title"]  message:[userInfo objectForKey:@"notification.body"]  preferredStyle:UIAlertControllerStyleAlert];
        [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

        }]];
        [self.window.rootViewController presentViewController:alertController animated:YES completion:nil];
    }
}
Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144