我正在尝试将消息发送到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]


客户端控制器类

// 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");


那将解释空消息。

10-06 10:54