Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ngCordova/Ionic Push Notifications when application is in the background

I'm currently building an android application using ionic/ngcordova. I'm at the point of implementing push notifications. I've implemented push notifications as a service which is injected at app.run(function(){..}) stage. The registration part works and I receive a callback containing the regid. Also, when the application is in the active state, the event is raised and the notification is received.

The problem I'm having is that when the application goes into the background, the notifications are not received at all. I would expect that a local notification would be raised when the app isn't running or something similar, but absolutely nothing happens, which is weird.

I've trawled the web for the last couple of days looking for a solution but I've been unable to find anything which kind of indicates to me that it should just work.

The following is my notificationService.js inside my app

app.factory('notificationService', ['$cordovaPush', function($cordovaPush){

  var dataFactory = {};

  //
  // When the device is ready and this service has been plumbed in...
  document.addEventListener("deviceready", function(){

      console.log("initializing push notifications...");

      _register();

  }, false);


  //
  // Registers the device for push notifications...
  var _register = function(){

      var config = {};

      if ( device.platform == 'android' || device.platform == 'Android' || device.platform == "amazon-fireos" ){

          // TODO: centralise this value as it can change...
          config = {
              senderID: "448168747432",
              ecb: "onNotificationGCM"
          };

      }else {
          // iOS
          config = {
              "badge":"true",
              "sound":"true",
              "alert":"true"
          };

          // Can add the following property to the config object to raise a callback with the information if need be...
          // "ecb": "onNotificationRegisterAPN"
      }

      $cordovaPush.register(config).then(function(result){

                    //
                    // Typically returns "ok" for android and devicetoken for iOS
          console.log(result);

      });
  };

    window.onNotificationGCM = function(result){

        console.log(result);

        /*
         I get called when the app is in the foreground, but nothing happens when the app is in the background.
        */

    };

  dataFactory.register = _register;

  return dataFactory;
}]);

If it helps, I'm using PushSharp via a .net application in order to deliver the notifications. Any help would be greatly appreciated.

UPDATE: I'm using the following frameworks/libs:

  • Ionic Framework 1.2.14-beta6
  • Cordova 4.2.0
  • PushPlugin
like image 706
Matthew Merryfull Avatar asked Feb 12 '15 00:02

Matthew Merryfull


1 Answers

For anyone else who's been pulling their hair out for a couple of days like I have, the solution was really simple...I was missing two properties in my Pushsharp QueueNotification request. So using the example given on the PushSharp github repo here: https://github.com/Redth/PushSharp#sample-code

push.QueueNotification(new GcmNotification().ForDeviceRegistrationId("DEVICE-REGISTRATION-ID-HERE").WithJson("{\"alert\":\"Hello World!\",\"badge\":7,\"sound\":\"sound.caf\"}"));

Needs to be updated to add the missing properties:

push.QueueNotification(new GcmNotification().ForDeviceRegistrationId("DEVICE REGISTRATION ID HERE")
                      .WithJson(@"{""alert"":""This is the future"",""badge"":7,""sound"":""sound.caf"",""title"":""Status Bar title"",""message"":""Some text you want to display to the user""}"));

Otherwise if your app happens to be developed using Cordova and its not currently in the foreground, nothing, repeat nothing will happen.

Tip my hat to gdelavald with his comment on PushPlugin for pointing me in the right direction here:

https://github.com/phonegap-build/PushPlugin/issues/212

like image 134
Matthew Merryfull Avatar answered Oct 04 '22 20:10

Matthew Merryfull