我正在使用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中。因此在保留pushNotification时会收到警报。

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

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

关于ios - 在前台时不显示通知,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37609715/

10-09 16:33