2014-10-04 54 views
0

好日子,推送通知條件

我使用的解析推送通知,以下是我的苦衷: 總之,我想「合併」這兩個條件:

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 
        } 
        } 
       }); 

解析雲JavaScript代碼(在雲/ 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

如果將用戶的「Gender」和「Looking_Gender」保存到「Installation」對象,聽起來會更容易。然後,您可以使用單個查詢定位安裝以進行推送。或者我誤解了,必須承認這個問題對我來說有點困惑。 – cYrixmorten 2014-10-04 19:41:11

+0

我看到你在哪裏,但我不想創建不必要的請求。從某種意義上說,如果用戶的性別已經存儲在「用戶」表下的解析中,那麼僅僅從這裏獲取信息反對通過安裝創建一個名爲性別的新列將不會更好。它在那裏存儲的原因是因爲我不僅僅使用Push通知,在許多活動中我都參考了parsequery。 – John 2014-10-04 20:01:43

+1

澄清,這兩個值是否已經保存在用戶對象上? – cYrixmorten 2014-10-04 20:08:59

回答

1

現在我有了更多的見解,我想我已經準備好了一個答案:

我從Cloud代碼推送,並且查詢與安裝對象相匹配,這就是爲什麼在那裏也有值有用。

看起來你是直接從應用程序發送的,所以我建議建立針對不同性別的頻道: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(); 

更新:

好的。多重查詢確實使問題更加複雜。

我想你將不得不轉移到雲代碼來執行這樣的高級查詢推送(這是推薦的安全原因)。

雲代碼指南:https://parse.com/docs/cloud_code_guide

Embrasing一個事實,即用戶可以擁有多個設備,你需要能夠獲取與用戶相關聯的所有設施。爲此,我建議在每次安裝時保存一個指向User的指針。您可以將其作爲您的應用第一次登錄的一部分。

假設你有一個,比如說,在你安裝指向相應的用戶擁有的設備owner列,那麼你可以做的雲代碼是這樣的:

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這個雲功能: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

這是所有大量的信息,如果它對你來說可能很重要,但我認爲這將是所有值得的,我知道這是爲我。

+0

感謝您花時間提供響應。最初的帖子 – John 2014-10-04 20:43:55

+1

沒問題:)我知道我是如何應付類似的挑戰的,我已經更新了我的回答。 – cYrixmorten 2014-10-04 21:35:58

+0

謝謝你的詳細解釋,我承認我會花一點時間來抓住所有這些信息,但我相信這將是值得的。你會偶然知道如何將JavaScript代碼轉換爲android,因爲它似乎在它的當前窗體中讀取它。謝謝 – John 2014-10-04 22:05:44