Skip to content

Instantly share code, notes, and snippets.

@Alexintosh
Last active February 29, 2016 14:15
Show Gist options
  • Save Alexintosh/826e69d2c667499cb652 to your computer and use it in GitHub Desktop.
Save Alexintosh/826e69d2c667499cb652 to your computer and use it in GitHub Desktop.
//app.js
.run(function($ionicPlatform, $notification, androidConfig, iosConfig) {
$ionicPlatform.ready(function() {
if( ionic.Platform.isWebView() ){
var push = $window.PushNotification.init({
"android": androidConfig,
"ios": iosConfig,
"windows": {}
});
push.on('registration', function(data) {
$notification.setRegId(data.registrationId);
});
push.on('notification', function(data) {
$notification.handle(data);
});
push.on('error', function(e) {
console.log(e);
});
}
});
})
//somewhere else
(function(){
'use strict';
angular
.module('ago')
.factory('$notification', Notification);
function Notification($api, androidConfig, iosConfig, $cordovaPush, $ionicPopup, $log, Utils, Settings, $rootScope){
var service = {
handle: handleNotification,
registerDevice: register,
setRegId: setRegId,
disablePush: disablePush
};
return service;
function setRegId(regId){
if (ionic.Platform.isAndroid()) {
service.ANDROID_regId = regId;
}
else if (ionic.Platform.isIOS()) {
service.IOS_regId = regId;
}
}
function disablePush(){
var deviceID;
if( ionic.Platform.isWebView() ) {
if (ionic.Platform.isAndroid()) {
deviceID = service.ANDROID_regId;
}
else if (ionic.Platform.isIOS()) {
deviceID = service.IOS_regId;
}
$api.disablePushNotifications(deviceID);
}
}
function register(){
if( ionic.Platform.isWebView() && Settings.fetch().notification.enabled ){
if( ionic.Platform.isIOS() ) {
if( !service.IOS_regId ) return false;
$api.enablePushForIOS( service.IOS_regId )
.then(function(res){
$log.debug('Push enabled IOS');
$log.debug( JSON.stringify(res) );
}, function(err){
$log.error( JSON.stringify(err) );
});
} else if (ionic.Platform.isAndroid()) {
$api.enablePushForAndroid(service.ANDROID_regId)
.then(function(res){
$log.debug('Push enabled ANDROID');
$log.debug( JSON.stringify(res) );
}, function(err){
$log.error( JSON.stringify(err) );
});
}
}
}
function showPopup(notification){
var alertPopup = $ionicPopup.show({
template: notification.message,
title: 'New push',
buttons: [
{ text: 'Cancel',
onTap: function(e){
alertPopup.close();
}
},
{
text: '<b>Read</b>',
type: 'button-positive',
onTap: function(e) {
//do something
}
}
]
});
}
function handleAndroid(notification){
if( !notification.additionalData.custom.foreground ) Utils.redirect('/app/alert/'+notification.additionalData.custom.custom.alertId);
else {
Utils.toast("New push");
$rootScope.$broadcast('newAlert');
}
}
function handleIOS(notification) {
if (notification.additionalData.foreground) {
Utils.toast("New push");
$rootScope.$broadcast('newAlert');
} else {
Utils.redirect('/app/alert/'+notification.additionalData.custom.alertId);
}
}
function handleNotification(notification){
if (ionic.Platform.isAndroid()) {
handleAndroid(notification);
}
else if (ionic.Platform.isIOS()) {
handleIOS(notification);
}
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment