0

對於通過我的應用程序發送FCM上游消息我跟着這個教程Sending Notifications with FirebaseFCM的Node.js服務器問題的Android

我的工作代碼如下:

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

var API_KEY = "my api key"; // Your Firebase Cloud Messaging Server API key 

// Fetch the service account key JSON file contents 
//var serviceAccount = require("serviceAccountKey.json"); 

// Initialize the app with a service account, granting admin privileges 
//firebase.initializeApp({ 
// credential: firebase.credential.cert(serviceAccount), 
// databaseURL: "https://-----.firebaseio.com" 
//}); 

exports.listenForNotificationRequests = functions.database.ref('/notificationRequests') 
    .onWrite(event => { 
     const request = event.data.val(); 
       sendNotificationToUser(
        request.username, 
        request.message, 
        function() { 
         console.error("success"); 
        } 
       ); 
    }); 

function sendNotificationToUser(username, message, onSuccess) { 
    request({ 
    url: 'https://fcm.googleapis.com/fcm/send', 
    method: 'POST', 
    headers: { 
     'Content-Type' :' application/json', 
     'Authorization': 'key='+API_KEY 
    }, 
    body: JSON.stringify({ 
     notification: { 
     title: message, //title of notification 
     body: message 
     }, 
     to : usertoken //a static registered user token 
    }) 
    }, function(error, response, body) { 
    if (error) { console.error(error); } 
    else if (response.statusCode >= 400) { 
     console.error('HTTP Error: '+response.statusCode+' - '+response.statusMessage); 
    } 
    else { 
     onSuccess(); 
    } 
    }); 
} 

在運行這段代碼,我在接受RemoteMessageMyFirebaseMessagingServiceonMessageReceived,但此消息返回空通知或數據。所以通知不會出現在應用程序中。這裏的任何機構能幫助我做什麼我錯了嗎?

admin.messaging()工作!

+1

你已經在使用火力管理員SDK所以爲什麼要去通過在調用admin.messaging()。sendToDevice()時從頭構建請求的麻煩? https://firebase.google.com/docs/cloud-messaging/admin/send-messages –

+0

@FranciscoMateo感謝您的回覆。我做了很多搜索,發送設備到firbase的設備消息,但從未回覆你的建議。我一定會爲此而更新我的問題。 –

回答

0

這是正在運行的代碼我的模塊中

var request = require('request'); 
var sendMsgToGroup = function(deviceId, message) { 
    request({ 
     url: 'https://fcm.googleapis.com/fcm/send', 
     method: 'POST', 
     headers: { 
      'Content-Type': ' application/json', 
      'Authorization': 'key=<YOUR API KEY>' 
     }, 
     body: JSON.stringify({ 
      notification: { 
       title: message, 
       body: 'Testing Notificaiton' 
      }, 
      "to": deviceId 
     }) 
    }, function(error, response, body) { 
     if (error) 
      console.log(error); 
     else if (response.statusCode >= 400) 
      console.log("HTTP Error" + response.statusCode + "-" + 
         response.statusCode + "\n" + body); 
     else 
      console.log(body); 
     }); 
    }; 

module.exports.sendMessageToGroup = sendMsgToGroup; 

你可以調用這個函數像

var FCM = require('./FCM/fcm_config.js'); 
FCM.sendMessageToGroup("<Device Token Number>", 'testing'); 

Reference Link