How to let server respond to app using Parse?

时间:2015-07-28 16:33:31

标签: android parse-platform

I'm not sure about the terminology of what I want to do at all, so I have no idea where to begin looking. I'm using Parse.com for my application's back end and it's working like a dream, I've been able to set up all my databases as necessary and retrieve and store new objects and users.

Here's what I need: Once a user's app adds an entry to a database, the server should send a response to all users that are logged in said database. I believe I should do this with the Cloud Code that's available on the Parse platform, but I have no idea how to do this. Any thoughts?

Thanks for reading -R

1 个答案:

答案 0 :(得分:1)

当用户向数据库添加信息时,将其写入表中。在云代码中写一个beforeSave钩子到这个表。因此,每次创建表条目时,都会调用这段javascript云代码。在此处,您可以向用户列表发送推送通知。可以在您的云代码中再次创建用户列表。云代码看起来像

Parse.Cloud.beforeSave("Tablename", function(request,response) {


    var post = request.object.get("post");
    var author = request.object.get("user");
    var postId = post.id;

    if (!request.object.createdAt) {
        post.fetch({
            success: function(post) {
                var query = new Parse.Query("Comments");
                query.equalTo("post", post);
                query.include("user");
                query.find({
                    success: function(results) {
                        var pushUsers = [];
                        var pushUserIds = [];

                        for (var i = 0; i < results.length; i++) {
                            var object = results[i];
                            if (!(author.id === object.get("user").id)) {
                                if (pushUserIds.indexOf(object.get("user").id) == -1) {
                                    pushUserIds.push(object.get("user").id);
                                    pushUsers.push(object.get("user"));
                                }
                            }
                        }
                        console.log("printing comment pushUsers");
                        console.log(pushUserIds);



                         var pushQuery = new Parse.Query(Parse.Installation);
                        pushQuery.containedIn("user", pushUsers);

                        Parse.Push.send({
                            where: pushQuery, // Set our Installation query

                            data: {
                                alert: "New Comment",
                                postObjectID: postId
                            }
                        }, {
                            success: function() {
                                // Push was successful
                            },
                            error: function(error) {
                                // Handle error
                            }
                        });
                        response.success();

                        }
                        catch(e) {
                            console.log(e);
                        }
                    },
                    error: function(error) {
                        console.log("Got an error " + error.code + " : " + error.message);
                    }
                });
            }
        });
        }
    response.success();

    });

您可以按照此步骤初始设置云代码。 https://parse.com/docs/js/guide#cloud-code-getting-started