0

My application receives silent push notifications... works fine if the app is in foreground, but when moved to background it only receives them for a while (8-10 minutes) and after nothing for about one hour and after again few received (for 8-10 minutes) and again nothing...

I also realised, that calling

FIRMessaging.messaging().appDidReceiveMessage(userInfo)

in background is causing this warning:

FIRMessaging receiving notification in invalid state 2

I am wondering, if these two things can't be linked... Firebase stops sending silent push messages after a while, because they are not confirmed when received in background???

Thanks a lot, Jan

UPDATE:

One part answered by Firebase support. There is no link between this warning and notifications delivery. Confirmation is used only for Analytics, for nothing else. Warning to be corrected, but not linked to my issue.

UPDATE:

I have just found these questions, which are describing the issues with silent push notifications delivery in background:

Silent push notifications only delivered if device is charging and/or app is foreground

didReceiveRemoteNotification:fetchCompletionHandler not being called when app is in background and not connected to Xcode

iOS8.1.2 didReceiveRemoteNotification method not called when device is not plugged in

No obvious solution there... at least I don't see it.

Community
  • 1
  • 1
Jan
  • 3
  • 3

1 Answers1

3

Sounds to me like you've got your app configured correctly for FIRMessaging(), but not for APNs.

Here's the thing: Firebase Messaging is only used for sending data-only messages when your app is in the foreground. By definition, it doesn't work when your app is in the background -- if you're trying to send messages to your app in the background, Firebase Cloud Messaging routes your message through APNs, which is received through normal APNs-y methods, like application(_:didReceiveRemoteNotification:fetchCompletionHandler:).

If you want more information on this topic, maybe I humbly suggest the Debugging Firebase Cloud Messaging on iOS post on the Firebase blog.

Todd Kerpelman
  • 16,875
  • 4
  • 42
  • 40
  • Hi Todd, thanks for your comment. All my messages should be sent via APN, as I used the content_available flag. I can receive the message in foreground as well as in the background, but in the background I only receive them for 8-10 minutes and after it stops. And again restarts in about 1 hour, again works for 8-10 minutes and it stops again. I went through the Firebase debugging post, but I have not found anything I would do differently. – Jan Apr 28 '17 at 20:30
  • Ahhh... then it's possible you might actually be getting throttled by APNs. How often are you making these content-available calls? Also, are you calling the completionHandler when you're done with whatever background processing you need to do? – Todd Kerpelman Apr 28 '17 at 21:48
  • In the current configuration it's about 4 times per minute. Strange is, that when the app is in the foreground, there is no issue... it works perfectly and it's as well delivered by APNS... the problem is in the background... my feeling is that it's more on Firebase side rather than on APNS. CompletionHandler I manage well, on each call. As I wrote in the initial post, I am not able to call FIRMessaging.messaging().appDidReceiveMessage(userInfo) method in background. To be more precise, I am able to call it, but it result in the warning stated above. Strange to me. Thanks. – Jan Apr 29 '17 at 07:41
  • 1
    4 times a minute? You're almost definitely getting throttled. APNs won't necessarily throttle you in the foreground because that's a sign the user is using your app and is okay with it receiving network calls. But if you're forcing it to wake up and do work every 15 seconds in the _background_, it's going to kill the battery and be a bad user experience. A good general guideline is to not send a background message more frequently than 4 times an hour. – Todd Kerpelman May 01 '17 at 16:07
  • Thanks Todd, yep, this could be a reason. But I would expect APNS to answer Firebase with an error core, when asked to deliver it. All messages are accepted successfully. Another thing I found is, that when I have my iPhone connected to Mac and the app is started from Xcode than it works well in background as well (all the night). How this would be linked with APNS? Maybe something with local power management? Delivered because 100% charged and on cable? Very strange, have not seen anything like that anywhere. – Jan May 01 '17 at 21:53
  • 1
    The throttling that happens is being done by the operating system, based on battery state and whether the app is in foreground; it's not APNS doing the throttling — you iPhone _is_ receiving the notification at the OS level, but iOS has placed your app on its naughty list (because you've received too many too quickly) and has decided to not forward the notifications to your application. There is no error issued. – Wes Campaigne Jul 10 '17 at 21:03