natsumiyu
natsumiyu

Reputation: 3257

Notification doesn't show when in foreground

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]

Upvotes: 0

Views: 722

Answers (1)

Nitin Gohel
Nitin Gohel

Reputation: 49720

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

Upvotes: 1

Related Questions