Google 节点 js 中的云消息传递

Google Cloud Messaging in Node js

将 GCM 用于基于节点的应用程序需要遵循哪些程序? 服务器端(Node js)和客户端(Android/iOS)是否需要特定代码?

  1. Android 设备应向服务器发送设备令牌才能接收通知。这里an example怎么做。
  2. 获取 gcm key 从服务器发送推送。
  3. 使用 node-gcm 包从 npm 和 node.js 应用发送推送。

基本示例:

const gcm = require('node-gcm'); //Google Cloud Messaging
const gcmKey = ''; // Your gcm key in quotes
const deviceToken = ''; // Receiver device token
const sender = new gcm.Sender(gcmKey);

var message = new gcm.Message();

message.addData({
  title: 'Push',
  body: 'This is push notification',
  otherProperty: true,
});

sender.send(message, {registrationIds: [token]}, (err) => {
  if (err) {
    console.error(err);
  }
  else {
    console.log('Sent');
  }
});