Robbie
Robbie

Reputation: 46

PushPlugin onNotification not fired on every second page - Cordova, Android

I'm working on an Android app with Cordova, and I have PushPlugin implemented. Everything works fine when the initial index.html file loads, but as soon as I load another html page, the onNotification function is not getting called.

From the LogCat I can see that the Javascript is being sent properly, but it is seemingly never being received. If I go on a third page, it will again work but then the fourth page won't. It's very weird because the success function is being called, but the onNotification is not.

Here is my code:

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

var pushNotification;

function onDeviceReady() {
    pushNotification = window.plugins.pushNotification;
    setupNotifications();
}

function setupNotifications() {

    if ( device.platform == 'android' || device.platform == 'Android' || device.platform == "amazon-fireos" ){

        pushNotification.register(
        successHandler,
        errorHandler,
        {
            "senderID":"527612045331",
            "ecb":"onNotification"
        });
    } else {
        pushNotification.register(
        tokenHandler,
        errorHandler,
        {
            "badge":"true",
            "sound":"true",
            "alert":"true",
            "ecb":"onNotificationAPN"
        });
    }
}

// Android and Amazon Fire OS
function onNotification(e) {
    // NOT REACHING HERE ON SECOND PAGES
    switch( e.event )
    {
    case 'registered':
        registerGCM(e.regid);
    break;

    case 'message':

        var notificationType = e.payload.data.type;

        break;

    case 'error':
        return;

    default:
        return;
  }
}

Any help is appreciated.

Upvotes: 0

Views: 393

Answers (2)

Divyesh
Divyesh

Reputation: 19

I had the same problem, I added 0.0.0.0/0 as ip address in gcm server key and it helped:

  1. Open your project in google developer console;
  2. Go to Credentials;
  3. Click on server key from api key(generate it if you not already done);
  4. Add Ip 0.0.0.0/0.

Upvotes: 0

user3751818
user3751818

Reputation:

onNotification must be have scope in window.

    window.onNotification = function(){

}

Upvotes: 1

Related Questions