【问题标题】:Sending ParsePush notifications with Back4App使用 Back4App 发送 ParsePush 通知
【发布时间】:2016-08-29 18:51:38
【问题描述】:

在从 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 工作正常,因为我可以通过他们的仪表板向我自己和我的用户发送通知。

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

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

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

【问题讨论】:

    标签: java android parse-platform push-notification google-cloud-messaging


    【解决方案1】:

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

    据此,您需要在 Parse 初始化之后将您的 GCM Sender 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();
    

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

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

    【讨论】:

    • 谢谢!此解决方案有效。我已经知道如何通过他们的服务启用基于云的通知,但是客户端推送更容易实现,您不必编写任何额外的 javascript。干杯。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-31
    相关资源
    最近更新 更多