-
-
Save emilioeduardob/01e4a8fa9088dfd7e4ba to your computer and use it in GitHub Desktop.
// 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) } | |
} | |
}); |
gracias @agustinf! lo actualice
@emilioeduardob @agustinf
Todavía hay un problemirijilla. Para que funcione registrar el callback con $rootScope.$on('$cordovaPush:notificationReceived', iosPushReceived);
tiene que estar seteada la configuración del $cordovaPush
que es con $cordovaPush.register(config)
. Eso solo se está llamando cuando se ejecuta la primera vez porque esta dentro de registerAndroid
. Realmente lo unico que solo se deberia llamar la primera vez es el regCallback()
.
Ayer medio reescribí el asunto para que funcionara, me decís que te parece. Vos reorganizalo a gusto! :)
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
BUG IMPORTANTE: En la linea 112 se está enlazando el evento de recepción de notificación a la función
androidPushReceived
. Sin embargo, como esta llamada está dentro deregisterAndroid()
, solo va a funcionar la primera vez que corremos la aplicación. Los mismo pasa en IOS, claro.