Search code examples
javascriptandroidcordovapush-notificationaerogear

Cordova aerogear-push android push is not defined


I'm trying to build a simple application on cordova using aerogear push notification plugin

What i'm doing is following closely this guide: https://aerogear.org/docs/guides/aerogear-cordova/AerogearCordovaPush/#_sample_example

However, after put the sample code in my js, this line:

push.register(onNotification, successHandler, errorHandler, pushConfig);

will cause a reference error since push is not defined

I followed all the step before and the aerogear-cordova-push plugin is in the folder of the plugins, maybe i require some additional steps to refer to the plugin?

Also, the plugin provide an index.html as example inside its folder, but even using that i'm not able to resolve push

I tried to move the js files of the plugin in the www folder and linked them on index before the execution of index.js, since this it isn't very correct cause other reference errors

The index.html on the www folder is the same that a standard cordova project provide after its creation

This is my index.js, i'm able to show the error on android throught the try catch:

var app = {
// Application Constructor
initialize: function() {
    this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
    try {
        app.receivedEvent('deviceready');
        var pushConfig = {
            pushServerURL: "...",
            android: {
                senderID: "...",
                variantID: "...",
                variantSecret: "..."
            }
        };
        push.register(app.onNotification, successHandler, errorHandler, pushConfig);
    }
    catch (e){
        alert(e);
    }


    function successHandler() {
        console.log('success')
    }

    function errorHandler(message) {
        console.log('error ' + message);
    }
},
onNotification: function(event) {
    alert(event.alert);
},

receivedEvent: function(id) {
    var parentElement = document.getElementById(id);
    var listeningElement = parentElement.querySelector('.listening');
    var receivedElement = parentElement.querySelector('.received');

    listeningElement.setAttribute('style', 'display:none;');
    receivedElement.setAttribute('style', 'display:block;');

    console.log('Received Event: ' + id);
}};  app.initialize();

Solution

  • I solved this because the plugin wasn't correct installed in my application, and the installation would fail because i was missing the google-service.json file that is required in order to build for android