通过Firebase生成推送通知

时间:2016-10-26 20:27:31

标签: node.js sails.js firebase-cloud-messaging

我正在尝试从我的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”:“成功”:1,“失败”:0,“canonical_ids”:0,“结果”:[{“message_ID”:“”}}}。

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

我可能错了什么?

2 个答案:

答案 0 :(得分:1)

使用fcm包装器。我使用了“fcm-node&#39;链接:https://www.npmjs.com/package/fcm-node

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

安装即可

<强>用法

&#13;
&#13;
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);
    }
});
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您的代码不干净。

这应该有效。将 CLIENT_PUSH_ID YOUR_AUTH_KEY 替换为真实

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);
});