ios - 在前台时不显示通知

标签 ios firebase firebase-cloud-messaging

我正在使用 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 时你会得到提醒。

因此,根据 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/

相关文章:

ios - 从网址加载图片,图片的网址不以.jpg等结尾

android - 与 Firebase 安装服务器 API 通信时出错 - 403 Forbidden

objective-c - 点击时返回标签的标签

ios - MBProgressHUD UIViewController 动画不一致

android - 我如何在 Google Firebase 数据库中使用过滤器和子元素

javascript - 使用 Firebase 身份验证时为 "This domain is not authorized"

javascript - 通过 firebase 云消息从 Android 应用程序向 Web 应用程序发送通知

Firebase notification_receive 未显示在 Firebase 分析中的事件中

iphone - 如何显示从 YouTube 加载内容?

Firebase 客户端错误 : Custom token corresponds to a different audience