Google Firebase remote notifications not popping up when app is running in background

安稳与你 提交于 2019-12-12 07:03:21

问题


I am using Google Firebase to send notifications to users. At the time I am trying to send notification to single device (mine).

Having a problem with receiving notifications - while my app is running in background banner not appears. But if I open my app, method didReceiveRemoteNotification: triggers my alert view:

- (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];

}

But as it's written in Google Firebase documentation, this method triggers after app opens, so it make sense.

So message is delivered to my device, I just can't trigger a notification banner if app is in background.

I read about setting message priority to High and custom data key content-available to 1, but no luck.

Am I missing something else in my code to trigger notification? I have done this with Google Firebase Guide to implement Notifications.


回答1:


I resolved my problem. I started reading documentation on Google Firebase once again and under Cloud Messaging I found this:

Provide your APNs token and the token type in setAPNSToken:type:. Make sure that the value of type is correctly set: FIRInstanceIDAPNSTokenTypeSandbox for the sandbox environment, or FIRInstanceIDAPNSTokenTypeProd for the production environment. If you don't set the correct type, messages are not delivered to your app.

So I missed to put this statement in method :didRegisterForRemoteNotificationsWithDeviceToken:

 - (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

[FIRInstanceID instanceID] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeSandbox];

}

Don't forget to put type: "FIRInstanceIDAPNSTokenTypeProd" for production.



来源:https://stackoverflow.com/questions/38520645/google-firebase-remote-notifications-not-popping-up-when-app-is-running-in-backg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!