Meteor-Community-Packages / raix-push

DEPRECATED: Push notifications for cordova (ios, android) browser (Chrome, Safari, Firefox)
https://atmospherejs.com/raix/push
MIT License
514 stars 197 forks source link

Android - The "Push.appCollection" is empty #363

Closed madc0w closed 5 years ago

madc0w commented 5 years ago

Here is the test project to demonstrate this case: https://github.com/madc0w/push-test

Clone this repo and then just:

meteor run android-device

On startup, a new user with a random username will be created. The server will then continue attempting to send push notifications every 8 seconds. This allows the app to be closed on the device, as notifications will not be received while the app is open.

In the server log, you will see:

I20190219-18:26:47.868(1)? Push: Send message "test title" via query {}
I20190219-18:26:47.870(1)? Push: Sent message "test title" to 0 ios apps 0 android apps
I20190219-18:26:47.871(1)? Push, GUIDE: The "Push.appCollection" is empty - No clients have registred on the server yet...

and, of course, no notification is ever received on the device.

There is a build.gradle.bak file under cordova-build-override/platforms/android. This file contains the modifications suggested in the raix:push docs, all indicated with a "ADDED MANUALLY!" comment. Attempting to build/run this project with this file (i.e., by removing the "bak" suffix from the file name) will crash with:

   Execution failed for task ':processDebugGoogleServices'.
   > No matching client found for package name 'com.idpxbkbrfctuod.ybwqvoa26l9c'

Nonetheless, the project builds fine without these manipulations, so I'm not sure this is important.

Android 6.0.1 Meteor 1.8.0.2

saikatharryc commented 5 years ago

@madc0w mostly use phonegap-plugin-push@1.5.2 i was using 2.2.3 and faced the same issue, but in 1.5.2 i resolved with that issue.

madc0w commented 5 years ago

Yes, we finally got it to work. Here is the confg that worked for us:

.meteor/packages: raix:push (obviously)

.meteor/versions: raix:push@3.4.1 (equally obvious)

.meteor/cordova-plugins: phonegap-plugin-push@2.2.3 cordova-plugin-device@2.0.2

cordova-build-override/platforms/android: copy your build.gradle file here from .meteor/local/cordova-build/platforms/android and add to the bottom:

// ADDED MANUALLY!
// see https://github.com/raix/push/issues/361
apply plugin: "com.google.gms.google-services"

// ADDED MANUALLY!
// see https://github.com/raix/push/issues/361
configurations.all {
    resolutionStrategy {
        force "com.android.support:support-v4:27.1.0"
    }
}

// ADDED MANUALLY!
// see https://github.com/raix/push/issues/361
configurations {
    all*.exclude group: "com.android.support", module: "support-v13"
}

and this bit (the lines marked "ADDED MANUALLY!") :

buildscript {
    repositories {
        jcenter()
        maven {
            url "https://maven.google.com"
        }
    }

    dependencies {
        classpath "com.android.tools.build:gradle:3.0.0"
        classpath "com.google.gms:google-services:4.0.1" // ADDED MANUALLY!
        classpath "com.google.firebase:firebase-core:11.0.1" // ADDED MANUALLY!
    }
}

Generate a google-services.json file and copy it to cordova-build-override/platforms/android.

Now, on the client:

        Push.Configure({
            android : {
                senderID : Meteor.settings.public.gcmSenderId,
                alert : true,
                badge : true,
                sound : true,
                icon : "ic_stat_icon_005_alpha_only",
                iconColor : "#00b3ff"
            // vibrate : true,
            // clearNotifications : true
            // icon: "ic_stat_icon_005_alpha_only",
            //          iconColor: "#00b3ff"
            },
            ios : {
                alert : true,
                badge : true,
                sound : true
            }
        });

        Push.enabled(true);

and, if you want:

        Push.setBadge(getNumNewUserNotifications()); // only relevant for iOS, ignored by other platforms

and finally, on the server:

        const apsCertFilename = "aps_prod_cert.pem";
        const apsKeyFilename = "aps_prod_key.pem";
        Push.Configure({
            apn: {
                certData: Assets.getText(apsCertFilename),
                keyData: Assets.getText(apsKeyFilename),
                // passphrase : "x",
                // production : true,
                // gateway : "gateway.push.apple.com"
            },
            gcm: {
                apiKey: Meteor.settings.private.gcmApiKey,
                //projectNumber : GCM_SENDER_ID
            },
            production: true,
            sound: true,
            badge: true,
            alert: true,
            vibrate: true,
            // sendInterval: 15000, Configurable interval between sending
            // sendBatchSize: 1, Configurable number of notifications to send per batch
            // keepNotifications: false,
        });

and....... we're almost there!

            const query = {};
            var badge = 1;
            if (user) {
                // will send to a specific user
                query.userId = user._id;
                badge = Math.max(1, newNotifCount(user._id));
            }
            messageText = Meteor.utils.truncate(messageText, 150);
            const summaryText = TAPi18n.__("you_have_n_notifications", {}, language);
            const notif = {
                //              android_channel_id : user ? user._id : null,
                from : "push",
                title : titleText,
                text : messageText,
                badge : badge,
                sound : "airhorn.caf",
                payload : {
// whatever data you want to send along with the notification
                },
                gcm : {
                    style : "inbox",
                    summaryText : summaryText
                },
                query : query
            };
            try {
                Push.send(notif);
            } catch (err) {
                console.error("Failed push notification", notif);
                console.error(err);
            }

I think that's about it. Hope this helps!

Oh, one more thing: This magic google-services.json file must include the new Firebase key, not GCM, and all references to GCM in the above should now be interpreted as Firebase instead, since Google broke the internet with Android 8.

shivang007 commented 5 years ago

@madc0w Glad to hear this finally worked for you! What was the issue?

madc0w commented 5 years ago

There were so many... and my memory is fading a bit, probably due to the trauma of this experience.

One which I do remember had nothing to do with raix or push at all, but rather TAPi18n, which simply crashes the server with a helpful "huh?" logged to the console if you say the wrong thing to it. That took a day to understand. Charming.

Specifically, this: TAPi18n.__("you_have_n_notifications", {}, language); which works great, but if you instead do: TAPi18n.__("you_have_n_notifications", language); Then the whole server goes down, and all the information you get is "huh?". Yeah... huh indeed.

shivang007 commented 5 years ago

Yea seems charming xD. I thought you got tired of errors and part your ways with the push functionality.