admin
2022-04-29 67bdeebb4dc381a2f46f31e3027ebcc3243a8aeb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package com.yeshi.makemoney.video.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.webkit.WebChromeClient;
import android.webkit.WebViewClient;
import android.widget.TextView;
 
import com.demo.lib.common.activity.BaseActivity;
import com.demo.lib.common.util.common.StringUtils;
import com.yeshi.makemoney.video.app.ui.MainActivity;
import com.yeshi.makemoney.video.app.utils.ui.JumpPageUtil;
 
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<String, Object> ps = null;
        if (!StringUtils.isEmpty(params)) {
            ps = new HashMap<>();
            JSONObject jsonObject = new JSONObject(params);
            for (Iterator<String> 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;
        }
    }
 
}