dongxuying7583 2013-11-22 07:46
浏览 31

使用Parse在java中使用Rest Api从我自己的服务器发送推送通知

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器