推送類型為「透傳訊息」,內容為 JSON 格式:{"cmd":"force_logout","token":"被擠掉的用戶令牌","msg":"具體提示信息"}。需在 BroadcastReceiver 中正確提取並解析。
onReceive 中獲取 Bundle 物件。JPushInterface.EXTRA_EXTRA 取得自訂欄位字串。JSONObject 解析 JSON,提取 cmd、token、msg 等欄位。private static String printBundle(Bundle bundle) {
StringBuilder sb = new StringBuilder();
for (String key : bundle.keySet()) {
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();
}
AndroidManifest.xml 正確註冊 Receiver 並設定 android:exported="true"。optString 避免欄位缺失拋出異常。