无法使用FCM向iOS发送静默推送通知

问题描述 投票:1回答:3

我正在尝试使用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开了一张票

ios firebase apple-push-notifications firebase-cloud-messaging
3个回答
2
投票

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

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

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

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

0
投票

对于无声通知,您需要确保以下几点:

1-您的有效负载在您的有效负载中没有alert密钥

2-在你的有效载荷中有content-available" : 1

3-启用推送通知的后台模式(选择目标>功能>后台模式>远程通知)

样本负载:

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

了解更多信息checkout apple的参考:Pushing Updates to Your App Silently


0
投票

这是用于ios发送静默的PHP版本。请添加'content-available'=> 1以进行静音通知

 $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"
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.