当应用在后台运行时,Google Firebase远程通知不会popup

我正在使用Google Firebase向用户发送通知。 当时我试图发送通知单个设备(我的)。

遇到接收通知问题 – 而我的应用程序运行在后台横幅不会出现。 但是,如果我打开我的应用程序,方法didReceiveRemoteNotification:触发我的警报视图:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:userInfo[@"notification"][@"body"] message:@"More info..." delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Open",nil]; [alert show]; } 

但是,由于它是用Google Firebase文档编写的,因此在打开应用程序后会触发此方法,所以这是有道理的。

所以消息被传递到我的设备,我只是不能触发通知横幅,如果应用程序在后台。

我读了有关设置消息优先级为高和自定义数据密钥的内容 – 可用1,但没有运气。

我是否在代码中缺less其他内容来触发通知? 我已经使用Google Firebase指南来实现通知。

我解决了我的问题。 我开始再次阅读Google Firebase上的文档,并在Cloud Messaging下发现了这一点:

在setAPNSToken:type:中提供您的APNs令牌和令牌types。 确保正确设置types的值:沙箱环境的FIRInstanceIDAPNSTokenTypeSandbox或生产环境的FIRInstanceIDAPNSTokenTypeProd。 如果您没有设置正确的types,则邮件不会传送到您的应用程序。

所以我错过了把这个声明放在方法:didRegisterForRemoteNotificationsWithDeviceToken:

  - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { [FIRInstanceID instanceID] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeSandbox]; } 

不要忘记把types:“FIRInstanceIDAPNSTokenTypeProd”用于生产。