简体   繁体   English

未发送Android Parse.com推送通知

[英]Android Parse.com push notification not sent

Background 背景

I have an Android app with a working receiver that can receive the push notification sent from iOS device and Parse website. 我有一个运行正常的接收器的Android应用,可以接收从iOS设备和Parse网站发送的推送通知。

However, the following cases are not working: 但是,以下情况不起作用:

  • send push notifications from Android to Android 将推送通知从Android发送到Android
  • send push notifications from Android to iOS 将推送通知从Android发送到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 由于Android应用程序可以毫无问题地接收推送通知,因此我想发送推送通知的逻辑/代码中肯定有一些内容

Problem Description 问题描述

When sending push notifications using parsePush.sendInBackground(SendCallback) method, it returns no ParseException s. 使用parsePush.sendInBackground(SendCallback)方法发送推送通知时,它不返回任何ParseException 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. 但是“解析仪表板”不会显示此推送通知,并且目标位置(在这种情况下为iOS或Android设备)也不会获得任何信息。

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. 在正常情况下,当通过Parse发送推送通知时,它会显示为仪表板中的推送历史记录(工作情况就是这样),但是当我尝试从Android设备发送推送时,它只是不显示任何内容仪表板和推送将永远无法实现。

Code

The problematic Android code: 有问题的Android代码:

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: 有效的iOS代码:

- (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. 我还可以确认推送中打包的数据不为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? iOS代码和Android代码基本相同,为什么Android中的发送推送功能不起作用?

UPDATE 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. 我将Parse SDK升级到1.8.2,并将其Logging选项设置为VERBOSE,但仍然找不到任何提示不发送Push通知的线索。

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. 我什至从Parse示例项目中仅通过Login和send message功能创建了一个简单项目,而其send message功能仍然无法使用。 So frustrating. 真令人沮丧

I have found the reason. 我找到了原因。

It is the "uri" field inside JSON. 它是JSON中的“ uri”字段。

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. 只要包含此字段(包含或不包含内容),Parse似乎都会忽略通知,尽管您会收到一个非错误的回调。

If you remove "uri" field inside your JSON, notifications will become normal. 如果您删除JSON中的“ uri”字段,则通知将变为正常。

I've reported this bug to Parse and they've started to solve it. 我已将此错误报告给Parse,他们已经开始解决它。

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. 根据Parse.com的答复,这是预期的功能,因此带有“ uri”字段的通知将在服务器端被丢弃,因此将不会发送。

related link: https://developers.facebook.com/bugs/338005256408244 相关链接: https : //developers.facebook.com/bugs/338005256408244

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM