推送通知条件

时间:2014-10-04 18:37:33

标签: java android android-layout android-activity parse-platform

美好的一天,

我正在使用Parse Push Notification,以下是我的困难: 简而言之,我想“合并”这两个条件:

 query.whereEqualTo("Gender", userLookingGender);
  pushQuery.whereEqualTo("the gender column of the ParseQuery", the user gender of the ParseQuery column);

换句话说,我想向属于该性别的用户发送一条消息。性别列以及性别可在名为“用户”的解析查询中找到。

更新: userLookingGender如下:

 String userLookingGender = ParseUser.getCurrentUser().getString(
            "Looking_Gender");

如果您需要任何澄清,请告诉我。

更新2: 我使用一个条件,性别,以便更容易理解。现在想象一下,如果我有多个条件,并且正在尝试仅向满足以下所有条件的收件人发送推送消息,并且在按钮单击时将其带到特定活动页面。

   ParseQuery<ParseObject> query = ParseQuery.getQuery("User");
                    query.whereNotEqualTo("objectId", ParseUser.getCurrentUser()
                            .getObjectId());
                    // users with Gender = currentUser.Looking_Gender
                    query.whereEqualTo("Gender", userLookingGender);
                    // users with Looking_Gender = currentUser.Gender
                    query.whereEqualTo("Looking_Gender", userGender);
                    query.setLimit(1) ;

                    ParseGeoPoint point = ParseUser.getCurrentUser().getParseGeoPoint("location");
                    query.whereWithinKilometers("location", point, mMax_Distance.doubleValue());

                    query.whereEqualTo("ActivityName", activityName);
                    query.whereGreaterThanOrEqualTo("UserAge", minimumAge);
                    query.whereLessThanOrEqualTo("UserAge", maximumAge);

更新3:

Android代码

                ParseCloud.callFunctionInBackground("sendPushToNearbyAndMatching", new HashMap<String, Object>(), new FunctionCallback<String>() {
                  public void done(String result, ParseException e) {
                    if (e == null) {
                      // success
                    }
                  }
                });

解析Cloud JavaScript代码(可在cloud / main.js中找到) 在这种情况下,所有者列是用户

 // Use Parse.Cloud.define to define as many cloud functions as you want.
    // For example:
    Parse.Cloud.define("hello", function(request, response) {
      response.success("Hello world!");
    });

Parse.Cloud.define("sendPushToNearbyAndMatching", function(request, response) {
    Parse.Cloud.useMasterKey();

    // the authenticated user on the device calling this function
    var user = request.user; 

    // the complex query matching users
    var query = new Parse.Query(Parse.User);
    query.whereNotEqualTo("objectId", user.id);
    // users with Gender = currentUser.Looking_Gender
    query.equalTo("Gender", user.get("Gender"));
    // users with Looking_Gender = currentUser.Gender
    query.equalTo("Looking_Gender", user.get("Looking_Gender"));
    query.equalTo("ActivityName", user.get("ActivityName"));

    query.greaterThanOrEqualTo("UserAge", user.get("Minimum_Age"));
   query.lessThanOrEqualTo("UserAge", user.get("Maximum_Age"));
    query.limit(1);


    query.each(function(user) {
        // sendPushNotification is added in next code section
        return sendPushNotification(user);
    }).then(function() {
        response.success("success!");
    }, function(err) {
        response.error(err);
    });

});


 var sendPushNotification = function(user) {

     var query = new Parse.Query(Parse.Installation);
     query.equalTo('users', user);

     return Parse.Push.send({
         where : query, // send to installations matching query
         expiration_interval : 600, // optional - expires after 10 minutes
         data : {
             alert: "App says hello!",
         }
     })
}

1 个答案:

答案 0 :(得分:1)

现在我有了更多的洞察力,我想我已经准备好了答案:

我从云代码推送,并且查询与安装对象匹配,这就是为什么在那里使用值也很有用的原因。

看起来您是直接从应用发送的,因此我建议为每个性别创建一个频道:https://parse.com/docs/push_guide#sending-channels/Android

然后你只需要:

String userLookingGender = ParseUser.getCurrentUser().getString(
        "Looking_Gender");
ParsePush push = new ParsePush();
push.setChannel(userLookingGender);
push.setMessage("Your message");
push.sendInBackground();

<强>更新

确定。多个查询确实使事情变得更加复杂。

我认为您必须转到Cloud Code才能执行此类高级查询推送(出于安全考虑,建议使用此方法)。

Cloud Code指南:https://parse.com/docs/cloud_code_guide

强调用户可以拥有多个设备这一事实,您需要能够获取与用户关联的所有安装。为此,我建议在每次安装时保存一个指向User的指针。您可以在首次登录应用时执行此操作。

假设您的安装中有一个owner列指向拥有该设备的相应用户,那么您可以在Cloud Code中执行以下操作:

Parse.Cloud.define("sendPushToNearbyAndMatching", function(request, response) {
    Parse.Cloud.useMasterKey();

    // the authenticated user on the device calling this function
    var user = request.user; 

    // the complex query matching users
    var query = new Parse.Query(Parse.User);
    query.whereNotEqualTo("objectId", user.id);
    // users with Gender = currentUser.Looking_Gender
    query.equalTo("Gender", user.get("Gender"));
    // users with Looking_Gender = currentUser.Gender
    query.equalTo("Looking_Gender", user.get("Looking_Gender"));
    query.limit(1);

    ... etc

    // execute the query
    // i am using each just to show an convenient way to iterate the results
    // instead of setting limit(1) consider executing the query using first() instead 
    // android SDK has a getFirstInBackground() as well

    query.each(function(user) {
        // sendPushNotification is added in next code section
        return sendPushNotification(user);
    }).then(function() {
        response.success("success!");
    }, function(err) {
        response.error(err);
    });

});

关于在javascript中查询用户:https://parse.com/docs/js_guide#users-querying

如何从Android调用此Cloud功能:https://parse.com/docs/android_guide#cloudfunctions

现在是时候将通知发送给用户拥有的设备了:

 var sendPushNotification = function(user) {

     var promise = new Parse.Promise();

     var query = new Parse.Query(Parse.Installation);
     query.equalTo('owner', user);

     query.count().then(function(count) {
         console.log("sending push to " + count + " devices");
         Parse.Push.send({
             where : query, // send to installations matching query
             expiration_interval : 600, // optional - expires after 10 minutes
             data : {
                alert: "App says hello!",
             }
        }).then(function() {
            // success
            console.log("push success");
            promise.resolve();
        }, function(error) {
            console.error(error.message);
            promise.reject(error);
        });
    });

    return promise;
}

有关更高级的推送(例如,如果您希望接收广播以处理某些数据),请参阅:https://parse.com/docs/push_guide#sending-queries/JavaScript

此外,如果您决定使用云代码并因此使用javascript,我强烈建议您查看承诺的工作原理。这使得处理异步调用时的生活变得更加容易,例如在发出查询时:https://parse.com/docs/js_guide#promises

这是很多信息,如果它对你来说是新的,可能需要大量的信息,但我认为这一切都是值得的,我知道这对我来说。