Firebase FCM silent push notifications for iOS

30,221

Solution 1

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

Solution 2

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.

Solution 3

I was working on Firebase silent push notification using nodejs. When I tried below code its was working fine. When I was adding "priority": "high" and "content_available": true it was giving below error.

Worked below code

const admin = require('firebase-admin');
const serviceAccount ="...."; //service account path
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount)
});

let  fcmToken = "...."; // Your token
let message ={
    "token": fcmToken,
    "data": {
        "updateApi": "activity"
    }
} 

admin.messaging().send(message)
  .then((response) =>{
    console.log('Successfully sent notification:', response);
})
  .catch((error) =>{
    console.log('Error while sending notification:', error);
});

Error when I added the priority and content_available in message object

{ code: 'messaging/invalid-argument',
     message: 'Invalid JSON payload received. Unknown name "priority" at \'message\': Cannot find field.\nInvalid JSON payload received. Unknown name "content_available" at \'message\': Cannot find field.' },
  codePrefix: 'messaging' }

Solution 4

If you are using a cloud function, I found a lot of the information, even in the official documentation was out of date, or different from the typescript interfaces.

This finally worked for me. Rather than set alert: "", just leave it out, it's optional. For custom properties, you can add to message.apns.payload. The fcmToken should be the user's token from their device.

  const message: TokenMessage = {
    token: fcmToken,
    apns: {
      headers: {
        "apns-priority": "5",
      },
      payload: {
        aps: {
          contentAvailable: true,
        },
        my_custom_parameter: true,
      },
    },
  };

  admin
    .messaging()
    .send(message)
    .then(() => {
      // do something.
    })
    .catch((error) => {
      functions.logger.error(
        "Error sending push notification: " + error.toString()
      );
      // Do something.
    });
};
Share:
30,221

Related videos on Youtube

vbgd
Author by

vbgd

Updated on July 06, 2021

Comments

  • vbgd
    vbgd almost 3 years

    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.
  • vbgd
    vbgd over 7 years
    But I don't want the user to receive a notification in the Notification Center. I just want a silent notification that will execute some code in the "didReceiveRemoteNotification". That's what "silent notification" means.
  • Dinesh Raja
    Dinesh Raja over 7 years
    Right after you set the notification object, it becomes a normal push notification. It's not a silent push notification at al.
  • Moxarth
    Moxarth over 6 years
    what to do to send the silent notification through FCM message console ?
  • Priy Ranjan
    Priy Ranjan over 5 years
    Yes, Just don't pass priority and content_available in above message object
  • Shubham1164
    Shubham1164 over 5 years
    Then how to control the values of these. Where I can change priority and content_available values?
  • Shanu Singh
    Shanu Singh about 4 years
    @vladiulianbogdan any luck?