简体   繁体   English

GCM给出空消息

[英]GCM gives null message

I am trying to get the message from server as a push notification in android. 我试图从服务器获取消息作为Android中的推送通知。 I am able to hit the server and but I got null message from server. 我能够点击服务器,但我收到了来自服务器的空消息。 I can see notification in android with no message. 我可以在android中看到没有消息的通知。 This is my code from server and I got android code from ANDROID HIVE 这是我的服务器代码,我从ANDROID HIVE获得了android代码

public class GCMBroadcast {
    @POST
    @Path("/getgcm")
public String getGcmData(){
    String str="success";


                try {
                    System.out.println("From CLient");
                    Sender sender = new Sender(
                            "AIzaSyBbfXkbCYWQdE5qyjJKwl-YLBX-F01ICug");
                    // add your own google api key in android menifest
                    // use this to send message with payload data
                    Message message = new Message.Builder()
                            .collapseKey("message")
                            .timeToLive(3)
                            .delayWhileIdle(true)
                            .addData("message", "Welcome to Push Notifications")
                            // you can get this message on client side app
                            .build();
                    System.out.println("message:"+message);
                    System.setProperty("http.proxyHost", "192.168.1.110"); 
                    // write you own proxy
                    System.setProperty("http.proxyPort", "8080");

                    // write you own proxy host
                    // Use this code to send notification message to a single device
                    Result result = sender
                            .send(
                                    message,
                                    "APA91bFEmQ53TKnJQXa0HbF9lXGTMEyRrp-6H9-_zZNBdFAUMsvXIG0rpvKcXn_6L5wBP77HskWw4svo6GLHZwfWdDf-yQCBAvIqp4fQF05cWqDtJ8mfNDnAQ8qdXByaEqwDmK3aQi0xIq7L3XGF1dSkbOOfBFIjlfDzlj4SG3z_SA-v3IUz_g4",
                                    1);
                    System.out.println("Message Result: " + result.toString()); 

                    // Use this code to send notification message to multiple
                    // devices
                    /*ArrayList<String> devicesList = new ArrayList<String>();

                    // add your devices RegisterationID, one for each device
                    devicesList
                            .add("APA91bFEmQ53TKnJQXa0HbF9lXGTMEyRrp-6H9-_zZNBdFAUMsvXIG0rpvKcXn_6L5wBP77HskWw4svo6GLHZwfWdDf-yQCBAvIqp4fQF05cWqDtJ8mfNDnAQ8qdXByaEqwDmK3aQi0xIq7L3XGF1dSkbOOfBFIjlfDzlj4SG3z_SA-v3IUz_g4");

                    // Use this code for multicast messages
                    MulticastResult multicastResult = sender.send(message,
                            devicesList, 0);
                    sender.send(message, devicesList, 0);
                    System.out.println("Message Result: "
                            + multicastResult.toString());
                     */
                } catch (Exception e) {
                    e.printStackTrace();
                    str="Failure";
                }
        return str;
    }
}

and I am using android hive example 我正在使用android hive示例

In the link you claim to get your code from, the message is extracted from the price parameter : 在您声称从中获取代码的链接中,邮件是从price参数中提取的:

/**
 * Method called on Receiving a new message
 * */
@Override
protected void onMessage(Context context, Intent intent) {
    Log.i(TAG, "Received message");
    String message = intent.getExtras().getString("price");

    displayMessage(context, message);
    // notifies user
    generateNotification(context, message);
}

However, in your server code, you put the message in a message parameter : 但是,在您的服务器代码中,您将消息放在message参数中:

                Message message = new Message.Builder()
                        .collapseKey("message")
                        .timeToLive(3)
                        .delayWhileIdle(true)
                        .addData("message", "Welcome to Push Notifications")

That would explain getting a null message. 这可以解释获得空消息。

It's possible, of course, that you changed the client code you got from that link, but since you didn't post your client code, I have no way of knowing that. 当然,您可以更改从该链接获得的客户端代码,但由于您没有发布客户端代码,我无法知道这一点。

hi in demo project you are getting intent.getExtras().getString("price"); 您好,在演示项目中,您将获得intent.getExtras()。getString(“price”); price here.As you are using php As your php admin to prvide you "name" field and use that name field and get message i also resolve this.. 价格在这里。当你使用PHP作为你的PHP管理员提供你“名称”字段并使用该名称字段和获取消息我也解决这个问题..

 @Override
        protected void onMessage(Context context, Intent intent) {
            Log.i(TAG, "Received message");
            String message = intent.getExtras().getString("message");
            displayMessage(context, message);
            // notifies user
            generateNotification(context, message);
        }

Parameter and variable key must match. 参数和变量键必须匹配。

You tested through this Web page but make android key "message" identical: 您通过此网页进行了测试,但使android密钥“消息”相同:

在此输入图像描述

在此输入图像描述

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

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