2017-05-29 158 views
0

我試圖使用node.js構建PushNotification服務器, 我使用了「node-fcm」,但它不適用於我,所以我嘗試使用「node-gcm」,但我面臨同樣的問題,我不知道如何向所有用戶發送notifcation?我需要在外地寫字(給:)?node-gcm:發送所有設備的PushNotification

這是我的代碼:

var gcm = require('node-gcm'); 
var Sender_ID = '55*****'; 
var API_KEY = 'my server key'; 
var sender = new gcm.Sender(API_KEY,{'proxy':'http://username:[email protected]_proxyinternet.com:8080' , timeout: 5000}); 
var message = new gcm.Message({ 
    notification: { 
     title: "Hello, World", 
     icon: "ic_launcher", 
     body: "This is a notification that will be displayed." 
    } 
}); 

var registrationTokens = []; 
registrationTokens.push(['All']); 
sender.send(message, { registrationTokens: 'All' }, function (err, response) { 
    if (err) console.error(err + ' ERROR'); 
    else console.log(response + ' ELSE'); 
}); 

結果是:

{ multicast_id: -1, success: 0, failure: 1, canonical_ids: 0, results: [ { error: 'InvalidRegistration' } ] } Error: Recipient key 'registrationTokens' was provided as an incorrect type. ERROR Process finished with exit code 0

注:我用離子2,我可以從https://console.firebase.google.com/接收notifcation。

回答

1

的問題解決了,其實我沒有找到解決辦法通知發送到所有用戶,所以我用的主題在Android應用程序是這樣的: 在我的應用程序離子我添加主題選項的Android選項,如:

const options: PushOptions = { 
    android: { 
    topics:['A123'], 
    senderID: "55*********5" 
    } 

和服務器I在我寫這篇文章的代碼index.js文件結束時使用此repositery

var gcm = require('./lib/node-gcm'); 
 

 
var message = new gcm.Message(); 
 
message.addNotification({ 
 
    title: 'Alert!!!', 
 
    body: 'Abnormal data access', 
 
    icon: 'drawable-hdpi-icon', 
 
    image: 'drawable-hdpi-icon', 
 
    alert: 'true', 
 
    sound: 'true' 
 
}); 
 
//Add your mobile device registration tokens here 
 
RETRY_COUNT = 4; 
 
var regTokens = 'AAAAgXm-v**:***************************************************EaH'; 
 

 
var sender = new gcm.Sender(regTokens,{'proxy':'http://Username:[email protected]_proxy.com:8080' , timeout: 5000}); 
 

 
sender.send(message, { topic: "/topics/A123" }, RETRY_COUNT, function (err, response) {  
 
    if(err) { 
 
     console.error(err); 
 
    } else { 
 
     console.log(response); 
 
    } 
 
});

這是所有步驟,我希望它會幫助你

0

,如果你想使用FCM-PUSH;這是真實的實施例:

var FCM = require('fcm-push') 
 

 
var SERVER_API='AAA*****************************jEaH';//put your api key here 
 
var fcm = new FCM(SERVER_API) 
 
var message = { 
 
    to: "/topics/A123", 
 
    //collapse_key: '55', 
 
    priority: 'high', 
 
    content_available: true, 
 
    notification: { 
 
     title: 'Title of your push notification', 
 
     body: 'Body of your push notification' 
 
    }, 
 
    data: { //you can send only notification or only data(or include both) 
 
     my_key: 'my value', 
 
     my_another_key: 'my another value' 
 
    } 
 
} 
 

 
fcm.send(message, function(err, response){ 
 
    if (err) { 
 
     console.log("Something has gone wrong!") 
 
    } else { 
 
     console.log("Successfully sent with response: ", response) 
 
    } 
 
})

相關問題