通过Firebase生成推送通知

我试图从我的REST服务生成通知。 缺点是不发送,虽然fcm.googleapis.com/fcm/send反应是成功的。 这个我以两种方式做,首先由模块http:

var http = require('http'); var options = { 'hostname': 'fcm.googleapis.com', 'path': '/fcm/send', 'method': 'POST', 'headers': { 'Authorization': 'key=<Key Server>', 'Content-Type': "application/json" } }; var data = { 'to':tokenPush, 'notification':notification }; var requestHttp = http.request(options, function(res){ res.setEncoding('utf8'); res.on('data', function (chunk) { console.log(chunk); }); }); requestHttp.on('error', function(e) { console.log('problem with request: ' + e.message); }); requestHttp.end(JSON.stringify(data)); 

另一种方式是通过shell的命令:

 var exec = require('child_process').exec; var cmd = 'curl -X POST --header "Authorization: key=<Key Server>" '; cmd += '--Header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send '; cmd +='-d \'{"to":"<Token Client>","notification":{"title":"Validación","body":"'+(new Date()).getTime()+'","sound":"default"}}\''; console.log("====================================="); console.log(cmd); console.log("====================================="); exec(cmd, function(error, stdout, stderr) { if (error) { console.log("====================================="); console.error('exec error:'+error); } console.log("====================================="); console.log('stdout: '+stdout); console.log('stderr: '+stderr); }); 

在这两种情况下,答案是:

{“Multicast_id”:“success”:1,“failure”:0,“canonical_ids”:0,“results”:[{“message_ID”:“”}]}。

当我使用“node”命令从单独的文件运行shell时,shell的代码正常工作。

我可能错了什么?

使用一个fcm包装。 我已经使用'fcm-node'链接: https : //www.npmjs.com/package/fcm-node

只需使用npm install fcm-node --save

用法

 var FCM = require('fcm-node'); var serverKey = ''; var fcm = new FCM(serverKey); var message = { //this may vary according to the message type (single recipient, multicast, topic, et cetera) to: 'registration_token', collapse_key: 'your_collapse_key', 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); } }); 

你的代码不干净。

这应该工作。 将CLIENT_PUSH_IDYOUR_AUTH_KEYreplace为真实

 var http = require('http'); var message = { "to": "CLIENT_PUSH_ID", "notification": { "title": "Validación", "body": (new Date()).getTime(), "sound": "default" } }; var postData = JSON.stringify(message); var options = { hostname: 'fcm.googleapis.com', path: '/fcm/send', method: 'POST', headers: { 'Content-Length': postData.length, 'Content-Type': 'application/json; charset=UTF-8', 'Authorization': 'key=YOU_AUTH_KEY' } }; var requestHttp = http.request(options, function (res) { res.setEncoding('utf8'); res.on('data', function (chunk) { console.log(chunk); }); res.on('error', function (e) { console.log('error:' + e.message); }); }); requestHttp.write(postData); requestHttp.end(); requestHttp.on('error', function (e) { console.log(e); });