phonegap / phonegap-plugin-push

Register and receive push notifications
MIT License
1.94k stars 1.91k forks source link

push plugin not registering certain devices #2232

Open bar-c opened 6 years ago

bar-c commented 6 years ago

Expected Behaviour

Register the device when the app starts obtaining a registrationId

Actual Behaviour

We have noticed that on some devices the registration is successful and on other fails, executing the push.on('error') message.

Additional Details

Looking at the docs, we see:

push.on('error', callback) The event error will trigger when an internal error occurs and the cache is aborted.

But we can't understand what internal error might be ocurring. If we delete the app's data and cache on the device, we receive a new registrationId and in this case it happens that the registration can be succesful the first time, or we clean data+ cache a few times and starts working. We noticed that on devices runing on Android 7.0 the registration is always succesful.

Reproduce Scenario (including but not limited to)

Initialize the plugin following the README example code:

this.registerPushNotification = function(cb) {

        function onPushPlugin() {
            try {
                if (window.plugins) {
            var push = PushNotification.init({
                "android": {
                    "senderID": ANDROID_SENDER_ID,
                    "clearNotifications": "true"
                },
                "ios": {
                    "senderID": IOS_SENDER_ID,
                    "alert": true,
                    "sound": true,
                    "badge": true,
                    "gcmSandbox": true
                },
                "windows": {}
            });
            push.on('registration', function(data) {
                util.setAPN(data.registrationId);
                console.log("[DEBUG] Device Registration event (idAPN): " + data.registrationId);
                console.log('---> idAPN: ' + util.getAPN());
                cb();
            });
            push.on('notification', function(data) {
                var notificationEvent = new CustomEvent("onNotification", { "detail": data });
                notificationEvent.detail = data;
                console.log("[DEBUG] Device Notification event: ", data);
                document.dispatchEvent(notificationEvent);
                push.finish(function() {
                    console.log("[DEBUG] Push-Success");
                }, function() {
                    console.error("[ERROR] Push-Finish");
                });
            });
            push.on('error', function(e) {
                console.error("[ERROR] Push-error: ", e);
                cb();
            });
                } else {
                    errorHandler("[DEBUG] No plugin");
        }
            } catch (err) {
                console.log('[DEBUG] Ha ocurrido el siguiente error en la pagina' + err);
                errorHandler(err);
            }
        }

            onPushPlugin();

        function errorHandler(error) {
            console.log('Error: ' , error);
            cb();
        }
    };

the function registerPushNotification, is called from a service when the app is initializing

Steps to Reproduce

Platform and Version (eg. Android 5.0 or iOS 9.2.1)

Android 7.0/6.0.1/4.4.2

(Android) What device vendor (e.g. Samsung, HTC, Sony...)

Tested on androids : Samsung S7/S8 and Motorola (moto g)

Cordova CLI version and cordova platform version

cordova --version                                    8.0.0
cordova platform version android            6.4.0

Plugin version

cordova plugin version | grep phonegap-plugin-push   1.10.6

Sample Push Data Payload

Sample Code that illustrates the problem

Code above

Logs taken while reproducing problem

We have the logs the client provided while logging on Android Studio

02-27 15:37:40.023 12910-12990/? E/PushPlugin: execute: Got JSON Exception RST 02-27 15:37:40.023 12910-12990/? D/PushPlugin: no icon option 02-27 15:37:40.023 12910-12990/? D/PushPlugin: no iconColor option 02-27 15:37:40.030 12910-12910/? D/SystemWebChromeClient: file:///android_asset/www/common/service/pushNotification/pushNotification.js: Line 110 : [ERROR] Push-error: RST 02-27 15:37:40.030 12910-12910/? I/chromium: [INFO:CONSOLE(110)] "[ERROR] Push-error: RST", source: file:///android_asset/www/common/service/pushNotification/pushNotification.js (110)

thank you!

macdonst commented 6 years ago

@bar-c I strongly recommend to upgrading to the 2.x versions of the plugin that support FCM.

I need more info as I can see you are getting a JSON exception in Java but none of the logs before that are shown. Running adb logcat | grep Push would help.

bar-c commented 6 years ago

@macdonst thanks! i'll run it as you said . we also tried upgrading to the 2.x versions, more specifically 2.1.0 and when trying to compile for android we get this error:

ANDROID_HOME=/home/bcardoso/apps/android-sdk JAVA_HOME=/usr/lib/jvm/java-8-oracle Subproject Path: CordovaLib The Task.leftShift(Closure) method has been deprecated and is scheduled to be removed in Gradle 5.0. Please use Task.doLast(Action) instead. at build_80taxggsi3d4l55fobiolz38i.run(/home/bcardoso/Supervielle/Supervielle_deploy_pushPlugin2.1.0/platforms/android/build.gradle:142) Configuration 'compile' in project ':' is deprecated. Use 'implementation' instead.

FAILURE: Build failed with an exception.

BUILD FAILED in 0s (node:4234) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: /home/bcardoso/Supervielle/Supervielle_deploy_pushPlugin2.1.0/platforms/android/gradlew: Command failed with exit code 1 Error output: FAILURE: Build failed with an exception.

BUILD FAILED in 0s (node:4234) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

macdonst commented 6 years ago

@bar-c yeah, it is failing to build because of #1718. See the last comment that explains the bug that Google has. In the meantime I've made it possible to set the play services/fcm version. You've probably got another plugin trying to include the same library.

bar-c commented 6 years ago

@macdonst yes, we had google analytics which we already removed. Is it imperative that we change branches 1.x to 2.x? Do you know if the device date and time influences the plugin or the expiration of the registrationId (if it had any)? thank you!

chadmilden commented 6 years ago

@macdonst I'm having trouble finding documentation on how to set the play services/fcm version. Currently I am doing this in the config.xml:

image

It still does not work when I try to deploy the app using PGB, still the same error: Failed to apply plugin [class 'com.google.gms.googleservices.GoogleServicesPlugin']

For input string: "+"

Could you point me in the right direction?

kareraisu commented 6 years ago

@chadmilden look for any firebase and/or play services dependencies in your platforms/android/build.gradle (and also in any extra .gradle file of your cordova plugins) and replace all the ":+" with a specific play services version. For example:

dependencies {
    implementation fileTree(dir: 'libs', include: '*.jar')
    // SUB-PROJECT DEPENDENCIES START
    implementation(project(path: "CordovaLib"))
    compile "com.google.gms:google-services:+" // this is OK (it is not *play* services)
    compile "com.google.android.gms:play-services-tagmanager:11.6.2" // REPLACED
    compile "com.google.firebase:firebase-core:11.6.2" // REPLACED
    compile "com.google.firebase:firebase-crash:11.6.2" // REPLACED
    compile "com.google.firebase:firebase-config:11.6.2" // REPLACED
    compile "com.google.firebase:firebase-messaging:11.6.2" // REPLACED
}
chadmilden commented 6 years ago

@kareraisu unfortunately I use phoneGapBuild to create ios and android install files. one of the caveats of phoneGapBuild is that you can't have any pre-build hooks, and you can't manually edit the build.gradle file, everything must work well together without manual or hook intervention. I'm still waiting for a fix or instrutions to get this working with phoneGapBuild.