There is a problem with iOS and data messages. It states here that
On iOS, FCM stores the message and delivers it only when the app is in
the foreground and has established a FCM connection.
So there MUST be a work around. Something similar to mine:
Send 2 push notifications:
1) Normal to wake the users phone / initiate while the app is in background using this code:
{
"to" : "/topics/yourTopicName",
"notification" : {
"priority" : "Normal",
"body" : "Notification Body like: Hey! There something new in the app!",
"title" : "Your App Title (for example)",
"sound" : "Default",
"icon" : "thisIsOptional"
}
}
2) Data notification that will trigger when user opens the app
{
"to" : "/topics/yourTopicName",
"data" : {
"yourData" : "1",
"someMoreOfYourData" : "This is somehow the only workaround I've come up with."
}
}
and, so, under the - (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage
method handle your data:
- (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage {
// Print full message
NSLog(@"%@", remoteMessage.appData);
//
//*** ABOUT remoteMessage.appData ***//
// remoteMessage.appData is a Key:Value dictionary
// (data you sent with second/data notification)
// so it's up to you what will it be and how will the
// app respond when it comes to foreground.
}
I will also leave this code to trigger the notification inside the app (create local notification), because you can use it to create a silenced banner, maybe, so the user gets notified again even when the app comes to foreground:
NSDictionary *userInfo = remoteMessage.appData;
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.userInfo = userInfo;
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.alertBody = userInfo[@"yourBodyKey"];
localNotification.alertTitle = userInfo[@"yourTitleKey"];
localNotification.fireDate = [NSDate date];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
It will trigger notification same second the app comes in foreground.