在前台时不显示通知

Notification doesn't show when in foreground

我正在使用 Firebase 控制台发送通知。在后台时我收到通知,但在前台时我没有收到任何通知。在文档中,据说实现 AppDelegate application:didReceiveRemoteNotification: 所以我添加了它,但仍然不起作用

这是我的代码

// [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]

当您在前台时,您需要设置 UIAlertViewController 并在 didReceiveRemoteNotification 中呈现 This。所以当你保留 pushNotification 时你会得到提醒。

因此,根据 JSON userInfo 的有效负载,您需要执行以下代码

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