安卓 GCM : Can we get data as a complete JSON structure?

标签 android google-cloud-messaging

我正在向 Google GCM 服务器 https://android.googleapis.com/gcm/send 发送一条消息 (POST),以便向已注册的 android 设备发送推送通知。 POST 主体看起来像这样:

{
    "registration_ids" : [android_registration_id],
    "data" : {
        "message" : "Hello World!",
        "update" : "You've got a new update",
        "info" : "You've got new information updates too!"
    }
}

假设我不知道“数据”字段中所有键值对都发送给我(gcm注册的android应用程序),我想枚举并打印它们,我可以提取“数据”中的字段吗"作为 JSON 结构?

例如在上面的示例中,我需要以下内容作为 JSON 对象:

{
    "message" : "Hello World!",
    "update" : "You've got a new update",
    "info" : "You've got new information updates too!"
}

最佳答案

Bundle data = intent.getExtras();
Iterator<String> it = data.keySet().iterator();
String key;
String value;
while(it.hasNext()) {
    key = it.next();
    value = data.getString(key);
}

试试这个。使用键和值可以构造初始 json。

关于安卓 GCM : Can we get data as a complete JSON structure?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15115385/

相关文章:

java - Android Java 过滤项上的空指针异常

android - java中的 "Stub "和 "AIDL"是什么意思?

android - phonestateintentreceiver.notifyPhoneCallState 有什么用?

java - android.view.InflateException : Binary XML file line #34: Error inflating class AutoFitTextureView

android - 是否可以在设备上没有 Google 帐户和 Google Play 服务的情况下使用 GCM?

php - GCM 服务器出现 401 错误

android - Fcm 集成得到异常

android - 更改完整的 : App displayName, Logo 、iOS 和 Android [2018]?

ios - Google Cloud Messaging iOS,接收多个通知

php - Chrome 403 禁止错误的 GCM