最近需要極光推送,不知道如何獲取到協議傳過來的值,
推送類型:透傳消息
消息內容:{"cmd":"force_logout","token":"被擠掉的用戶令牌","msg":"具體提示信息"}
應該怎麼獲取,在receiver中應該怎麼寫???
激光推送的SDK裡面有一個simple,在MyReceiver裡面這樣去拿
String content = (String) bundle.get(JPushInterface.EXTRA_ALERT);就能拿到內容,看你的需求是要解析一個json數據,demo裡面也有。
DEMO的71行裡面基本把解析方法已經說出來了
// 打印所有的 intent extra 數據
private static String printBundle(Bundle bundle) {
StringBuilder sb = new StringBuilder();
for (String key : bundle.keySet()) {
if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
}else if(key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)){
sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
} else if (key.equals(JPushInterface.EXTRA_EXTRA)) {
if (TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_EXTRA))) {
Log.i(TAG, "This message has no Extra data");
continue;
}
try {
JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
Iterator<String> it = json.keys();
while (it.hasNext()) {
String myKey = it.next().toString();
sb.append("\nkey:" + key + ", value: [" +
myKey + " - " +json.optString(myKey) + "]");
}
} catch (JSONException e) {
Log.e(TAG, "Get message extra JSON error!");
}
} else {
sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
}
}
return sb.toString();
}