简体   繁体   English

使用Java将HTTP发布到Google Cloud Messaging(Android推送通知)

[英]HTTP Post to Google Cloud Messaging (Android Push Notification) using Java

I am trying to send a message to GCM (java class) so then it can send it as a push notification. 我正在尝试将消息发送到GCM(java类),以便随后可以将其作为推送通知发送。 I am getting a 200 response. 我收到200条回应。

When my phone receives the message it shows it as null. 当我的手机收到该消息时,它将显示为空。

My code for the java class: 我的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();
    }

}
}

The Response I am getting is: 我得到的回应是:

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]

Client Controller Class 客户端控制器类

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

}

Client Intent Service: 客户意向服务:

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

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

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

Client Broadcast Receiver: 客户端广播接收器:

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

You are sending a parameter named message : 您正在发送一个名为message的参数:

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

But your client expects a parameter named price : 但是您的客户需要一个名为price的参数:

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

That would explain the null message. 那将解释空消息。

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

相关问题 错误-使用Google Cloud Messaging(GCM)的Android Push Notification - ERROR - Android Push Notification using Google Cloud Messaging (GCM) 使用XMPP服务器和Google云消息传递(或更新的Firebase云消息传递)进行推送通知的Android应用程序App - Chat App for Android using a XMPP Server and Google Cloud Messaging (or the newer Firebase Cloud Messaging) for Push Notifications Android:Android推送通知应该使用什么? Google Cloud Messaging或Parse.com - Android: what should I use for android push notification? Google Cloud Messaging or Parse.com 带有Google Cloud消息传递的android推送通知库 - android push notifications library with google cloud messaging 如何实现Google Cloud Messaging发送推送通知Android Studio的服务器 - How to implementing server for Google Cloud Messaging to send Push notification android studio 使用 Firebase Cloud Messaging 创建每日推送通知 - Create daily push notification using Firebase Cloud Messaging 具有Android Java中的Google Cloud Messaging的Firebase - Firebase with Google Cloud Messaging in Android Java 使用MySql在Java中实现Google Cloud消息传递 - Google Cloud messaging implementation in Java using MySql 在谷歌云消息传递为Android - In Google cloud messaging for android Firebase Cloud Messaging使用Java发送HTTP-Post - Firebase Cloud Messaging Send HTTP-Post in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM