ios - 无法使用 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 上开了一张票
解决方案
您的代码看起来正确。您可以在可变内容和可用内容中尝试多种真假组合。您也可以尝试不发送 mutableContent 字段。但据我记得,两者都应该是真的。
其他调试方法是检查从 nodejs 输出的 json。您可以在 NWPusher 或 PostMan 中尝试相同的有效负载。这会给你一些线索。
我已经在邮递员中尝试了以下有效负载,对我来说它工作正常
{"mutable_content":true,"content_available" : true,"priority":"high","registration_ids":["d3-fa7XDQQ4:APA91bEKXbT3HAv6ko9RukcxaB4QBr8zOIT06CBpj9o0Sl6TxsMpTnAMQ86t14foIcuQuDUyzMApbvDORELYyD0WOX3BcfP7ZNtWx9uY0JGm2B7cmdlPoOFs68fe6rz3Q7tq_8Ib7Y4H"]}
推荐阅读
- django - 芹菜任务在 Django 框架中不起作用
- java - 使用 ImageIO 保存 javafx WritableImage 不起作用
- php - 带有挂载的 sudo 中的 www-data
- mxgraph - mxGraph .Net 中的分层布局
- java - 将 threadpoolexecutor 传递给 CompletableFuture 的方法有哪些?
- python - 遍历元组列表
- javascript - 关闭 IFrame 时如何使用 JavaScript 取消设置“document.domain”属性?
- javascript - 模态打开关闭后如何查找表格行
- angularjs - Angular - 基于 user_id 显示仪表板和其他 component.html
- python - 使用 shell prefer sed cut 命令和 awk 重命名文件名