我試圖從我的NodeJS
服務器發送推送通知到我的iOS
設備,但我無法讓它工作。我正在做Send to topic
方法。我試過Firebase Admin
(NodeJS)文檔中的代碼示例,但它似乎不起作用。從我的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);
});
}
這裏是我的銀行代碼訂閱話題:
override func viewDidLoad() {
super.viewDidLoad()
FIRMessaging.messaging().subscribe(toTopic: "/topics/someTopic")
}
當我跑我的NodeJS應用程序,我得到的Successfully sent message:
日誌,所以我不確定爲什麼我沒有收到iOS設備上的推送通知。
是的,我已經爲開發和生產設置了必要的APN
證書。我可以從Firebase Cloud Messaging
網絡控制檯發出推送通知,併成功接收我的iOS
設備上的通知。我似乎無法讓我的NodeJS
Firebase Admin
應用程序正常工作。感謝任何幫助。
更新:我還想指出,即使使用訂閱Firebase主題的Swift代碼,指定的主題也不會顯示在Firebase Web控制檯上。我知道在Firebase Web控制檯上顯示需要花費數小時,但從我運行代碼將我的iOS設備訂閱到Firebase主題後已經過去了一個星期。
我也嘗試改變我的NodeJS代碼發送到單個設備,而不是發送到主題,並且工作。這只是發送到主題不起作用。我已將範圍縮小至send to topic
。難道是我的Swift代碼沒有成功地將我的iOS設備訂閱到指定的主題?但這是我從Firebase文檔中獲得的確切片段。
你實現所有需要註冊的遠程通知和接收主題的消息器件代碼? https://firebase.google.com/docs/notifications/ios/console-topics –
@PatNeedham是的。我可以驗證這一點,因爲我可以從Firebase Web控制檯發出推送通知,併成功地在我的iOS設備上接收通知。我似乎無法接收來自我的Node應用程序的通知 – aresz