5

my project uses Firebase Notifications as its APNs service, but I have been using the Firebase Console to send notifications to my device as a test, and they only show up (through Console Output) in the foreground. When the app is in the background or the device is in the lock screen, no notifications come to the device. Console output, however, does finally arrive from the applicationReceivedRemoteMessage method when I open the app back up.

func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) {

        print("%@", remoteMessage.appData)
        print("QQQQQ")
    }

Example Output:

%@ [AnyHashable("notification"): {
    body = Hi;
    e = 1;
}, AnyHashable("from"): 492525072004, AnyHashable("collapse_key"): org.myApp]
QQQQQ
Ryan Cocuzzo
  • 3,109
  • 7
  • 35
  • 64
  • Possible duplicate of [Not receiving APNs when out of app](http://stackoverflow.com/questions/40295475/not-receiving-apns-when-out-of-app) – Chris Oct 29 '16 at 19:54
  • The question that you referenced me to has no answers and is phrased differently. – Ryan Cocuzzo Oct 30 '16 at 21:07

1 Answers1

0

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.

Nikola Markovic
  • 301
  • 1
  • 13