ApplicationReceivedRemoteMessage runs only in the foreground

My project uses Firebase notifications as an APNs service, but I use the Firebase Console to send notifications to my device as a test, and they only appear on the screen (via console output) in the foreground. When the application is in the background or the device is on the lock screen, no notifications are received on the device. However, the console output finally comes from the applicationReceivedRemoteMessage method when I open the application.

func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) { print("%@", remoteMessage.appData) print("QQQQQ") } 

Result:

 %@ [AnyHashable("notification"): { body = Hi; e = 1; }, AnyHashable("from"): 492525072004, AnyHashable("collapse_key"): org.myApp] QQQQQ 
+5
source share
1 answer

Problem with iOS and data messages. It is indicated here in which

In iOS, FCM saves a message and delivers it only when the application is in the foreground and has established an FCM connection.

Thus, MUST work. Something similar to mine:

Send 2 push notifications:

1) Normal to wake up a user's phone / initiate when the application is in the 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 be launched when the user opens the application

 { "to" : "/topics/yourTopicName", "data" : { "yourData" : "1", "someMoreOfYourData" : "This is somehow the only workaround I've come up with." } } 

and thus, by the method - (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage process 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 up to you what will it be and how will the // app respond when it comes to foreground. } 

I also left this code to trigger a notification inside the application (create a local notification), because you can use it to create a banner with blocking, which is probably why the user receives the notification again even when the application comes to the forefront:

 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 launch a notification at the very moment when the application appears in the foreground.

0
source

Source: https://habr.com/ru/post/1258979/


All Articles