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

2

1

Background

  1. I installed PushPlugin. According to the docs I used automatic installation. But when I run cordova run android, JavaScript returns the error, 'Cannot read property pushNotification of undefined'

  2. If I add

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

    then the error changes to the one in this question's title.

  3. This is how my HTML loads the 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>
    

    Initialization code is in index.js where after deviceready I call pushNotification.register.

    After the register completes, I call MyEmberApp.deferReadiness()

  4. After automatically installing the plugin, I just have to run register, according to the docs. But this still leads to 'Cannot read pushNotification....'

  5. It seems that PushNotification.js is automatically inserted after deviceready fires. But the plugin is not doing so. If I insert the script in index.html, the error Object has no method 'exec' occurs because deviceready hasn't fired yet.

  6. deviceready

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

Question

What am I doing wrong? How should I do this?

Update

I just realized that I have only tried the automatic installation. I have not tried the manual steps. But that is no reason why the direct plugin install shouldn't work

Distributor answered 22/11, 2014 at 12:5 Comment(1)
I'm voting to close this question as off-topic because it arose as a result of an error in my setup. The question is hence irrelevantDistributor
D
2

I finally realized that the error was due to the 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 right into place. Though this is a careless mistake, I still leave this question and it's answers for others who might encounter this issue

Distributor answered 20/12, 2014 at 6:21 Comment(0)
D
1

I can't really see, why ur solution is not working. The only thing I can provide is my working solution. There might be some redundancies or unnecessary things in there, because I myself tried 35 Versions before I got it working:

First Thing is I attach to pg events in an App 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 is registering the 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 in a case of success the ECB is called with the device ID which is could 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 might help.

Dolly answered 26/11, 2014 at 11:57 Comment(5)
Thanks for looking into it. Before I try a different approach, I tried to install Cordova Device plugin, but that too did not work.Distributor
It seems that the way I have initialized and used Ember is probably the cause. I'm working on thatDistributor
I have tried everything, removed platform, added platform, removed plugins, added them. I also removed all ember code with just the plugins and still the error did not changeDistributor
I have updated my question. You might be able to point me in the right direction now!Distributor
Have u tried building the App with build.phonegap.com maybe its a version issue or somethingDolly

© 2022 - 2024 — McMap. All rights reserved.