Ember CLI + PhoneGapBuild推送通知

时间:2015-03-27 01:34:24

标签: cordova ember.js ember-cli

我正在尝试在Ember CLI项目中的初始化程序中设置推送通知。我的初始化程序包括:

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

到目前为止一切顺利。除了ecb现在预计“onNotificationGCM”功能在全球范围内。那么,以下应该去哪里?

   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;

使用window.onNotificationGCM或in.UNNotificationGCM在初始化程序中声明它不起作用:

  

processMessage失败:错误:ReferenceError:onNotificationGCM不是   定义

这个问题Cordova Pushplugin: ecb not called建议改变其示例中的回调:

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

除了在Ember CLI中,它在初始化器中会是什么?是否可以访问或是否有更好的方法在Ember CLI中实现所有这些?

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

在我的公司,我们开发了一套垫片/接口,以便更轻松地使用Ember应用程序中的Cordova插件。

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

它隐藏了PushPlugin所需的所有回调处理,并公开了应用程序中服务发出的事件。

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(){...});
    }
  }
});