push notifications

Node js:

This is an easy basic implementation  about apple push notification by using node-js .

please get the apple certificate as well as the password from ios developer.

please create the following files

push_lib.js

var join = require(‘path’).join, pfx = join(__dirname, ‘Certificates.p12‘);

/*!
* Create a new gateway agent
*/

var apnagent = require(‘apnagent’), agent = module.exports = new apnagent.Agent();

/*!
* Configure agent
*/

agent.set(‘pfx file’, pfx).set(“passphrase”,’password‘).enable(‘sandbox’);

/*!
* Error Mitigation
*/

agent.on(‘message:error’, function (err, msg) {
switch (err.name) {
// This error occurs when Apple reports an issue parsing the message.
case ‘GatewayNotificationError’:
console.log(‘[message:error] GatewayNotificationError: %s’, err.message);

// The err.code is the number that Apple reports.
// Example: 8 means the token supplied is invalid or not subscribed
// to notifications for your application.
if (err.code === 8) {
console.log(‘ > %s’, msg.device().toString());
// In production you should flag this token as invalid and not
// send any futher messages to it until you confirm validity
}

break;

// This happens when apnagent has a problem encoding the message for transfer
case ‘SerializationError’:
console.log(‘[message:error] SerializationError: %s’, err.message);
break;

// unlikely, but could occur if trying to send over a dead socket
default:
console.log(‘[message:error] other error: %s’, err.message);
break;
}
});

/*!
* Make the connection
*/

agent.connect(function (err) {
// gracefully handle auth problems
if (err && err.name === ‘GatewayAuthorizationError’) {
console.log(‘Authentication Error: %s’, err.message);
process.exit(1);
}

// handle any other err (not likely)
else if (err) {
throw err;
}

// it worked!
var env = agent.enabled(‘sandbox’)
? ‘sandbox’
: ‘production’;

console.log(‘apnagent [%s] gateway connected’, env);
});

 

push_test.js

var agent = require(‘./push_lib’);
/*!
* Send message
*/
module.exports = “yourdevice token”;
var x=module.exports;
agent.createMessage()
.device(x)
.alert(‘Custom variables’)
.set(‘id_1’, 12345)
.set(‘id_2’, ‘abcdef’)
.send();

 

Source url :https://blog.engineyard.com/2013/developing-ios-push-notifications-nodejs

 

 

 

Laravel 5:

This is an easy basic implementation  about apple push notification by laravel 5 .

please get the apple certificate as well as the password from ios developer.

Run the follwoing command in order to geenrate pem file:

openssl pkcs12 -in Certificates.p12 -out my_private_key.pem -nodes

create a function :

function testPushNotification(){
PushNotification::app(‘appNameIOS’)
->to(‘c9bccca836e2ddeb153b3205f937cc212dd2fab295dd3f21807289ba33cd2970’)
->send(‘Hello World, i`m a push message from Audiochimp’);
}

 

riute.php

Route::get(‘/push’, ‘ClientController@testPushNotification’);

when we run baseurl/push we can get the notifcation

 

source url :https://github.com/davibennun/laravel-push-notification

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s