我正在尝试使用node.js构建pushnotification dever,我使用了" node-fcm",但它与我不起作用,soii尝试使用" node-gcm",但是我遇到了同样的问题,我不知道如何向所有用户发送通知?我需要在字段中写的内容(to:(??
这是我的代码:
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:password@my_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,成功:0,失败:1,canonical_ids:0,结果:[{error:'invalIdRegrigistration'}]}}错误:收件人键'registrationTokens'作为不正确类型提供。出口代码0
完成错误过程
注意:我使用离子2,我可以从https://console.firebase.google.com/。
解决了问题,实际上我没有找到向所有用户发送通知的解决方案,因此我使用了Android应用中的主题: 在我的离子应用中,我将主题选项添加到Android选项中,例如:
const options: PushOptions = {
android: {
topics:['A123'],
senderID: "55*********5"
}
,对于服务器,我使用了此存储库
最后,我将此代码写入index.js文件:
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:Password@my_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);
}
});
这是所有步骤,我希望它会帮助您
,如果要使用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)
}
})