在前台时不显示通知
Posted
技术标签:
【中文标题】在前台时不显示通知【英文标题】:Notification doesn't show when in foreground 【发布时间】:2016-06-03 08:37:36 【问题描述】:我正在使用 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]
【问题讨论】:
也许这是一个愚蠢的问题,但是当您说它不起作用时,您的意思是您正在查看设备中的应用程序,同时连接到 Xcode 调试器并且您没有看到 NSLog控制台中的消息,对吗? 【参考方案1】:当您在前台时,您需要设置 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];
【讨论】:
它只显示OK
按钮。我怎样才能得到我的title
和body
的通知?以上是关于在前台时不显示通知的主要内容,如果未能解决你的问题,请参考以下文章
Flutter Firebase如何控制通知在应用程序处于前台时不显示
android 如何实现后台时用通知栏显示有新的消息,当在前台时不显示通知