首页 > 解决方案 > 无法使用 FCM 向 iOS 发送静默推送通知

问题描述

我正在尝试使用 node.js 模块firebase-admin 7.0.0和以下代码向 iOS 设备发送静默数据通知:

// 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);
});

我收到回复说消息已发送,但它从未到达设备上。而如果我使用NWPusher发送消息,它可以正常工作(下面的示例有效负载)

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

有任何想法吗?

仅供参考:我还在GitHub 上开了一张票

标签: iosfirebaseapple-push-notificationsfirebase-cloud-messaging

解决方案


您的代码看起来正确。您可以在可变内容和可用内容中尝试多种真假组合。您也可以尝试不发送 mutableContent 字段。但据我记得,两者都应该是真的。

其他调试方法是检查从 nodejs 输出的 json。您可以在 NWPusher 或 PostMan 中尝试相同的有效负载。这会给你一些线索。

我已经在邮递员中尝试了以下有效负载,对我来说它工作正常

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

推荐阅读