问题
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