Parse.com push notifications not consistently work

2019-05-03 16:35发布

问题:

Push notifications from parse.com is not consistently working. Randomly push notifications will fail, resulting in a GCM - MISMATCH SENDER ID" error. It is my understanding that programmatically we do not have to do anything with the GCM because parse.com sends the objectId to GCM. In either case, I have not been able to pinpoint any specific reason why this error occurs sometimes and other times it doesn't. Additionally, I am using Parse version, 1.10.2.

My Application class has the following

Parse.initialize(this, APPLICATION_ID_DEBUG, CLIENT_KEY_DEBUG);
            Parse.setLogLevel(Parse.LOG_LEVEL_VERBOSE);
ParsePush.subscribeInBackground(Constants.CHANNEL, new SaveCallback() {
            @Override
            public void done(ParseException e) {
                if (Utils.checkIfNull(e)) {
                    // subscribed to channel
                } else {
                    // failed to subscribe to channel
                }
            }
        });

After the user logs into my app I attach a channel to them. The channel data I save is just the user's unique id I am getting from server.

        List<String> arryChannel = new ArrayList<>();
        arryChannel.add(uniqueUserId);

        final ParseInstallation parseInstallation = ParseInstallation.getCurrentInstallation();
        parseInstallation.put(Constants.CHANNEL, arryChannel);
        parseInstallation.saveInBackground(new SaveCallback() {
            @Override
            public void done(ParseException e) {
                if (Utils.checkIfNull(e)) {
                    // update channel with user's unique id
                } else {
                    // failed to update channel with user unique id
                }
            }
        });

Finally, when the user logs out I unsubscribe them from their channel. I added unsubscribe to try and prevent any one device from receiving multiple push notifications because they have logged in as multiple users into the app and subscribed to multiple channels. The following is how my code looks when you log out.

                    ParsePush.unsubscribeInBackground(Constants.CHANNEL, new SaveCallback() {
                        @Override
                        public void done(ParseException e) {
                            if (Utils.checkIfNull(e)) {
                                // successfully unsubscribed to channel

                                // save the updated (unsubscribed) parse installation
                                final ParseInstallation parseInstallation = ParseInstallation.getCurrentInstallation();
                                parseInstallation.put(Constants.CHANNEL, new ArrayList<String>());
                                parseInstallation.saveInBackground(new SaveCallback() {
                                    @Override
                                    public void done(ParseException e) {
                                        if (Utils.checkIfNull(e)) {
                                            // add whatever logs here to check for any issues with unsubscribing
                                        } else {
                                            // failed to update channel
                                        }
                                    }
                                });
                            } else {
                                Logger.e("PARSE", "failed to unsubscribed to channel: " + e.getMessage());
                            }
                        }
                    });

The result of this implementation is that when push notifications are not working, it will continue to fail for about 50-100 times. Then it will start working for about 150-200 times. Then it goes back to not working. It is not a work, not-work type back and forth. It is more of a fail, fail, fail multiples times and then success, success, success multiple times. Any help on what I am missing in my implementation is appreciated. Thanks in advance.

回答1:

I have finally figured out the answer to this question! The issue had nothing to do with my implementation. For anyone else experiencing this same conflict, please look for any other 3rd party services that also are using push notifications. For me, Mixpanel was the culprit. When I deleted mixpanel.initPushHandling() from my codebase, all started working. And this makes sense, because when you initialize push notifications for mixpanel, you pass in a value that is used for GCMSenderID. Parse push notifications work differently. With parse.com, you do not have to send a GCMSenderID, because parse will automatically send in an objectId to perform their push notifications. Between the two, this causes a GCM-MISMATCH-SENDER error.

So the solution is, remove any sort of services that may be conflicting with parse.com. And feel free to use my implementation, it is good. Cheers!



回答2:

I faced the problem and after some rummage, finally found the solution. As Parse said in it's docs you should provide each Sender_ID that your app uses to push messages, if you use another push provider in addition to Parse. Take a look at below:

The Parse Android SDK chooses a reasonable default configuration so that you do not have to worry about GCM registration ids, sender ids, or API keys. In particular, the SDK will automatically register your app for push at startup time using Parse's sender ID (1076345567071) and will store the resulting registration ID in the deviceToken field of the app's current ParseInstallation.

However, as an advanced feature for developers that want to send pushes from multiple push providers, Parse allows you to optionally register your app for pushes with additional GCM sender IDs. To do this, specify the additional GCM sender ID with the following <meta-data> tag as a child of the <application> element in your app's AndroidManifest.xml:

<meta-data android:name="com.parse.push.gcm_sender_id"
           android:value="id:YOUR_SENDER_ID" />;

In the sample snippet above, YOUR_SENDER_ID should be replaced by a numeric GCM sender ID. Note that the Parse SDK expects you to prefix your sender ID with an id: prefix, as shown in the sample snippet.

If you want to register your app with multiple additional sender IDs, then the android:value in the <meta-data> element above should hold a comma-delimited list of sender IDs, as in the following snippet:

<meta-data android:name="com.parse.push.gcm_sender_id"
           android:value="id:YOUR_SENDER_ID_1,YOUR_SENDER_ID_2,YOUR_SENDER_ID_3" />;


回答3:

Check this out. I guess that this is the correct answer

https://support.layer.com/hc/en-us/articles/204496844-How-can-I-use-Layer-with-other-push-services-