使用 Back4App 发送 ParsePush 通知

Sending ParsePush notifications with Back4App

在从 Parse 迁移到 Back4App 之前,我可以通过查询我要向其发送推送通知的用户的 Installation 对象轻松发送这样的推送通知:

    private void handleLikeNotification(ParseObject messageObject) {
        String userId = messageObject.getParseObject(ParseConstants.KEY_SENDER_AUTHOR_POINTER).getObjectId();
        String result = messageObject.getString(ParseConstants.KEY_NOTIFICATION_TEXT);

        // Initiate installation query
        ParseQuery<ParseInstallation> pushQuery = ParseInstallation.getQuery();
        pushQuery.whereEqualTo("userId", userId);

        // Create push notification
        ParsePush push = new ParsePush();
        push.setQuery(pushQuery);
        push.setMessage("Liked by " + ParseUser.getCurrentUser().getUsername() + ": " + result);
        push.sendInBackground();


        // Check condition and send push notification
        if (!userId.equals(ParseUser.getCurrentUser().getObjectId())) {
            send(notification);
        }

    protected void send(ParseObject notification) {
        notification.saveInBackground(new SaveCallback() {
            @Override
            public void done(ParseException e) {
                if (e == null) {
                        // success!

                } else {
                        // notification failed to send!

                }
            }
        });
     }

我遵循了他们的文档并且我的 Receiver 工作正常,因为我可以通过他们的仪表板向我自己和我的用户发送通知。

我对 Whosebug 的问题是为什么我不能通过我的应用程序创建和发送这样的推送通知?这个相同的代码适用于我的应用程序,它仍然由原始的 Parse 托管解决方案托管。

我的推送通知现在由 GCM 提供服务,所以我想我需要使用 GCM 发送它们 class 但我不知道去哪里看。

此时我最好的假设是 ParsePush class 不适用于我当前的配置。还有什么选择?

刚刚查看了 Back4App 的文档,他们似乎有一些关于如何设置客户端推送的步骤,这似乎是您遇到的问题。

根据它,您需要在 Parse 初始化之后立即放置 GCM 发件人 ID,如下所示:

ParseInstallation installation = ParseInstallation.getCurrentInstallation();
installation.put("GCMSenderId", "Your GCM Sender ID here");
installation.saveInBackground();

之后,只需使用 ParsePush 查询:

ParseQuery pushQuery = ParseInstallation.getQuery();
// Here you can personalize your pushQuery
// Add channels as you like, or remove this part to reach everyone
LinkedList<String> channels = new LinkedList<String>();
channels.add("group_1");
// Change this "message" String to change the message sent
String message = "Back4App says Hi!";
// In case you want to send data, use this
JSONObject data = new JSONObject("{\"rating\": \"5 stars\"}");

ParsePush push = new ParsePush();
// Set our Installation query
push.setQuery(pushQuery);
// Sets the channels
push.setChannels(channels);
// Sets the message
push.setMessage(message);
// Sets the data
push.setData(data);
push.sendInBackground();

文档还说您需要启用客户端推送选项。

也许这应该有助于解决您的问题。