0

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.

MOzeb
  • 423
  • 1
  • 6
  • 14
  • You're actually not allowed to set a `content-available` message to 1 if you're also sending it high-priority. Maybe drop the `content-available` part and see if that helps. – Todd Kerpelman Jul 22 '16 at 15:27
  • Also given that you mention custom data, I'm assuming that you are sending from the Firebase console. The console does not support the content_available field, custom data is treated just as key/value pairs available in the data object of the message. Did you add the "Required background modes"? – Arthur Thompson Jul 22 '16 at 21:25
  • @ToddKerpelman okay, I removed content-available but still no luck.. still receiving message, but notification not showing when app is in background – MOzeb Jul 25 '16 at 07:47
  • @ArthurThompso Background modes (remote notifications) are added. – MOzeb Jul 25 '16 at 07:48
  • If you are sending notifications from the Firebase console and your app is in the background the notification should be displayed automatically, not via your code in didReceiveRemoteNotification. Maybe you should post more of your AppDelegate in your question. Also when your app is in the foreground what is the content of the userInfo object? – Arthur Thompson Jul 25 '16 at 15:55

1 Answers1

2

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.

MOzeb
  • 423
  • 1
  • 6
  • 14