The community forums are in BETA and closed to the public.

Sending push notifications from javascript server side

Hi

I am working in wakanda, A full stack javascript environment. I need to send data from the server side, so I am using a little node.js script to serve as a gateway between my main app and the world. This script run a node.js version of pubnub.

So basically what I do is:

/**
 * Wakanda server side
 * -------------------
 */
pubnub.publish( {
    channel: 'nodejs_gateway',
    message: {
        title: title,
        regid: regid,
        channel: channel,
        say: say,
        event: event,
        OS: OS,
        dataToTransfer: dataToTransfer || {}
    }
} );

/**
 * Node.js script
 * --------------
 */
var pubnub = require( 'pubnub' )( {
    ssl: true,
    publish_key: 'xxx',
    subscribe_key: 'yyy'
} );

pubnub.subscribe( {
    channel: 'nodejs_gateway',
    callback: function( message ) {
        console.log( ' :: ', message );

        var OS = message.OS;
        var regid = message.regid;
        var channel = message.channel;
        var dataToTransfer = message.dataToTransfer;
        var gwtype = ( OS === 'ios' ) ? 'apns' : 'gcm';

        pubnub.mobile_gw_provision( {
            device_id: regid,
            channel: channel,
            op: 'add',
            gw_type: gwtype,
            error: function( msg ) {
                console.log( msg );
            },
            callback: function() {
                console.info( ' :: ', 'Sending now...' );
                // ...
                message.publish();
            }
        } );
    }
} );

console.log( 'NOTIFIER STARTED' );

The issue is that this workaround is unstable, Sometimes works (notifications are delivered and arrives to my phone), sometimes not. Any suggestion is welcome. Thanks.-


1 person has this question

What are you using to receive the message on your phone? Phonegap, a browser?

Phonegap

Hye Igor,

It seems like your end goal is to send phone notifications. Why are you using a gateway to do this? You should be able to call the following code from your NodeJS backend:

var OS = message.OS;
        var regid = message.regid;
        var channel = message.channel;
        var dataToTransfer = message.dataToTransfer;
        var gwtype = ( OS === 'ios' ) ? 'apns' : 'gcm';

        pubnub.mobile_gw_provision( {
            device_id: regid,
            channel: channel,
            op: 'add',
            gw_type: gwtype,
            error: function( msg ) {
                console.log( msg );
            },
            callback: function() {
                console.info( ' :: ', 'Sending now...' );
                // ...
                message.publish();
            }
        } );

Hi, sorry the delay in response. I do this b/c my main app is not running over node.js it run over wakanda, A full stack javascript environment (http://www.wakanda.org/)


Comments to this discussion are now closed!