New! Check out Ionic Native. It's like ngCordova but for ES6 and TypeScript.

$cordovaPushV5

Allows your application to receive push notifications. To receive notifications in your controllers or services, listen for $cordovaPushV5:notificationReceived event.

Note: Ionic offers a tightly-integrated Push Notification service to make sending and receiving push notifications easy for Ionic apps: Ionic Push docs

# This requires phonegap/cordova CLI 5.0+ ( current stable v1.7.2 )
phonegap plugin add phonegap-plugin-push --variable SENDER_ID="XXXXXXX"

# or
cordova plugin add phonegap-plugin-push --variable SENDER_ID="XXXXXXX"

# It is also possible to install via repo url directly ( unstable )
phonegap plugin add https://github.com/phonegap/phonegap-plugin-push --variable SENDER_ID="XXXXXXX"

# or
cordova plugin add https://github.com/phonegap/phonegap-plugin-push --variable SENDER_ID="XXXXXXX"
cordova plugin add https://github.com/phonegap-build/PushPlugin.git

Methods

initialize(options)

Initializes the plugin on the native side.

Param Type Detail
options Object Configuration object for setting default values

Returns Promise which resolves to an PushNotification instance.

For details about options, refer to the official docs.

onNotification()

Start listening to receive push notifications. This should be called after initialize resolves.

onError()

Start listening to receive push notification errors. This should be called after initialize resolves.

register()

Returns Promise which resolves to data including the registrationId

unregister()

The unregister method is used when the application no longer wants to receive push notifications. Beware that this cleans up all event handlers previously registered, so you will need to re-register them if you want them to function again without an application reload.

$rootScope.$on('$cordovaPushV5:notificationReceived', func(event, notification))

Triggered each time a push notification is received by a 3rd party push service on the device.

For details about the callback parameters, refer to the official docs

$rootScope.$on('$cordovaPushV5:errorOcurred', func(event, error))

Triggered when an internal error occurs and the cache is aborted.

For details about the callback parameters, refer to the official docs

getBadgeNumber() - iOS only

Get the current badge count visible when the app is not running

setBadgeNumber(number) - iOS only

Set the badge count visible when the app is not running

Param Type Detail
number Integer Indicates what number should show up in the badge. Passing 0 will clear the badge
finish() - iOS only

Tells the OS that you are done processing a background push notification.

Example

Setup Push Notifications when the App is first opened:

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
    $cordovaPushV5.onNotification();
    // start listening for errors
    $cordovaPushV5.onError();
    
    // register to get registrationId
    $cordovaPushV5.register().then(function(registrationId) {
      // save `registrationId` somewhere;
    })
  });
  
  // triggered every time notification received
  $rootScope.$on('$cordovaPushV5:notificationReceived', function(event, data){
    // 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
  });

});

Additional Resources