package com.demo.app.push; import android.app.Activity; import android.app.ActivityManager; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.widget.TextView; import com.demo.app.ui.MainActivity; import com.demo.app.utils.ui.JumpPageUtil; import com.demo.lib.common.activity.BaseActivity; import com.demo.lib.common.util.common.StringUtils; import org.json.JSONException; import org.json.JSONObject; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import cn.jpush.android.api.JPushInterface; public class PushOpenClickActivity extends BaseActivity { public static PushData pushData = null; private static final String TAG = "OpenClickActivity"; private TextView mTextView; public static Intent createIntent(Context context, String type, String params) { Intent intent = new Intent(context, PushOpenClickActivity.class); intent.putExtra("type", type); intent.putExtra("params", params); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); return intent; } private static void jump(Activity context, String type, String params) throws ClassNotFoundException, JSONException { pushData = null; Log.i(TAG, String.format("%s:%s", type, params)); Map ps = null; if (!StringUtils.isEmpty(params)) { ps = new HashMap<>(); JSONObject jsonObject = new JSONObject(params); for (Iterator keys = jsonObject.keys(); keys.hasNext(); ) { String key = keys.next(); ps.put(key, jsonObject.get(key)); } } JumpPageUtil.jump(JumpPageUtil.AppJumpType.valueOf(type), ps, context); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mTextView = new TextView(this); setContentView(mTextView); JSONObject extra = handleOpenClick(); if (extra == null) { finish(); return; } String params = extra.optString("params"); String type = extra.optString("type"); StringBuilder builder = new StringBuilder(); builder.append("\n"); builder.append("params:" + params); mTextView.setText(builder.toString()); if (!isAppRunning(getApplicationContext(), getPackageName())) { //暂存数据 pushData = new PushData(type, params); Intent intent = new Intent(this, MainActivity.class); startActivity(intent); finish(); } else { try { jump(this, type, params); finish(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } } } private String getPushData() { String data = null; //获取华为平台附带的jpush信息 if (getIntent().getData() != null) { data = getIntent().getData().toString(); } //获取fcm、oppo、vivo、华硕、小米平台附带的jpush信息 if (TextUtils.isEmpty(data) && getIntent().getExtras() != null) { data = getIntent().getExtras().getString("JMessageExtra"); } return data; } private JSONObject handleOpenClick() { Log.d(TAG, "用户点击打开了通知"); String data = getPushData(); Log.w(TAG, "msg content is " + String.valueOf(data)); if (TextUtils.isEmpty(data)) return null; try { JSONObject jsonObject = new JSONObject(data); String msgId = jsonObject.optString("msg_id"); byte whichPushSDK = (byte) jsonObject.optInt("rom_type"); //上报点击事件 JPushInterface.reportNotificationOpened(this, msgId, whichPushSDK); JSONObject extras = jsonObject.optJSONObject("n_extras"); if (extras != null) { return extras; } } catch (JSONException e) { Log.w(TAG, "parse notification error"); } return null; } //获取推送SDK的名称 private String getPushSDKName(byte whichPushSDK) { String name; switch (whichPushSDK) { case 0: name = "jpush"; break; case 1: name = "xiaomi"; break; case 2: name = "huawei"; break; case 3: name = "meizu"; break; case 4: name = "oppo"; break; case 5: name = "vivo"; break; case 6: name = "asus"; break; case 8: name = "fcm"; break; default: name = "jpush"; } return name; } private boolean isAppRunning(Context context, String packageName) { ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); ActivityManager.RunningTaskInfo info = manager.getRunningTasks(1).get(0); Log.i(TAG, "numActivities:" + info.numActivities); Log.i(TAG, "numRunning:" + info.numRunning); if (info.numActivities < 2) return false; else return true; } public static void resumeJumpActivity(Activity context) { if (pushData != null) { try { jump(context, pushData.type, pushData.getParams()); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } } } class PushData { private String params; private String type; public PushData(String type, String params) { this.params = params; this.type = type; } public String getParams() { return params; } public void setParams(String params) { this.params = params; } public String getType() { return type; } public void setType(String type) { this.type = type; } } }