PushPlugin TypeError: Object # <Object> has no method '' exec

Background

  • I installed PushPlugin . According to the docs, I used unattended installation. But when I run cordova run android , JavaScript returns an error: "Unable to read the pushNotification property from undefined"

  • If I add

     <script type="text/javascript" charset="utf-8" src="PushNotification.js"></script> 

    then the error will change to the one in this question.

  • This is how my HTML loads scripts

     <script type="text/javascript" src="cordova.js"></script> <script src="js/libs/jquery-1.10.2.js"></script> <script src="js/libs/handlebars-1.1.2.js"></script> <script src="js/libs/ember-1.5.1.js"></script> <script type="text/javascript" src="js/index.js"></script> <script type="text/javascript" src="js/model.js"></script> <script type="text/javascript" src="js/router.js"></script> <script type="text/javascript" src="js/controller.js"></script> <script type="text/javascript" src="js/view.js"></script> 

    The initialization code is in index.js , where after deviceready I call pushNotification.register .

    After register completed, I call MyEmberApp.deferReadiness()

  • After installing the plugin automatically, I just need to run register , according to the docs. But this still leads to "Unable to read pushNotification ...."

  • It seems that PushNotification.js automatically inserted after deviceready triggered. But the plugin does not. If I insert a script in index.html , an Object has no method 'exec' error occurs because deviceready is not running yet.

  • deviceready

     if ('device is android') { document.addEventListener("deviceready", this.onDeviceReady(), false); } 

Question

What am I doing wrong? How can I do it?

Update

I just realized that I just tried the silent installation. I have not tried manual steps. But that is not the reason why direct installation of the plugin should not work.

+1
source share
2 answers

Finally, I realized that the error was caused by EventListener for deviceready . I changed

  if ('device is android') { document.addEventListener("deviceready", this.onDeviceReady(), false); } 

to

  document.addEventListener("deviceready", this.onDeviceReady, false); 

and everything fell into place. Although this is a careless mistake, I still leave this question, and it is responsible for others who may encounter this problem.

+2
source

I can't understand why the cheers solution is not working. The only thing I can provide is my working solution. There may be some layoffs or unnecessary things, because I myself tried 35 versions before I earned:

The first thing I attach to pg events in the Application Initializer and register my notification services:

 Ember.Application.initializer({ name: 'phonegap', /* ...... */ initialize: function(container, application){ // Push container.register('notification:manager', GambifyApp.NotificationManager, { singleton: true }); container.register('notification:handler', GambifyApp.NotificationHandler, { instantiate: false }); container.injection('notification:handler', 'appController', 'controller:application'); container.injection('notification:handler', 'commentRoute', 'route:usergroup.comment'); } } 

Then my manager service registers the device:

 GambifyApp.NotificationManager = window.GambifyApp.NotificationManager = Ember.Object.extend({ init: function(){ //var self = this; var pushNotification = Ember.get(window, 'plugins.pushNotification'); if(!Ember.isEmpty(pushNotification)){ if ( device.platform == 'android' || device.platform == 'Android' ) { pushNotification.register( this.successHandler, this.errorHandler, { "senderID":GambifyApp.config.android_sender_id, "ecb":"window.GambifyApp.NotificationHandler.onNotificationGCM" }); } } else { Ember.Logger.error('pushNotification Plugin not running'); } GambifyApp.NotificationHandler.manager = this; }, successHandler: function (result) { }, errorHandler: function (error) { Ember.Logger.error('Error while registering with push:' + error); }, }); 

Then, if successful, the ECB is called with a device identifier that can be processed by my handler:

 GambifyApp.NotificationHandler = window.GambifyApp.NotificationHandler = { manager: null, onNotificationGCM: function(e){ console.log('---------- GCM Event:-----------'); console.log(e); if(e.event === "registered") { console.log(e.regid); // Registraion ID } }, }; 

Hope this helps.

+1
source

Source: https://habr.com/ru/post/977405/


All Articles