java - 使用 Java HTTP Post 到 Google Cloud Messaging(Android 推送通知)

标签 java android apache push-notification google-cloud-messaging

我正在尝试向 GCM(java 类)发送消息,以便它可以将其作为推送通知发送。我收到了 200 条回复。

当我的手机收到消息时,它显示为空。

我的java类代码:

package com.push.notification.server;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;

public class sendNotification {
    public static String REQUEST_URL = "https://android.googleapis.com/gcm/send";
    private static String GCM_ID = "APA91bHae4Fx4H0zqLfb0_IlmxMdAGP16UB0HnrpTAPgnVnKt8XUTOYfern_0N4CizwICKCdgU-xEKS_1JEsyfcaNatBSuroxaxn30ub7jrhlUTHNInw1okJRlKrJkuRRaG1-qYuBJJ2";

/**
 * @param args
 */
public static void main(String[] args) {

    List<NameValuePair> formparams = new ArrayList<NameValuePair>();

    formparams.add(new BasicNameValuePair("registration_id", GCM_ID));
    formparams.add(new BasicNameValuePair("data.message", "testing"));
    // UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams,
    // "UTF-8");

    HttpClient httpclient = HttpClientBuilder.create().build();
    HttpPost httpPost = new HttpPost(REQUEST_URL);
    HttpResponse response;

    httpPost.setHeader("Authorization",
            "key=AIzaSyCmQuVoudeMktmXshp8gQep9DAqWklic4s");
    httpPost.setHeader("Content-Type",
            "application/x-www-form-urlencoded;charset=UTF-8");

    try {
        httpPost.setEntity(new UrlEncodedFormEntity(formparams, "utf-8"));
        httpclient.execute(httpPost);

        //Get the response
        response = httpclient.execute(httpPost);

        int responseCode = response.getStatusLine().getStatusCode();
         String responseText = Integer.toString(responseCode);      
         System.out.println("HTTP POST : " + responseText);

         /*Checking response */
         if(response!=null){
             InputStream in = response.getEntity().getContent(); //Get the data in the entity
             System.out.println("HTTP POST : " + in.toString());
         }
        //Print result
        System.out.println(response.toString());

    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
}

我得到的回应是:

HTTP POST : 200
HTTP POST : org.apache.http.conn.EofSensorInputStream@5673ef7
HTTP/1.1 200 OK [Content-Type: text/plain; charset=UTF-8, Date: Tue, 28 Jan 2014 21:38:15 GMT, Expires: Tue, 28 Jan 2014 21:38:15 GMT, Cache-Control: private, max-age=0, X-Content-Type-Options: nosniff, X-Frame-Options: SAMEORIGIN, X-XSS-Protection: 1; mode=block, Server: GSE, Alternate-Protocol: 443:quic, Transfer-Encoding: chunked]

客户端 Controller 类

// Notifies UI to display a message.
void getPushNotification(Context context) {

Intent intent = new Intent(Config.DISPLAY_MESSAGE_ACTION);
intent.putExtra(Config.EXTRA_MESSAGE, message);

// Send Broadcast to Broadcast receiver with message
context.sendBroadcast(intent);

}

客户意向服务:

@Override
protected void onMessage(Context context, Intent intent) {

Log.i(TAG, "Received message");
message = intent.getExtras().getString("price");

Log.i("GCM MESSAGE", "Message= " + message);
}

客户端广播接收器:

public class GCMReceiver extends GCMBroadcastReceiver { 
@Override
protected String getGCMIntentServiceClassName(Context context) { 
return "com.testing.gcm.GCMIntentService"; 
}

最佳答案

您正在发送一个名为message的参数:

formparams.add(new BasicNameValuePair("data.message", "testing"));

但是您的客户需要一个名为 price 的参数:

message = intent.getExtras().getString("price");

这可以解释空消息。

关于java - 使用 Java HTTP Post 到 Google Cloud Messaging(Android 推送通知),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21417601/

相关文章:

java - 如何通过 Java 使用公私钥身份验证来使用 SFTP 的可靠示例

java - 在android中快速下载HTML的方法?

安卓蓝牙打印

linux - 如何调试本地主机 - 无法访问该站点

apache - 在单个服务器 (IP) 上为多个域(虚拟主机)设置 SSL

java - Eclipse 中的 Pom.xml 错误

java - 在Servlet中获取表单值

java - 如何在 JXDatePicker 上注册 KeyStroke

java - 我们可以通过对象引用找到 Unix Pid 吗?

低内存清理后的 Android onActivityResult 和 onResume 生命周期