3

I'm developing an app and need it to save the device's register ID with push.on('registration'), so in the future I can send notifications with it. It is working with IOS but not with Android.

var scripts = {
initialize: function () {
    this.bindEvents();
},
bindEvents: function () {
    document.addEventListener('deviceready', this.onDeviceReady, false);
    document.addEventListener('deviceregistered', this.onDeviceRegistered, false);
},
onDeviceReady: function () {

    window.open = cordova.InAppBrowser.open;

    screen.orientation.lock('portrait');

    var w = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
    var viewport = document.getElementById("viewport");
    if (w < 320) {
        viewport.setAttribute('content', 'width=device-width, initial-scale=0.68, user-scalable=no');
    }

    navigator.splashscreen.hide();

    setTimeout(function () {
        scripts.setupPush();
    }, 1000);

},
setupPush: function () {
    var push = PushNotification.init({
        "android": {
            "senderID": "125487585225"/*,
             "icon": "twitter",
             "iconColor": "red" */
        },
        "browser":{},
        "ios": {
            "senderID": "125487585225",
            "gcmSandbox": false,
            "sound": true,
            "vibration": true,
            "badge": true,
            "alert": true
        },
        "windows": {}
    });

    push.on('registration', function (data) {
        alert('teste');
        $.post( webService + 'Registro_Dispositivo/Salvar', {
            "id": data.registrationId,
            "plataforma": device.platform
        }) .always (function (){
            window.localStorage.setItem(appnome + "-registrationId", data.registrationId);
        });
    });

    push.on('notification', function (data) {
        var href = '#/' + data.additionalData.link;
        window.location.href = href;
        alert('notification');
    });

    push.register(function(){
        alert('register');
        alert("OK");
    }, function(){
        alert("Erro");
    });

    PushNotification.hasPermission(function(data){
        alert(data.isEnabled);
        alert('tem permissão');
    });

    push.on('error', function (e) {
        alert("Erro no Push = " + e.message);
        // console.log(data.registrationId);
    });
},
onDeviceRegistered: function (evt) {
    // alert('Device Registered');
    console.log('aqui');
    localStorage.setItem(appnome + '-devicePushId', evt.devicePushId);
    localStorage.setItem(appnome + '-devicePushToken', evt.devicePushToken);
}
};


scripts.initialize();

As said above, this code is working fine with IOS, but nothing happens when I execute it on android, it is like there is no action at all.

Thanks in advance.

2 Answers 2

4

Make sure you've added the 'google-services.json' file in your App location. Also please verify your Firebase account (https://console.firebase.google.com/) with included the App package name like 'com.yourapp.name'. Once verified please rebuilt the App.

0
3

What's the output in the console when debugging the app with the device connected?

Anyway, there are plenty of things to do to ensure the push plugin will work. Apart from adding the google-services.json file you might need to edit config.xml. Also, actions to install the plugin are heavily dependant on your cordova CLI and cordova-android versions which you failed to mention.

Make sure you have read the plugin's Installation page from top to bottom entirely and you'll surely find out where's the problem.

Not the answer you're looking for? Browse other questions tagged or ask your own question.