Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase FCM silent push notifications for iOS

Tags:

I have a problem with silent notifications on iOS.

When my application is in background, I don't receive silent notification sent by FCM. But if I try to send directly to APNS, the notification is successfully received.

This is the JSON sent to FCM:

{  "to" : "<token>", "priority": "high", "content_available": true, "data" : {   "<key>" : "<string>",   "<key2>" : "<string>" } 

}

This is the JSON sent directly to APNS:

{   "aps": {     "content-available": 1   },   "<key>": "<string>",   "<key>": "<string>" } 

I have already tried to remove the "priority" key because I saw someone saying that I shouldn't set the priority if the "content_available" is already set. It didn't work.

  1. I have "Push Notifications" enabled in XCode > Capabilities.
  2. I have "Remote notifications" checked in Background Modes in XCode > Capabilities.
  3. The FCM notifications are working fine when app is in foreground and sometimes when the app is in background.
like image 952
vbgd Avatar asked Nov 25 '16 14:11

vbgd


People also ask

How do I send a FCM silent notification?

@spacitron fcm is firebase cloud messaging, so he does ;) do not send body , title , link , inside the notification otherwise user will get visible notifications . if you want to send totally silent notification then add some custom parameter in notification or keep it blank .

Can I use Firebase for push notification to iOS?

For Apple client apps, you can receive notification and data payloads up to 4000 bytes over the Firebase Cloud Messaging APNs interface. To write your client code in Objective-C or Swift, we recommend that you use the FIRMessaging API.

What is silent push notification in iOS?

Silent push notifications are simply notifications that mobile app users receive without any pings, alerts, or interruptions to the user. They arrive on the mobile device and sit in the notification tray until read or dismissed.

Will iOS awake my app when I receive silent push notification?

I am new to iOS and i struggled with silent push notification,googled a lot and got stuck. Will iOS awake my app when i receive silent push notification when app is not launched(i.e when app is removed from app switcher). this method is called and works fine when app is in foreground and background.


2 Answers

Remove "notification" key value pair and add "content_available": true

It will look like this

{      "to" : "...",     "priority": "high",     "content_available": true,     "data" : {       ....     } } 

This should make it a silent APNS and you need to handle with corresponding APNS delegate methods.

You will need to handle this through delegates Refer this firebase documentation for details: https://firebase.google.com/docs/cloud-messaging/concept-options

like image 181
Alap Anerao Avatar answered Sep 21 '22 20:09

Alap Anerao


I found an workaround. I put an empty value for "sound" in "notification" field and the silent notifications are delivered even when the application is in background.

{      "to" : "...",     "priority": "high",     "notification": {         "sound": ""     },     "data" : {       ....     } } 

My hunch is that Apple does not allow silent notifications with a 'high' priority and somehow "notification": {"sound": ""} tricks the APNS that this notification is not a silent one.

like image 30
vbgd Avatar answered Sep 21 '22 20:09

vbgd