Reputation: 304
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!
Upvotes: 0
Views: 553
Reputation: 674
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(){...});
}
}
});
Upvotes: 1