【问题标题】:Push Notifications With Firebase, Node.js, and Google Cloud Messaging for iOS app使用 Firebase、Node.js 和 Google Cloud Messaging for iOS 应用推送通知
【发布时间】:2016-06-24 14:15:25
【问题描述】:

我正在尝试使用 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);
      }
    });
}

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

{ multicast_id: 7398179070839209000,
  success: 1,
  failure: 0,
  canonical_ids: 0,
  results: [ { message_id: '0:1457564370785913%939a5f18939a5f18' } ] }

可能会发生什么?看起来事情应该可以正常工作,但事实并非如此

【问题讨论】:

  • 您可以在您的消息中添加priority: 'high' 吗?我们最近遇到了一个问题,需要向 iOS 设备发送通知(除非该应用已经处于活动状态)。

标签: node.js push-notification firebase google-cloud-messaging


【解决方案1】:

默认情况下,消息以标准优先级发送,根据Google Cloud Messaging docs

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

不知何故,这个正常的优先级似乎是affect iOS more than Android apps

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

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

【讨论】:

  • 今天 GCM 发送了两次推送。当我取出"priority: 'high'" 时,它开始表现正常。似乎priority 键有时表现得很奇怪
猜你喜欢
  • 1970-01-01
  • 2022-12-14
  • 1970-01-01
  • 2018-09-11
  • 2016-11-21
  • 2015-12-20
  • 1970-01-01
  • 2017-09-12
  • 1970-01-01
相关资源
最近更新 更多