gpt4 book ai didi

java - 谷歌云消息 : Payload value always string

转载 作者:太空宇宙 更新时间:2023-11-03 10:42:06 24 4
gpt4 key购买 nike

我正在为我的一个应用程序集成护目镜云消息传递。从服务器,我发送键值对为:

'not_id'       => 1000,
'title' => 'This is a title. title',
'vibrate' => 1,
'sound' => 1

在安卓 GCMIntentService 中:

protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {

int not_id=extras.getInt("not_id");

在提取key not_id(整数)的值时,抛出如下异常:

Key not_id expected Integer but value was a java.lang.String.java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer

gcm 是否将所有值都转换为字符串?

徒劳地浏览了文档。我做错了什么吗?

最佳答案

我遇到了同样的问题。我发现的解决方法是将我们创建的 json 字符串化,并将整个 json 作为键值对添加到我们发送到 gcm 云服务器的数据对象中 -

通常我们发送什么 -

myData: {
'title' : 'New Notification',
'myAge' : 25
}
json: {
'to': to,
'data': myData
}

这样,数据包中的所有值都被转换为字符串。上面的数据25被转换成String。

我这样做的方式 -

json: {
'to': to,
'data': {'myData' : myData}
}

现在 25 仍将是 Integer 。

注意 - 在发送之前将 myData JsonObject 字符串化。在 Javascript 中,我使用 JSON.stringify(myData);

然后在 Android 端我们可以检索整个 json -

@Override
public void onMessageReceived(String from, Bundle data) {
try {
JSONObject myData = new JSONObject(data.getString("myData"));
} catch (JSONException e){}
}

现在所有检索到的值都将是它们的原始类型。

关于java - 谷歌云消息 : Payload value always string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31726593/

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