简体   繁体   中英

Unable to send silent push notifications to iOS using FCM

I'm attempting to send a silent data notification to an iOS device using node.js module firebase-admin 7.0.0 and the following code:

// load lib
var firebase = require('firebase-admin');

// add API keys
firebase.initializeApp({
    credential: firebase.credential.cert('./certs/firebase-private-key.json')
});

// device token send back from device
var token = '00397db1aa2f1fa625f71deef0c8e8ef0d4427cf60a13c0fb4bd290dcec41f4b';

// data message to send (contains no notification details so should be silent)
var message = {
   data: {
      key1: 'hello',
      key2: 'world'
   }
};

var options = {
    priority: 'high',
    timeToLive: 60 * 60 * 24,
    contentAvailable: true,
    mutableContent: true
};

// send the message
firebase.messaging().sendToDevice(token, message, options).then(function (res) {
    // it worked!
    console.log(res);
})
.catch(function (err) {
    // it failed :(
    console.log(err);
});

I get a response saying the message was sent, but it never arrives on the device. Whereas if I send the message using NWPusher it works fine (example payload below) :

{"aps":{ "content-available": 1,"badge":0, "hello": "there" }}

Any ideas?

FYI: I have also opened a ticket on GitHub

Your code looks correct . You may try multiple combination of true and false in mutable content and content available. And you may also try not sending mutableContent fields. But as far as I remember both should be true.

Other way to debug is to check what json is going out from nodejs. Youcan try same payload in NWPusher or PostMan. This will give you some clue.

I have tried out the following payload in postman, for me it's working fine

{"mutable_content":true,"content_available" : true,"priority":"high","registration_ids":["d3-fa7XDQQ4:APA91bEKXbT3HAv6ko9RukcxaB4QBr8zOIT06CBpj9o0Sl6TxsMpTnAMQ86t14foIcuQuDUyzMApbvDORELYyD0WOX3BcfP7ZNtWx9uY0JGm2B7cmdlPoOFs68fe6rz3Q7tq_8Ib7Y4H"]}

For silent notifications you need to make sure couple of things:

1- Your payload doesn't have alert key in your payload

2- Have content-available" : 1 in your payload

3- Background mode for Push Notification is enabled ( select your target > capabilities > Background Modes > Remote Notification)

sample payload:

{
    "aps" : {
        "content-available" : 1
    },
    "acme1" : "bar",
    "acme2" : 42
}

for more information checkout apple's reference : Pushing Updates to Your App Silently

For anyone still having this issue, as Gokul mentioned the code is just fine

Just make sure you are following the steps in the Apple docs

Specially make sure that you are adding the correct callback to your AppDelegate: application(_:didReceiveRemoteNotification:fetchCompletionHandler:)

Note that it is not the same as the one used to handle regular notifications application(_:didReceiveRemoteNotification:)

Also make sure that Remote Notifications is turned on under Background modes in the Project Settings as described here :

在此处输入图片说明

This is PHP version for sent silent for ios. Please add 'content-available' => 1 for silent notificication

 $body = array(
   'content-available' => 1,
   'sound' => ''
 ); 

{
  "aps" : {
    "alert" : "Notification with custom payload!",
    "badge" : 1,
    "content-available" : 1
  },
  "data" :{
    "title" : "Game Request",
    "body" : "Bob wants to play poker",
    "action-loc-key" : "PLAY"
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM