使用Firebase,Node.js和Google Cloud Messaging for iOS应用推送通知

Phi*_*her 0 push-notification node.js firebase google-cloud-messaging

我正在尝试使用Firebase,Node.js和Google Cloud Messaging来发送iOS应用的推送通知.下面是Node.js代码

var Firebase = require('firebase'),
    gcm = require('node-gcm')
    ;
var pushRef = new Firebase("https://<myapp>.firebaseio.com/ios/queue/tasks");

pushRef.on("child_added", function(snapshot) {

    console.log("child added event registers");

    var notificationData = snapshot.val();
    sendNotification(notificationData);
    snapshot.ref().remove();

});

function sendNotification(notificationData) {

    var message = new gcm.Message({
      notification: {
        title: "Title",
        body: notificationData.message
      }
    });

    var sender = new gcm.Sender(<my Google Server Key>);
    var registrationTokens = [notificationData.recipientId];

    console.log("about to send message");
    console.log("this is the registration token: " + registrationTokens);

    sender.send(message, { registrationTokens: registrationTokens }, 10, function (err, response) {
      if(err) {
        console.error(err);
      } else {
        console.log(response);
      }
    });
}
Run Code Online (Sandbox Code Playgroud)

没有错误,并且所有console.log语句都注册.但是,我从未在设备上收到推送通知.这是console.log(响应):

{ multicast_id: 7398179070839209000,
  success: 1,
  failure: 0,
  canonical_ids: 0,
  results: [ { message_id: '0:1457564370785913%939a5f18939a5f18' } ] }
Run Code Online (Sandbox Code Playgroud)

可能会发生什么?看起来事情应该有效,但事实并非如此

Fra*_*len 5

默认情况下,会根据Google Cloud Messaging文档以标准优先级发送邮件:

这是邮件传递的默认优先级.普通优先级消息不会打开睡眠设备上的网络连接,并且可能会延迟它们的传送以节省电池电量.对于时间敏感度较低的消息,例如新电子邮件或其他要同步的数据的通知,请选择正常的传送优先级.

不知何故,这个普通优先级似乎比Android应用程序更能影响iOS.

要立即发送消息,您需要添加priority: 'high'消息:

var message = new gcm.Message({
  priority : "high",
  notification: {
    title: "Title",
    body: notificationData.message
  }
});
Run Code Online (Sandbox Code Playgroud)