Philip Sopher Philip Sopher - 6 months ago 473
Node.js Question

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

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

Answer

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