简体   繁体   English

在android http post中转换“php脚本发送推送通知”

[英]Convert a “php script to send push notification” in android http post

I want to convert a php script (to send push notification in android devices) in to a android http post code. 我想将一个php脚本 (在android设备中发送推送通知)转换为android http邮政编码。

php script (working fine) : php脚本(工作正常)

printed response : 印刷回复:

{"multicast_id":7657471480142898452,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1362731818787207%38894628f9fd7ecd"}]} { “multicast_id”:7657471480142898452, “成功”:1, “失败”:0, “canonical_ids”:0 “结果”:[{ “MESSAGE_ID”: “0:1362731818787207%38894628f9fd7ecd”}]}

 <?php
 $registrationIds = array("APA91bH1-35qLot...."); //device registration id

$apiKey = "AIzaSyCrKrzFl...";   //"YOUR_BROWSER_API_KEY";

$response = sendNotification($apiKey,$registrationIds,array('message' => $message, 'tickerText' => $tickerText, 'contentTitle' => $contentTitle, "contentText" => $contentText) );

echo $response;

function sendNotification( $apiKey, $registrationIdsArray, $messageData ) {
$headers = array("Content-Type:" . "application/json", "Authorization:" . "key=" . $apiKey);
$data = array(
'data' => $messageData,
'registration_ids' => $registrationIdsArray
);

$ch = curl_init();

curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send" );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($data) );

$response = curl_exec($ch);
curl_close($ch);

return $response;

}

?>

Android code (not working): Android代码(不工作):

printed response : 印刷回复:

<HTML>
<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
 <BODY BGCOLOR="#FFFFFF" TEXT="#000000">
 <H1>Unauthorized</H1>
<H2>Error 401</H2>
 </BODY>
 </HTML>

public boolean sendMessage() {

        String serviceUrl = "https://android.googleapis.com/gcm/send";

        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("registration_ids", "APA91bH1-35qLotyb......"));
        nameValuePairs.add(new BasicNameValuePair("data", "this is a test message"));

        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(serviceUrl);
            httpPost.setHeader("Authorization", "AIzaSyCrKrzFlPbauq......");
            httpPost.setHeader("Content-Type", "application/json");
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            json_str = EntityUtils.toString(httpEntity);

        } catch (UnsupportedEncodingException e) {
            json_str = "Can't connect to server";
        } catch (MalformedURLException e) {
            json_str = "Can't connect to server";
        } catch (IOException e) {
            json_str = "Can't connect to server";
        }

        Log.i("***sendMessage", json_str);
        return true;
    }

You forget the key= prefix before your API key. 您忘记了API密钥之前的key=前缀。 Should be 应该

httpPost.setHeader("Authorization", "key=AIzaSyCrKrzFlPbauq......");

That's why you got the 401 Error. 这就是你得到401错误的原因。 You have more errors though. 但是你有更多的错误。 You are not sending a JSON string, even though your content type is declared as JSON. 即使您的内容类型声明为JSON,也不会发送JSON字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM