0

我正在使用 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]
4

1 回答 1

1

当您在前台时,您需要UIAlertViewControllerdidReceiveRemoteNotification. 因此,您在保留 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];
    }
}
于 2016-06-03T08:49:33.890 回答