Last active
August 29, 2015 14:22
-
-
Save VincentHelwig/403366bdc4e268786b4e to your computer and use it in GitHub Desktop.
Cordova PushPlugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var app = angular.module('pushApp'); | |
app.run(function(PushFactory) { | |
document.addEventListener('deviceready', function () { | |
PushFactory.init(); | |
}); | |
}); | |
app.factory('PushFactory', ['CONFIG', '$http', function(CONFIG, $http) { | |
'use strict'; | |
var pushNotification; | |
function _iosTokenResponse(result) { | |
_registerDeviceToken({ | |
'user': device.uuid, | |
'type': 'ios', | |
'token': result | |
}); | |
} | |
function _successHandler(result) { | |
// Return OK | |
} | |
app._onNotification = function(e) { | |
switch(e.event) { | |
case 'registered' : | |
if ( e.regid.length > 0 ) { | |
_registerDeviceToken({ | |
'user': device.uuid, | |
'type': 'android', | |
'token': e.regid | |
}); | |
} | |
break; | |
case 'message': | |
// Display message / data | |
console.log(e); | |
break; | |
case 'error': | |
console.log(e); | |
break; | |
default : | |
} | |
} | |
app._onIosNotification = function(event) { | |
if (event.alert) { | |
// Display message / data | |
} | |
if (event.sound) { | |
} | |
if (event.badge) { | |
} | |
} | |
function _erroHandler(error) { | |
// Return error on registration for all os | |
} | |
function _registerDeviceToken(pData) { | |
var _config = { | |
url: CONFIG.SERVER.SUBSCRIBE, | |
method: 'POST', | |
data: pData | |
} | |
var _xhr = $http(_config) | |
.success( function( data ) { console.log(data); }) | |
.error( function(error) { console.log(error); }); | |
} | |
var _init = function() { | |
try { | |
pushNotification = window.plugins.pushNotification; | |
if (pushNotification) { | |
switch (device.platform) { | |
case 'android': | |
case 'Android': | |
case 'amazon-fireos': | |
pushNotification.register( | |
_successHandler, | |
_erroHandler, | |
{ | |
'senderID': CONFIG.ANDROID.PROJECT_ID, | |
'ecb': 'app._onNotification' | |
} | |
); | |
break; | |
default: | |
pushNotification.register( | |
_iosTokenResponse, | |
_erroHandler, | |
{ | |
'badge': CONFIG.IOS.BADGE, | |
'sound': CONFIG.IOS.SOUND, | |
'alert': CONFIG.IOS.ALERT, | |
'ecb': 'app._onIosNotification' | |
} | |
); | |
} | |
} | |
} catch(error) { | |
console.log(error.message); | |
} | |
}; | |
return { | |
init: _init | |
} | |
}]); | |
app.factory('CONFIG', function() { | |
return { | |
SERVER: { | |
SUBSCRIBE: 'http://46.105.27.185:8000/subscribe', | |
}, | |
ANDROID: { | |
PROJECT_ID: '1025424274405' | |
}, | |
IOS: { | |
BADGE: 'true', | |
SOUND: 'true', | |
ALERT: 'true' | |
} | |
}; | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment