Android Parse.com push notification not sent

2019-09-03 12:15发布

问题:

Background

I have an Android app with a working receiver that can receive the push notification sent from iOS device and Parse website.

However, the following cases are not working:

  • send push notifications from Android to Android
  • send push notifications from Android to iOS

Since the Android app can receive push notifications without any problems, I guess there must be something with my logic/code of sending the push notifications

Problem Description

When sending push notifications using parsePush.sendInBackground(SendCallback) method, it returns no ParseExceptions. So it means no error.

But the Parse Dashboard does not show this push notifications and the target destination (either iOS or Android device in this case) does not get anything.

In the normal case, when a push notification is sent via Parse, it will show up as a push history in the Dashboard (the working case does that), but when I tried to send pushes from Android device, it just not show anything in the Dashboard and the pushes are never get delivered.

Code

The problematic Android code:

public void onShouldSendPushData(MessageClient messageClient, Message message, List<PushPair> pushPairs) {
        //TODO setup offline push notification
        Log.d(TAG, "Recipient not online. Should notify recipient using push");
        if (pushPairs.size() > 0 && !chatRecipient.isEmpty()) {
            PushPair pp = pushPairs.get(0);
            String pushPayload = pp.getPushPayload();

            ParsePush parsePush = new ParsePush();
            ParseQuery query = ParseInstallation.getQuery();
            ParseQuery userQuery = ParseUser.getQuery();
            userQuery.whereEqualTo("username", chatRecipient);
            query.whereMatchesQuery("user", userQuery);
            parsePush.setQuery(query);

            // JSON object for android push
            String alertString = getResources().getString(R.string.push_notification_msg);

            try {
                JSONObject data = new JSONObject();
                data.put("alert", String.format(alertString, chatRecipient));
                data.put("badge", "Increment");
                data.put("sound", "default");
                // pass the sender name as "title"
                data.put("title", ParseUser.getCurrentUser().getUsername());
                data.put("uri", "");
                data.put("SIN", pushPayload);

                parsePush.setData(data);
                parsePush.sendInBackground(new SendCallback() {
                    @Override
                    public void done(ParseException e) {
                        if (e == null) {
                            Log.i(TAG, String.format("Push notification to %s sent successfully", chatRecipient));
                        } else {
                            Log.e(TAG, String.format("Private chat push notification sending error: %s", e.getMessage()));
                        }
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

The working iOS code:

- (void)message:(id<SINMessage>)message shouldSendPushNotifications:(NSArray *)pushPairs {
    // use parse to send notifications
    // send notifications
    NSLog(@"Recipient not online. Should notify recipient using push");
    if (pushPairs.count > 0 && userSelected != nil) {
        pushData = [[pushPairs objectAtIndex:0] pushData];
        pushPayload = [[pushPairs objectAtIndex:0] pushPayload];

        PFPush *push = [[PFPush alloc] init];
        PFQuery *query = [PFInstallation query];
        PFQuery *userQuery = [PFUser query];
        [userQuery whereKey:@"username" equalTo:userSelected];

        [query whereKey:@"user" matchesQuery:userQuery];
        [push setQuery:query];
        NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:
                              [NSString stringWithFormat:@"You have a new Message from %@", [PFUser currentUser].username], @"alert",
                              @"Increment", @"badge",
                              @"default", @"sound",
                              pushPayload, @"SIN",
                              nil];

        [push setData:data];
        [push sendPushInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (succeeded) {
                NSLog(@"Push notification to %@ sent successfully.", userSelected);
            } else {
                NSLog(@"push notifications sending error: %@", error);
            }
        }];
    } else {
        NSLog(@"Error: No push pairs.");
    }

Note

I can confirm that the code above is getting called each time I want to send push notifications, and no exceptions are returned. I can also confirm that the data packaged inside the push is not null.

I'm not posting the receiver's code as that part of code is working and should do nothing with this issue.

The iOS code and Android code basically are the same, why the sending pushes function in Android not working?

UPDATE

I upgraded Parse SDK to 1.8.2, with its Logging options set to VERBOSE and still can't find any clue why the Push notifications are not sent.

I even made a simple project out of the Parse example project with only Login and send message functions and its sending message function is still not working. So frustrating.

回答1:

I have found the reason.

It is the "uri" field inside JSON.

As long as this field is included (with or without the content), notifications seemed being ignored by Parse, although you'll get a non-error callback.

If you remove "uri" field inside your JSON, notifications will become normal.

I've reported this bug to Parse and they've started to solve it.

Update

According to the reply from Parse.com, this is an intended feature, so the notifications with "uri" field will be discarded on the server side, thus it will not be sent.

related link: https://developers.facebook.com/bugs/338005256408244