使用Firebase,Node.js和Google Cloud Messaging for iOS应用推送通知 [英] Push Notifications With Firebase, Node.js, and Google Cloud Messaging for iOS app

查看:162
本文介绍了使用Firebase,Node.js和Google Cloud Messaging for iOS应用推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Firebase,Node.js和Google云消息传递为iOS应用发送推送通知。下面是Node.js代码:

$ p $ var Firebase = require('firebase'),
gcm = require(' node-gcm')
;
var pushRef =新的Firebase(https://< myapp> .firebaseio.com / ios / queue / tasks);
$ b pushRef.on(child_added,function(snapshot){

console.log(child added event registers);

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

});

函数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(即将发送消息);
console.log(这是注册令牌:+ registrationTokens);
$ b sender.send(message,{registrationTokens:registrationTokens},10,function(err,response){
if(err){
console.error(err);
} else {
console.log(response);
}
});



$ b $没有错误,并且所有的console.log语句都被注册了。但是,我从来没有收到我的设备上的推送通知。这里是console.log(响应):

$ $ p $ code多点传送ID:7398179070839209000
成功:1
失败:0,
canonical_ids:0,
results:[{message_id:'0:1457564370785913%939a5f18939a5f18'}]}

可能会发生什么?它看起来像事情应该工作,但它们不是

解决方案

通过默认消息发送与标准的优先级,根据 Google Cloud Messaging文档


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

优先级似乎影响iOS比Android应用程序



要立即获得消息,您需要为您添加 priority:'high'消息:

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


I am trying to use Firebase, Node.js, and Google Cloud Messaging to send push notifications for an iOS app. Below is the Node.js code

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

There are no errors, and all of the console.log statements register. However, I never receive a push notification on my device. Here is the console.log(response):

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

What might be going on? It looks like things should be working, but they aren't

解决方案

By default messages are sent with standard priority, which according to Google Cloud Messaging docs:

This is the default priority for message delivery. Normal priority messages won't open network connections on a sleeping device, and their delivery may be delayed to conserve battery. For less time-sensitive messages, such as notifications of new email or other data to sync, choose normal delivery priority.

Somehow this normal priority seems to affect iOS more than Android apps.

To get the messages delivered immediately, you'll want to add priority: 'high' to you message:

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

这篇关于使用Firebase,Node.js和Google Cloud Messaging for iOS应用推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆