2017-05-29 89 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) 
 
    } 
 
})