API Docs for:
Show:

CordovaShims.Services.Notifications Class

Service that allows for registration/unregistration and handling of push notifications received from GCM or APNs.

Configuration and setup of the service is done in an initializer:

import NotificationsService from 'cordova-shims/services/notifications';

export function initialize(container, application) {
  let service = NotificationsService.create({
    gcmSenderId: '{INSERT-KEY-FROM-GCM-HERE}',
    gcmTimeout: 10000 // Timeout for GCM in ms. Default: 15000
  });
  application.register('service:notifications', service, {
    instantiate: false
  });
  application.inject('route', 'notifications', 'service:notifications');
}

export default {
  name: 'notifications-service',
  initialize: initialize
};

Methods

_playSound

(
  • filename
)
private

Plays a sound on the device. Fails silently.

Parameters:

  • filename String

    Filename for the sound to play.

_setBadgeCount

(
  • badgeCount
)
private

Sets the badge count on the app icon if possible. Fails silently.

Parameters:

  • badgeCount Number

    The current badge count.

_subscribeForEcb

() private

Sets up callbacks expected by PushPlugin

register

() RSVP.Promise

Registers the device for push notifications.

Example:

// app/routes/application.js
import Em from 'ember';

export default Em.Route.extend({
  actions: {
    subscribeForNotifications: function(){
      let store = this.store;

      this.notifications.register().then(function(data){
        let network = data.network,
              token = data.token;
        return store.createRecord('device', {
          network: network,
          token: token
        }).save();
      }).then(function(deviceModelFromStore){
        Em.Logger.info('Successfully registered for notifications');
      }).catch(function(error){
        Em.Logger.error('Something went wrong registering for notifications', error);
      });
    }
  }
});

Returns:

RSVP.Promise:

A promise that will resolve with an object containing the network (apns or gcm) and the token for the device.

unregister

() RSVP.Promise

Unregisters this specific device for push notifications.

Example:

// app/routes/application.js
import Em from 'ember';

export default Em.Route.extend({
  actions: {
    unsubscribeNotifications: function(){
      this.notifications.unregister().then(function(){
        Em.Logger.info('Successfully unregistered the device');
      }).catch(function(error){
        Em.Logger.error('Something went wrong while unregistering', error);
      });
    }
  }
});

Returns:

RSVP.Promise:

A promise that will resolve when registration is successful.

Properties

gcmSenderId

String

Project ID from Google API Console. Must be set if you want to register devices with GCM.

gcmTimeout

Number

Timeout for GCM registration. In ms.

Default: 15000

Events

alert

Event Payload:

  • message String

    The message received in the payload

badge

Event Payload:

  • count Number

    Badge count recieved in the payload

registered

private

Please do not attach event listeners to this event. Use the register method instead to handle device registration.

Event Payload:

  • data Object

    An object with a network and a token. network will be either 'apns' or 'gcm'.

sound

Event Payload:

  • filename String

    Filename of the sound that should be played