2017-04-18 116 views
0

我試圖從我的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設備上的通知。我似乎無法讓我的NodeJSFirebase Admin應用程序正常工作。感謝任何幫助。

更新:我還想指出,即使使用訂閱Firebase主題的Swift代碼,指定的主題也不會顯示在Firebase Web控制檯上。我知道在Firebase Web控制檯上顯示需要花費數小時,但從我運行代碼將我的iOS設備訂閱到Firebase主題後已經過去了一個星期。

我也嘗試改變我的NodeJS代碼發送到單個設備,而不是發送到主題,並且工作。這只是發送到主題不起作用。我已將範圍縮小至send to topic。難道是我的Swift代碼沒有成功地將我的iOS設備訂閱到指定的主題?但這是我從Firebase文檔中獲得的確切片段。

+0

你實現所有需要註冊的遠程通知和接收主題的消息器件代碼? https://firebase.google.com/docs/notifications/ios/console-topics –

+0

@PatNeedham是的。我可以驗證這一點,因爲我可以從Firebase Web控制檯發出推送通知,併成功地在我的iOS設備上接收通知。我似乎無法接收來自我的Node應用程序的通知 – aresz

回答

1

想通了。事實證明,調用viewDidLoad()中的訂閱代碼是一個壞主意,因爲該應用程序可能尚未在該時間點完全連接到FCM和APN。所以我將這些代碼重新放置在我的AppDelegate中的didRegisterForRemoteNotificationsWithDeviceToken中。

現在它工作。

我從這個計算器問題線程的想法:Cannot receive push notification on iOS from Firebase 3.2.0 topics

希望這有助於人誰遇到這個問題