0

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.

4
  • please see this link stackoverflow.com/a/24949806/3310181 Commented Jan 7, 2015 at 6:40
  • @param thx for the help mate. I looked through your answer and I can't see any difference in the sending function, however I do find some difference in the AndroidManifest.xml. Yours are difference from the Parse.com push notification guide (parse.com/tutorials/android-push-notifications) Could you explain why? Commented Jan 7, 2015 at 9:22
  • @param and also, like I said in the question: my app can receive notifications without a problem, but it can't send push notifications(no records on Dashboard and the destination device can't receive it), do you have any idea why? thanx Commented Jan 7, 2015 at 9:25
  • You were actually able to get a push notification to show up on your notification bar? Because this does not work for me. Commented Aug 21, 2015 at 22:21

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

@ChintanShah please refer to the updated answer, thanks
@ss1271 can u pls tell me what i m doing wrong i m still failed to send notification to user stackoverflow.com/questions/30303082/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.