I need to send push notifications to my android app from my server using Parse Push Notification.
What I found is you can send push notification to the specific devices by using its object_id
, which is unique and assigned to the app at every installation.
Now I am using the REST API of Parse to send push notification by following the PHP code
<?php
$url = 'https://api.parse.com/1/push';
$appId = 'YOUR_APP_ID_HERE';
$restKey = 'YOUR_REST_API_KEY_HERE';
$target_device = 'TARGET_INSTALLATION_OBJECT_ID_HERE'; // using object Id of target Installation.
$push_payload = json_encode(array(
"where" => array(
"objectId" => $target_device,
),
"data" => array(
"alert" => "This is the alert text."
)
));
$rest = curl_init();
curl_setopt($rest,CURLOPT_URL,$url);
curl_setopt($rest,CURLOPT_PORT,443);
curl_setopt($rest,CURLOPT_POST,1);
curl_setopt($rest,CURLOPT_POSTFIELDS,$push_payload);
curl_setopt($rest,CURLOPT_HTTPHEADER,
array("X-Parse-Application-Id: " . $appId,
"X-Parse-REST-API-Key: " . $restKey,
"Content-Type: application/json"));
$response = curl_exec($rest);
echo $response;
?>
I implemented it in JAVA
String restApiKey = "xxxxxxxxxx";
String appKey = "xxxxxxxxxx";
static String targetDevice = "xxxxxxxxxx";
static String url = "https://api.parse.com/1/push";
public static void main(String a[]){
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
JSONObject json = new JSONObject();
json.put("objectId", targetDevice);
json.put("test", "01234");
StringEntity s = new StringEntity(json.toString());
s.setContentEncoding("UTF-8");
s.setContentType("application/json");
post.setEntity(s);
httpClient.execute(post);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Now, I don't know where to put port (443) in this as well as the appId
and rest-api-key
, which is put in the header in PHP code.