Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse REST API: Having the channel name, can I get the device type before actually sending to Push?

We have a server which occasionally sends pushes to our users. Every user has his own channel name in Parse. Because our ios and android pushes look entirely different, we try to send the push two times, one time targetting his channel name and android devices and one time targetting his channel name and ios devices:

$aAndroidPush = array(
    "where" => array(
        "deviceType" => "Android",
        "channels" => array('$in' => array("push_user"))
    ),
    "data" => array(
        "action" => "com.android.action",
        "alertMessage" => "this is a push",
        "t" => "web",
        "m" => 0
    )
);

$aIOSPush = array(
    "where" => array(
        "deviceType" => "ios",
        "channels" => array('$in' => array("push_user"))
    ),
    "data" => array(
        "alert" => "this is a push",
        "sound" => "p.mp3",
        "t" => "web",
        "m" => 0 ,
        "badge" => ""
    )
);

$aHeaders = array(  
       "Content-Type: application/json",  
       "X-Parse-Application-Id: parse_app_id" .,  
       "X-Parse-REST-API-Key: parse_rest_key" .   
);

$oRest = curl_init();
curl_setopt($oRest,CURLOPT_HTTPHEADER,$aHeaders);  
curl_setopt($oRest,CURLOPT_SSL_VERIFYPEER, false);  
curl_setopt($oRest,CURLOPT_RETURNTRANSFER, true);   
curl_setopt($oRest,CURLOPT_URL,"https://api.parse.com/1/push");   
curl_setopt($oRest, CURLOPT_POST, 1);  

curl_setopt($oRest, CURLOPT_POSTFIELDS, json_encode($aIOSPush));
curl_exec($oRest);

curl_setopt($oRest, CURLOPT_POSTFIELDS, json_encode($aAndroidPush));
curl_exec($oRest);

So my question is, is there a way to make a query in parse (REST API or the new PHP API) before sending the pushes, to see if the targetting channel is using either android or ios and thus sending only one push request and not two?

like image 274
Petros Mastrantonas Avatar asked Apr 09 '15 11:04

Petros Mastrantonas


2 Answers

Parse has great PHP docs that can help here. Because you are using a custom push notification handler in Android, the default Push code won't likely work for you.

One of the things to look at is the Parse Query to the "_Installation" object.

Your code would be:

$query = new ParseQuery("_Installation"); // or try ParseInstallation()?
$query->equalTo("objectId", $yourObjectId);
$result = $query->first();
if ($result->get("deviceType") == "android") {
   // we are Android
 } else {
   // we are iOS
 }

It would help to know what other data you have on the Installation prior to the push. If you have objectId, that makes this code work. If you have an array of channels, you'll need to select users in those channels, and then foreach over the array, grab the objectId, and make a query that way.

Happy to explain if you leave a comment letting me know one way or the other.

like image 162
Adam Link Avatar answered Oct 29 '22 09:10

Adam Link


I haven't used Parse and I am not sure what kind of information you have stored related to the user. But seems like if you have the installation objectId then you can use it to retrive the deviceType of the user.

For more details Reference

Edited:

Seems like there is yet another way of doing it.

You can Query Installations and pass Query Constraintsto get the desired user object in return.

like image 23
Arun Poudel Avatar answered Oct 29 '22 07:10

Arun Poudel