0

I'm trying to setup push notifications inside an initializer in an Ember CLI project. My initializer includes:

initialize: function(container, application) {
     var globals = container.lookup("route:application").get('globals');

     application.deferReadiness();
     document.addEventListener('deviceready', onDeviceReady, false);

     function onDeviceReady() {
           var startAppJob = setTimeout(function(){application.advanceReadiness();}, 10000),
           pushNotification = window.plugins.pushNotification;

           if ( device.platform == 'android' || device.platform == 'Android' ){
                 pushNotification.register(
                       successHandler,
                       errorHandler, 
                       {
                          "senderID":"xxx",
                          "ecb":"onNotificationGCM"
                       });
            }
    }

So far so good. Except the ecb now expects the "onNotificationGCM" function to be in the global scope. So, where should the following go?

   onNotificationGCM = function(e) {
        switch( e.event ){
          case 'registered':
            clearTimeout(startAppJob)
            application.advanceReadiness();

            if ( e.regid.length > 0 ){
                  globals.set('push_registration_id', e.regid)
                  globals.set('push_device_type', 'iandroidos')
                   console.log('registered')
            }
          break;

Declaring it with window.onNotificationGCM or this.onNotificationGCM inside the intializer doesn't work:

processMessage failed: Error: ReferenceError: onNotificationGCM is not defined

This question Cordova Pushplugin: ecb not called suggests altering the callback which in their example becomes:

"ecb":"window.GambifyApp.NotificationHandler.onNotificationGCM"

Except in Ember CLI what would this be inside an initializer? Is it even possible to access or is there a better way to implement all this in Ember CLI?

Thanks in advance for any help!

Community
  • 1
  • 1
user1814277
  • 304
  • 2
  • 9
  • 1
    `window.onNotificationGCM` should definitely work – runspired Mar 27 '15 at 01:56
  • 1
    I checked what we'd done, and it was this: `"ecb":"window.ecb"` – runspired Mar 27 '15 at 02:00
  • Ok, so the solution was indeed to use window.onNotifaction = function() as well as "ecb":"window.onNotifcation". You also need to place the window.onNotification function **outside** of the onDeviceReady function but within initialize. – user1814277 Mar 27 '15 at 10:31

1 Answers1

1

At my company we have developed a set of shims/interfaces to make working with Cordova plugins in Ember apps easier.

https://github.com/Skalar/ember-cli-cordova-shims

It hides all the handling of callbacks that PushPlugin requires, and exposes events emitted by a service in your application.

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

// In your route
export default Ember.Route.extend({
  init: function(){
    this._super();
    this.notifications.on('alert', function(){...});
  },

  actions: {
    subscribeForNotifications: function(){
      this.notifications.register().then(function(){...});
    }
  }
});
theodorton
  • 674
  • 1
  • 6
  • 11