1

我試圖使用node.js向Firebase中的主題發送iOS推送通知。我遵循this tutorial,但無法弄清楚爲什麼通知不會將其發送給訂閱該主題的設備。我可以從控制檯發送主題消息,並且listenForNotificationRequests()正在成功刪除notificationRequest孩子。使用Firebase的iOS推送通知主題消息未將其發送到設備

這裏是通知請求的在火力地堡結構:

example notification request

這裏是node.js的代碼與密鑰/移除的網址:

var firebase = require('firebase-admin'); 
var request = require('request'); 

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

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

// Initialize the app with a service account, granting admin privileges 
firebase.initializeApp({ 
    credential: firebase.credential.cert(serviceAccount), 
    databaseURL: "URL_REMOVED" 
}); 
ref = firebase.database().ref(); 

function listenForNotificationRequests() { 
    var requests = ref.child("notificationRequests"); 
    requests.on("child_added", function(requestSnapshot) { 
    var request = requestSnapshot.val(); 
    sendNotificationToUser(
     request.username, 
     request.message, 
     function() { 
     requestSnapshot.ref.remove(); 
     } 
    ); 
    }, function(error) { 
    console.error(error); 
    }); 
}; 

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({ 
     to : "/topics/user_"+username, 
     priority : "high", 
     notification: { 
     title: message 
     } 
    }) 
    }, function(error, response, body) { 
    if (error) { console.error(error); } 
    else if (response.statusCode >= 400) { 
     console.error('HTTP Error: '+response.statusCode+' - '+response.statusMessage); 
    } 
    else { 
     onSuccess(); 
    } 
    }); 
} 

// start listening 
listenForNotificationRequests(); 

任何幫助/建議是大大不勝感激!

編輯:

這裏是我如何訂閱該主題在斯威夫特:

FIRMessaging.messaging().subscribe(toTopic: "user_\(currentUserID!)")

+0

代碼的NodeJS看起來還好。你能不能把你的客戶端代碼加入訂閱?您在發送信息後是否收到成功回覆? –

+0

'application/json'中有空格嗎? –

+0

@AL。爲主題訂閱添加了客戶端代碼。我已經確認訂閱正在通過控制檯成功發送和接收主題消息進行工作。我正在將node.js應用程序部署到Google Cloud App Engine。仍試圖找出如何在那裏調試。那是我應該關注的地方嗎? –

回答

3

對於任何人誰碰到這個運行時,我發現這個問題。我必須將「文本」添加到通知有效內容中,該通知有效內容填充了通知的主體。本教程僅包含「標題」,它是可選的,並且本身不夠...至少對於我閱讀過的APNS而言。

感謝那些回答!

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({ 
 
     "priority" : "high", 
 
     "notification" : { 
 
     "title": sender, 
 
     "text": message 
 
     }, 
 
     "to" : "/topics/user_"+username 
 
    }) 
 
    }, function(error, response, body) { 
 
    if (error) { console.error(error); } 
 
    else if (response.statusCode >= 400) { 
 
     console.error('HTTP Error: '+response.statusCode+' - '+response.statusMessage); 
 
    } 
 
    else { 
 
     onSuccess(); 
 
    } 
 
    }); 
 
}

相關問題