【发布时间】:2017-09-14 17:58:18
【问题描述】:
我正在尝试从我的NodeJS 服务器向我的iOS 设备发送推送通知,但我无法让它工作。我正在使用Send to topic 方法。我已经尝试了Firebase Admin (NodeJS) 文档中的代码示例,但它似乎不起作用。
这是我的 NodeJS 代码:
const admin = require('firebase-admin'); // Firebase-admin module to send push notifications via firebase
const serviceAccount = require('./myprivatekey.json'); //privateKey.json file
// setup firebase admin
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: FIREBASE_URL
});
sendMessage('someTopic', 'test message');
function sendMessage(topicName, message) {
// The topic name can be optionally prefixed with "/topics/".
var topic = `/topics/${topicName}`;
console.log(topic);
// See the "Defining the message payload" section below for details
var payload = {
notification: {
title: 'test title',
body: message
}
};
// Send a message to devices subscribed to the provided topic.
admin.messaging().sendToTopic(topic, payload)
.then(function (response) {
// See the MessagingTopicResponse reference documentation for the
// contents of response.
console.log('Successfully sent message:', response);
})
.catch(function (error) {
console.log('Error sending message:', error);
});
}
这是我订阅该主题的 Swift 代码:
override func viewDidLoad() {
super.viewDidLoad()
FIRMessaging.messaging().subscribe(toTopic: "/topics/someTopic")
}
当我运行我的 NodeJS 应用程序时,我收到了 Successfully sent message: 日志,所以我不确定为什么我没有在我的 iOS 设备上收到推送通知。
是的,我已经为开发和生产设置了必要的APN 证书。我能够从Firebase Cloud Messaging Web 控制台发送推送通知,并在我的iOS 设备上成功接收通知。我似乎无法让我的 NodeJS Firebase Admin 应用程序工作。感谢您对此的任何帮助。
更新:我还想指出,即使使用 Swift 代码订阅 firebase 主题,指定的主题也不会出现在 Firebase Web 控制台上。我知道在 Firebase Web 控制台上出现需要几个小时,但自从我运行代码将我的 iOS 设备订阅到 firebase 主题已经有一周了。
我还尝试将我的 NodeJS 代码更改为发送到单个设备而不是发送到主题,这很有效。只是发送到主题不起作用。我已将可能的问题缩小到send to topic。难道是我拥有的 Swift 代码没有成功地将我的 iOS 设备订阅到指定的主题?但这正是我从 Firebase 文档中得到的确切 sn-p。
【问题讨论】:
-
您是否实现了为远程通知和接收主题消息注册设备所需的所有代码? firebase.google.com/docs/notifications/ios/console-topics
-
@PatNeedham 是的。我可以验证这一点,因为我可以从 Firebase Web 控制台发送推送通知,并在我的 iOS 设备上成功接收通知。我似乎无法接收来自我的 Node 应用程序的通知
标签: ios node.js firebase firebase-cloud-messaging