Urban Airship for notifications


#1

any working code examples for integrating urban airship for push notifications


#2

you could always checkout http://urbanairship.com/resources/developer-resources for said examples like https://github.com/urbanairship/phonegap-ua-push/tree/master/Example it shouldn’t be too hard to transform that to Angular + Ionic

Maby someone here has a working example though


#3

I’ve used Urban Airship and PushWoosh, but in my opinion, Pushwosh is a bit simpler to integrate than urban airship. Plus they have a cordova 3.X compatible plugin so you can install it via the command line. There’s plenty of integration tutorials on the site about integrating for both iOS and Android.


#4

yep i agree pushwosh is easier to apply but annoying to have to pay 39 euro / mo for just testing the remote api while Urban Airship offers 1 million push messages via api for free per month


#5

I am working on app now which I decided to go with UrbanAirship, I had some problems at first to getting everything to work, but then it’s working, the only disadvantage I see that i am relaying on a third party service.

Below is some code to handle the push notifications inside $ionicPlatform.ready(), but I still need to refactor this code into a service/provider but don’t have the time too. If you have any suggestion regarding the refactoring into a service/provider please let me know as I am new to AngularJs.

Note: Some code depends on custom services e.g Notifications

// Monitor Settings Toggle for changes.
    $scope.$watch('pushNotification.checked', function(checked){

      console.log('PushNotification: Change value to ' + checked);

      // Enable/Disable on server according to value.
      checked ? PushNotification.enablePush() : PushNotification.disablePush();

    });

    // If iOS 
    if( $ionicPlatform.is("iOS") ) {
      
      // setAutobadge to true
      PushNotification.setAutobadgeEnabled(true);

    }

    // If Andorid 
    if( $ionicPlatform.is("android") ) {

      PushNotification.setVibrateEnabled(true);
      PushNotification.setSoundEnabled(true);

    }

    // Update the interface with the current UA settings
    PushNotification.isPushEnabled(function(isEnabled) {

      // Revert the one and zero to true and false.
      $scope.pushNotification.checked = isEnabled ? true : false;

    });

    // Incoming message callback
    var handleIncomingPush = function(event) {
      if(event.message) {
        
        // timeout 0 for iOS
        $timeout(function(){
          // Alert with message
          Notification.alert(event.message);
          console.log(event);

          // if url available navigate to
          // @todo test
          if(event.extras.url){
            console.log(event.extras.url);
            $state.go('calendarbyId', {eventId: event.extras.url});
          }
        }, 0);

      } else {
        $timeout(function(){
          console.log("No incoming message");
        }, 0)
      }
    }

    // Registration callback
    var onRegistration = function(event)  {
      if (event.error) {
          Notification.alert('There was an error registering for push notifications.');
          console.log(event.error);
      } else {
          // Notification.alert("Registered with ID: " + event.pushID);
          console.log("Registered with ID: " + event.pushID);
      }
    }

    // Register for any urban airship events
    document.addEventListener("urbanairship.registration", onRegistration, false)
    document.addEventListener("urbanairship.push", handleIncomingPush, false)
    
    // Handle resume
    document.addEventListener("resume", function() {
      console.log("Device resume!", new Date())
      
      PushNotification.resetBadge()
      PushNotification.getIncoming(handleIncomingPush)

      // Reregister for urbanairship events if they were removed in pause event
      document.addEventListener("urbanairship.registration", onRegistration, false)
      document.addEventListener("urbanairship.push", handleIncomingPush, false)
    }, false)


    // Handle resume
    document.addEventListener("active", function() {
      console.log("Device active!", new Date())
      
      PushNotification.resetBadge()
      PushNotification.getIncoming(handleIncomingPush)

      // Reregister for urbanairship events if they were removed in pause event
      document.addEventListener("urbanairship.registration", onRegistration, false)
      document.addEventListener("urbanairship.push", handleIncomingPush, false)
    }, false)

    // Handle pause
    document.addEventListener("pause", function() {
      console.log("Device pause!", new Date())
     
      // Remove urbanairship events.  Important on android to not receive push in the background.
      document.removeEventListener("urbanairship.registration", onRegistration, false)
      document.removeEventListener("urbanairship.push", handleIncomingPush, false)
    }, false)

    // Handle pause
    document.addEventListener("resign", function() {
      console.log("Device resign!", new Date())
     
      // Remove urbanairship events.  Important on android to not receive push in the background.
      document.removeEventListener("urbanairship.registration", onRegistration, false)
      document.removeEventListener("urbanairship.push", handleIncomingPush, false)
    }, false)

    // Register for notification types
    PushNotification.registerForNotificationTypes(PushNotification.notificationType.badge | 
      PushNotification.notificationType.sound | 
      PushNotification.notificationType.alert)
    
    // Get any incoming push from device ready open
    PushNotification.getIncoming(handleIncomingPush)


#6

tnx for the code…i was having problems with registering device tokens.
do you have any idea on how to do it on the mobile device? tnx


#8

You have to test on a real device and make sure of the provisioning profiles that supports push notifications.


#9

I was wondering if you had any more code to share for this example, to provide context? I have been trying to get UA working with ionic, with no luck - the UA example app (included with their cordova plugin) works perfectly first time, but despite everything I’ve tried in ionic I can’t get the android push registration to happen…