GCM 推送服务端(nodejs)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37263637/article/details/82898599

昨天接入苹果APNs推送,今天需要支持GCM端推送,推送服务器都需要在墙外,否者无法连接到GCM服务器,而接受推送服务器的手机app需要有谷歌服务框架,最好待推送手机也在墙外。

同样万能的npm也提供了gcm推送模块。

推送代码:

var gcm = require('node-gcm');
 
var sender = new gcm.Sender('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
//GCM 服务器凭证 在FCM后台应用获取 凭证

// Prepare a message to be sent
var message = new gcm.Message({
    data: { //透传消息
        alert: '{"msgID":1,"time":"2018-09-28 13:29:16","msgParam":""}',//透传消息消息体
    },
    notification: { //通知
        title: "Hello, World",//推送标题
        body: "This is a notification that will be displayed if your app is in the background."//推送内容
    }
});
 
var regTokens = ['xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx']; 
//GCM regID
// Actually send the message
sender.send(message, {registrationTokens: regTokens}, function (err, response) {
    if (err) console.error(err);
    else console.log(response);
});

猜你喜欢

转载自blog.csdn.net/m0_37263637/article/details/82898599
GCM