$cordovaPushV5 not firing on(‘connection)’ event

This was my latest project which was an cordova project using the Ionic framework and I had to integrate push notifications into it. To do this I used the $cordovaPushV5 plugin provided by ngCordova. The older PushPlugin is now deprecated.

The plugin is easy enough to setup and use as this part is well documented on the ngCordova page itself. However, I had some issues when it came to handling the onNotification event. It just wasn’t firing. I found out by going through the known issues and the documentation in github that for the plugin code written in the application to be executed the payload of the notification HAS TO include a field, among other data, called ‘content-available’  and this field should be set to ‘1’.here.

Once this is set to one then the on notification event occurs without a problem. If you still seem to have a problem with the event firing, such as,

  • It works the first time you run the app but doesn’t work after you logout and close,
  • I doesn’t work after you resume
  • It doesn’t work after closing and opening

The reason is this, you have to make sure that the even is bound to onNotification every time. To do this every time you login make sure you run the below code which binds the event. P.S, This is not my code, it is copied directly from the ngCordova website. I have just added a few comments.

module.run(function($http, $cordovaPushV5) {

  var options = {
    android: {
      senderID: "12345679"
    },
    ios: {
      alert: "true",
      badge: "true",
      sound: "true"
    },
    windows: {}
  };

  // initialize
  $cordovaPushV5.initialize(options).then(function() {
    // start listening for new notifications
    //This is the most important part, without this the plugin code will not
    //bind the even hanler you have written
    $cordovaPushV5.onNotification();
    // start listening for errors
    $cordovaPushV5.onError();

    // register to get registrationId
    $cordovaPushV5.register().then(function(data) {
      // `data.registrationId` save it somewhere;
    })
  });

  // triggered every time notification received
  $rootScope.$on('$cordovaPushV5:notificationReceived', function(event, data){
    //this is where the notification handling should happen
    // data.message,
    // data.title,
    // data.count,
    // data.sound,
    // data.image,
    // data.additionalData
  });

  // triggered every time error occurs
  $rootScope.$on('$cordovaPushV5:errorOcurred', function(event, e){
    // e.message
  });

});

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s