![](/img/trans.png)
[英]Twilio Conversations push notifications with Firebase Cloud Messaging
[英]How to send push notifications to multiple devices using Firebase Cloud Messaging
我找到了一種從我的expressJS服務器向我的離子應用程序發送推送消息的方法,我找到了GCM 。 使用GCM,我可以通過令牌列表傳遞消息,如下所示:
sender.send(message, {
registrationTokens: deviceTokens
}, function (err, response) {
if (err) console.error(err);
else console.log('response' + JSON.stringify(response));
});
但是當我發現GCM成為FCM時,我試圖使用FCM做同樣的事情,但直到現在還沒有運氣。 我聽說過發送主題,但我找不到一個例子。
任何人都可以舉例說明如何使用FCM發送主題消息?
我的FCM代碼:(只使用1個令牌)
var FCM = require('fcm-node');
var serverKey = 'xxx';
var fcm = new FCM(serverKey);
var message = {
to: 'device-token',
notification: {
title: event.title,
body: event.information
}
};
fcm.send(message, function (err, response) {
if (err) {
console.log("Something has gone wrong! \n" + err);
} else {
console.log("Successfully sent with response: \n ", JSON.stringify(response));
}
});
我估計您正在使用fcm推送庫來進行推送通知,如果要向多個用戶發送相同的通知,則使用“registration_ids”參數而不是“to”。 此標記接受字符串數組。
例如:registration_ids:[“registrationkey1”,“registrationkey2”]。
注意:限制一次是100鍵。
我認為谷歌很好地記錄了它。 基本上,有兩種方法可以向多個組發送通知:
主題消息 :您讓客戶端訂閱特定主題,然后在發送通知時,您只需修改請求以定位特定主題。 訂閱該主題的所有客戶端都將收到該消息。
POST request to this end point. https://fcm.googleapis.com/fcm/send Content-Type:application/json Authorization:key=SERVER_AUTHORIZATION_KEY { "to": "/topics/foo-bar", "data": { "message": "This is a Firebase Cloud Messaging Topic Message!" } }
您如何訂閱特定主題取決於設備上下文。 我提供的鏈接中提到了Android和IOS的文檔。
設備組 :這基本上建立在您已經擁有要定位的設備的注冊令牌的方法之上。 您可以像這樣組建一個設備組:
POST request https://android.googleapis.com/gcm/notification Content-Type:application/json Authorization:key=API_KEY project_id:SENDER_ID { "operation": "create", "notification_key_name": "appUser-Chris", "registration_ids": ["4", "8", "15", "16", "23", "42"] }
以下請求返回notification_key
,您可以在to
字段中使用該按鈕發送通知。 是的,您必須在某處保存此notification_key
並使用它,如:
POST request
https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=SERVER_AUTHORIZATION_KEY
{
"to": "aUniqueKey", //This is your notification_key
"data": {
"hello": "This is a Firebase Cloud Messaging Device Group Message!",
}
}
當然,您可以添加和刪除組中的設備以及所有其他精細控制。 就像我提到的那樣,它都記錄得很好,應該讓你在沒有打嗝的情況下開始。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.