Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Node.js not pushing all notifications

Node.js not pushing all notifications

Problem

I just started learning Node.js as I find it the right tool for the job of creating a push notification server.

So I got this dodgy script here I hacked together using some internet tutorials:

// ------------------------------------------------------------
// HTTP Server stack using Node.js
// ------------------------------------------------------------

// import the HTTP module that ships with Node.js 
var http = require("http");
var url = require("url");

// push notification helper modules
var crypto = require("crypto");
var tls = require("tls");
var fs = require("fs");
var stream = null;

process.chdir(__dirname);

//connect(function(){});

function connect(next)
{
    filepath = 'cert_and_key_dev.pem';  
    var certPem = fs.readFileSync(filepath, encoding='ascii');
    var keyPem = fs.readFileSync(filepath, encoding='ascii');
    var options = { key: keyPem, cert: certPem };

    var apnshost_dev = 'gateway.sandbox.push.apple.com';
    var apnsport = 2195;

    stream = tls.connect(apnsport, apnshost_dev, options, function() {
        // connected
        console.log('I am connected to APNS, WOO HOO!');
        //next(!stream.authorized, stream);

        pushTest();
    });
}

function pushTest()
{
    var pushnd = { aps: { alert: 'This is a test' }, customParam: { foo: 'bar' } } // aps is required
    var hextoken = '...my push token here....';

    // construct the protocol data unit
    var payload = JSON.stringify(pushnd);
    var payloadlen = Buffer.byteLength(payload, 'utf-8'); // encoded UTF-8 string length, max 255 bytes
    var tokenlen = 32;
    var buffer = new Buffer(1 + 4 + 4 + 2 + tokenlen + 2 + payloadlen);
    var i = 0;
    buffer[i++] = 1; // command
    var msgid = 0xbeefcace; // message identifier, can be left 0
        buffer[i++] = msgid >> 24 & 0xFF;
    buffer[i++] = msgid >> 16 & 0xFF;
        buffer[i++] = msgid >> 8 & 0xFF;
    buffer[i++] = msgid & 0xFF;

    // expiry in epoch seconds (1 hour)
    var seconds = Math.round(new Date().getTime() / 1000) + 1*60*60; // expire in epoch seconds (1 hour)
    buffer[i++] = seconds >> 24 & 0xFF;
    buffer[i++] = seconds >> 16 & 0xFF;
    buffer[i++] = seconds >> 8 & 0xFF;
    buffer[i++] = seconds & 0xFF;

    buffer[i++] = tokenlen >> 8 & 0xFF; // token length
    buffer[i++] = tokenlen & 0xFF;
    token = hextobin(hextoken);
    token.copy(buffer, i, 0, tokenlen);
    i += tokenlen;

    buffer[i++] = payloadlen >> 8 & 0xFF; // payload length
    buffer[i++] = payloadlen & 0xFF;

    payload = Buffer(payload);
    payload.copy(buffer, i, 0, payloadlen);

    var j = 0;

    // try sending multiple copies of the notification in one stream
    for(var j = 0; j 

In the for loop:

// try sending multiple copies of the notification in one stream
for(var j = 0; j 

I was only able to receive at most 2 notifications out of the 5.

Am I doing it wrong ?

Problem courtesy of: Zhang

Solution

Apple checks on its side for push messages duplication to prevent spam.
Try to change the message content.

Solution courtesy of: lesha

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Node.js not pushing all notifications

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×