我正在使用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]
答案 0 :(得分:1)
当您在前台时,需要设置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];
}
}