Search code examples
androidcordovanotificationspushpushsharp

How to get android native push notification on cordova?


The event on application is being triggered when the server send the notification, but I get only the alert with the message on app, not the native notification.

I am using ionic framework with the $cordovaPush plugin. On the server side I am using PushSharp library to call GCM.

This is the code added to app.js on $ionicPlatform.ready

 var androidConfig = {
            badge: true,
            sound: true,
            alert: true,
            "senderID": "10xxxxxxxxxx"
        };

$cordovaPush.register(androidConfig).then(function (result) {
  // Success
}, function (err) {
  // Error
})

$rootScope.$on('$cordovaPush:notificationReceived', function (event, notification) {
  alert('$cordovaPush:notificationReceived');
  switch (notification.event) {
    case 'registered':
      if (notification.regid.length > 0) {
        alert('registration ID = ' + notification.regid);
      }
      break;

    case 'message':
      // this is the actual push notification. its format depends on the data model from the push server
      alert(JSON.stringify(notification));
      break;

    case 'error':
      alert('GCM error = ' + notification.msg);
      break;

    default:
      alert('An unknown GCM event has occurred');
      break;
  }
}, false);

This is the code from the server (C# + PushSharp lib)

var push = new PushBroker();
//Wire up the events for all the services that the broker registers
push.OnNotificationSent += NotificationSent;
push.OnChannelException += ChannelException;
push.OnServiceException += ServiceException;
push.OnNotificationFailed += NotificationFailed;
push.OnDeviceSubscriptionExpired += DeviceSubscriptionExpired;
push.OnDeviceSubscriptionChanged += DeviceSubscriptionChanged;
push.OnChannelCreated += ChannelCreated;
push.OnChannelDestroyed += ChannelDestroyed;

push.RegisterGcmService(new
                        GcmPushChannelSettings("AIxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"));

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


Solution

  • I have not used that plugin, but I recommend you this one:

    https://github.com/phonegap/phonegap-plugin-push

    this is the one most used, and it works like a charm