gpt4 book ai didi

android - GCM 给出空消息

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:03:55 27 4
gpt4 key购买 nike

我正在尝试从服务器获取消息作为 android 中的推送通知。我能够访问服务器,但我从服务器收到了空消息。我可以在 android 中看到没有消息的通知。这是我来自服务器的代码,我从 ANDROID HIVE 获得了安卓代码

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;
}
}

我正在使用 android hive example

最佳答案

在您声称从中获取代码的链接中,消息是从 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);
}

但是,在您的服务器代码中,您将消息放在 message 参数中:

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

这可以解释为什么会收到空消息。

当然,您可能更改了从该链接获得的客户端代码,但由于您没有发布您的客户端代码,我无法知道这一点。

关于android - GCM 给出空消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17880593/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com