Skip to content

Instantly share code, notes, and snippets.

@jbasinger
Last active August 29, 2015 14:24
Show Gist options
  • Save jbasinger/8533c6fe4417990af665 to your computer and use it in GitHub Desktop.
Save jbasinger/8533c6fe4417990af665 to your computer and use it in GitHub Desktop.
angular.module('yourTotallyAwesomeApp')
.run(function($ionicPlatform, $ionicPush, $ionicUser) {
$ionicPlatform.ready(function() {
//A cheat check to make sure we're on a device
if(window.plugins){
$ionicPush.register({
canRunActionsOnWake: true,
onNotification: function(notification) {
if (notification.event){
if(notification.event == "registered"){
//Here you'll want to do something with notification.regid
//It's the deviceToken your server will use to tell Ionic
//whose device this is.
console.log(notification.regid);
}
if(notification.event == "message"){
//Yes you need to access payload.payload.
//I tried changing it to something else server side
//and it didn't work. Sorry =(
console.log(notification.payload.payload);
if(notification.foreground){
//This state happens when the notification comes in and the user
//is in the application.
console.log('Foreground Notification Received!');
} else {
if(notification.coldstart){
//This state happens when the notification is selected by the
//user but the app is not currently running.
console.log('User tapped notification while app was closed!');
} else {
//This state happens when the notification is selected by the
//user but the app has been backgrounded.
console.log('User tapped notification while app was backgrounded!');
}
}
}
}
}
}).then(function(deviceToken) {
var user = $ionicUser.get();
if(!user.user_id) {
user.user_id = device.uuid;
};
$ionicUser.identify(user);
});
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment