Skip to content

Instantly share code, notes, and snippets.

@emilioeduardob
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emilioeduardob/01e4a8fa9088dfd7e4ba to your computer and use it in GitHub Desktop.
Save emilioeduardob/01e4a8fa9088dfd7e4ba to your computer and use it in GitHub Desktop.
AngularJS Service for phonegap PushPlugin
// We depend on ngCordova. app.env is just a helper to get ENV variables
angular.module('starter', ['app.env', 'ngCordova'])
.run(function(PushNotifications) {
ionic.Platform.ready(function() {
// This is just needed for Android GCM, on IOS would be ignored
PushNotifications.setGcmSenderId('XXX');
PushNotifications
.ensureRegistration()
.onMessage(function(message) {
console.log("New push notification", message);
});
});
});
angular.module("utils")
.factory("PushNotifications", function($cordovaPush, $rootScope){
var msgCallback;
var regCallback;
var errorCallback;
var gcmSenderId;
var service = {
setGcmSenderId: setGcmSenderId,
ensureRegistration: ensureRegistration,
getToken: getToken,
onMessage: onMessage
}
return service;
function setToken(token) {
window.localStorage.setItem('pushToken', token);
}
function setGcmSenderId(senderId) {
gcmSenderId = senderId;
}
function getToken() {
return window.localStorage.getItem('pushToken', '');
}
function onMessage(cb) {
msgCallback = cb;
}
// returns an object to the callback with source and token properties
function ensureRegistration(cb, errorCb) {
regCallback = cb;
errorCallback = errorCb;
document.addEventListener("deviceready", function(){
if (ionic.Platform.isAndroid()) {
registerAndroid();
$rootScope.$on('$cordovaPush:notificationReceived', androidPushReceived);
}
if (ionic.Platform.isIOS()) {
registerIOS();
$rootScope.$on('$cordovaPush:notificationReceived', iosPushReceived);
}
});
return this;
}
function registerIOS() {
var config = {
"badge": true,
"sound": true,
"alert": true,
};
$cordovaPush.register(config).then(function(result) {
setToken(result.deviceToken);
if (regCallback !== undefined) {
regCallback({
source: 'ios',
token: result.deviceToken
});
}
}, function(err) {
if (errorCallback !== undefined) {
errorCallback(err);
}
console.log("Registration error on IOS: ", err)
});
}
// Inits the Android registration
// NOTICE: This will not set the token inmediatly, it will come
// on the androidPushReceived
function registerAndroid() {
var config = {
"senderID": gcmSenderId
};
// PushPlugin's telerik only register if necesary or when upgrading app
$cordovaPush.register(config).then(function(result) {
console.log("Registration requested!");
}, function(err) {
console.log("Error registering on Android", err);
});
}
// Process incoming push messages from android
function androidPushReceived(event, notification) {
switch(notification.event) {
case 'registered':
if (notification.regid.length > 0 ) {
setToken(notification.regid);
if (regCallback !== undefined) {
regCallback({
source: 'android',
token: notification.regid
})
}
}
break;
case 'message':
if (msgCallback !== undefined) { msgCallback(notification) }
break;
case 'error':
console.log('GCM error = ' + notification.msg);
break;
default:
console.log('An unknown GCM event has occurred');
break;
};
}
function iosPushReceived(event, notification) {
if (msgCallback !== undefined) { msgCallback(notification) }
}
});
@emilioeduardob
Copy link
Author

Gracias @amosrivera. Efectivamente el PushPlugin hace todo el setup de callbacks cuando llamas a register. Como que no me sonaba algo muy bueno. El plugin original PushPlugin de phonegap va a tratar de registrar el telefono cada vez que parte, pero el fork de telerik ya soluciona esto, y solo se registra si es la primera vez o si se actualizo la app version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment