Push Notifications With Firebase, Node.js, and Google Cloud Messaging for iOS app

情到浓时终转凉″ 提交于 2019-12-12 03:30:01

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/35904572/push-notifications-with-firebase-node-js-and-google-cloud-messaging-for-ios-ap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!