3

This is what my log looks like when my push notification gets called on火力地堡雲消息推送通知送交設備

我目前正在創建推送通知設置爲用戶用戶設置的iPhone。我目前正在使用Firebase,所以很自然地轉向使用Firebase Cloud Messaging來完成此項任務。這是我部署到我的Firebase的功能中的設置。有沒有什麼我在這裏做錯了會導致通知沒有被髮送到設備?我感謝任何幫助,如果有更多所需的信息,我會很樂意提供。

const functions = require('firebase-functions'); 

const admin = require('firebase-admin'); 
admin.initializeApp(functions.config().firebase); 

// Listens for new messages added to messages/:pushId 
exports.pushNotification =  functions.database.ref('/messages/{pushId}').onWrite(event => { 

console.log('Push notification event triggered'); 

// Grab the current value of what was written to the Realtime Database. 
var valueObject = event.data.val(); 
console.log(valueObject) 

if(valueObject.photoUrl != null) { 
    valueObject.photoUrl= "Sent you a photo!"; 
} 

// Create a notification 
const payload = { 
    notification: { 
     title:valueObject.toId, 
     body: valueObject.text || valueObject.photoUrl, 
     sound: "default" 
    }, 
}; 

//Create an options object that contains the time to live for the notification and the priority 
const options = { 
    priority: "high", 
    timeToLive: 60 * 60 * 24 
}; 
return admin.messaging().sendToTopic("pushNotifications", payload, options); 
if(!data.changed()){ 

}); 

exports.pushNotification = functions.database.ref('/messages/{pushId}').onWrite(event => { 
const data = event.data; 
console.log('Push notification event triggered'); 
return; 
} 



}); 
+0

你上傳了firebase雲消息部分的p12嗎? –

+0

嘿謝謝你的回覆!我不太確定p12是什麼? @JigneshMayani – oneQuestionAsker

+0

您是否在迴應中發生錯誤? –

回答

-1

發送此jsone在registration_ids字段中輸入您之後的參數,你必須張貼您的所有設備令牌的數組,你要發送推送通知 這是POST請求的方法體

{ "registration_ids" : [Send Array of Device Token], 
    "data" : 
    { 
    "image_url" : "send your image here" 
    "message" : "Send your message here" }, 
"notification" : 
    { 
    "title" : "APP Name", 
    "sound" : "default", 
    "priority" : "high" 
    } 
} 

這裏是帖子的網址

https://fcm.googleapis.com/fcm/send

和發送key="Your Authorization key"在要求外商投資企業的HTTPHeader雲通訊的基本設置形式LD

採取參考這裏

https://firebase.google.com/docs/cloud-messaging/ios/client

+0

在OP的情況下,他明確使用Firebase Admin模塊發出請求(通過admin.messaging())。我不會按照你在這裏的建議發出HTTP請求。您引用的鏈接僅引用Swift和Objective C,OP的問題與JS庫有關。僅僅因爲這些原因,我就低估了你的答案。 – AlxVallejo

0

我注意到,您兩次暴露相同的功能。這也是一個問題。此外,我建議你promisify admin.messaging,以便您可以處理和檢查錯誤。

let topic = "pushNotifications"; 
admin.messaging().sendToTopic(topic, payload, options) 
     .then(function(response) { 

      console.log("Successfully sent message:", response); 
      console.log("Topic: " + topic); 
      res.status(200).send("success"); 
     }) 
     .catch(function(error) { 
      console.log("Error sending message:", error); 
      res.status(500).send("failure"); 
     }); 
相關問題