Skip to content

Skalar/ember-cli-cordova-shims

Repository files navigation

Codeship npm npm npm

Ember CLI Cordova Shims

This Ember addon makes it simple to use a range of different Cordova plugins.

Supported features

Push Notifications

Dependencies:

  • cordova plugin add org.apache.cordova.device
  • cordova plugin add https://github.com/phonegap-build/PushPlugin.git

Automatic behavior:

  • Badge count will be set on the application on incoming notification
  • Sounds will play on incoming notification (if set in the payload)

Events:

  • alert - passes the message in the notification
  • badge - passes a badge count with every emitted event
  • sound - passes the filename of the sound to play

Public Methods:

  • register - Prompts the user to allow notifications to this device
  • unregister - Revokes notification subscription for this device

Usage:

// app/initializers/pushplugin.js
import NotificationsService from 'ember-cli-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
};
// app/routes/application.js
import Em from 'ember';

export default Em.Route.extend({

  badge: 0,

  listenForNotifications: function(){
    this.notifications.on('alert', function(message){
      alert(message); // or do whatever you want
    });
    this.notifications.on('badge', (badgeCount) => {
      this.set('badge', badgeCount); // or do whatever you want
    });
  }.on('init'),

  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);
      });
    },

    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);
      });
    }
  }
});

Caveats:

  • It's not possible to know what device what unregistered when calling unregister
  • PushPlugin has a weird setup for registering devices with GCM. There is a timeout of 15 seconds when doing a registration with GCM. If GCM does not send a registered event within this timeframe, the device is not registered. The timeout value may be overridden by passing gcmTimeout when instantiating the service.

Dialogs

Dependencies:

  • cordova plugin add https://github.com/apache/cordova-plugin-dialogs.git

Public Methods:

  • alert - Show a message to the user
  • confirm - Ask for some confirmation from the user
  • prompt - Ask for some information from the user

Usage:

// app/initializers/dialogs-service.js
import DialogsService from 'ember-cli-cordova-shims/services/dialogs';

export function initialize(container, application) {
  application.register('service:dialogs', DialogsService);
  application.inject('route', 'dialogs', 'service:dialogs');
}

export default {
  name: 'dialogs-service',
  initialize: initialize
};
// app/routes/application.js
import Em from 'ember';

export default Em.Route.extend({

  actions: {
    something: function(){
      this.dialogs.alert({
        title: 'Hey',
        message: 'This just happened',
        button: 'OK'
      }).then(function(){
        // Do something
      });
    }
  }
});

Network Activity Indicator

Dependencies:

  • cordova plugin add com.wearecocoon.cordova.plugin.networkactivity

Usage:

// app/adaptesr/application.js
import NetworkActivityAdapterMixin from 'ember-cli-cordova-shims/mixins/network-activity-adapter';

export default DS.RESTAdapter.extend(NetworkActivityAdapterMixin, {
  // Your implementation goes here
});

Installation

  • git clone this repository
  • npm install
  • bower install

Running

Running Tests

  • ember test
  • ember test --server

Documentation

The modules are documented with yuidoc.

  • npm install yuidocjs -g
  • yuidoc -c yuidoc.json

Building

  • ember build

For more information on using ember-cli, visit http://www.ember-cli.com/.