-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathJPushModule.java
More file actions
289 lines (243 loc) · 10.1 KB
/
JPushModule.java
File metadata and controls
289 lines (243 loc) · 10.1 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package cn.reactnative.modules.jpush;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.BuildConfig;
import android.util.Log;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import org.json.JSONException;
import org.json.JSONObject;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.data.JPushLocalNotification;
/**
* Created by lvbingru on 10/23/15.
*/
public class JPushModule extends ReactContextBaseJavaModule {
private static Boolean registered = false;
private static JPushModule gModules = null;
private static String holdMessage = null;
public JPushModule(ReactApplicationContext reactContext) {
super(reactContext);
if (!registered) {
JPushInterface.init(reactContext);
if (BuildConfig.DEBUG) {
JPushInterface.setDebugMode(true);
}
registered = true;
}
}
@Override
public String getName() {
return "JPush";
}
@Override
public Map<String, Object> getConstants() {
final Map<String, Object> constants = new HashMap<>();
if (holdMessage != null) {
constants.put("initialNotification", holdMessage);
}
return constants;
}
@Override
public void initialize() {
super.initialize();
gModules = this;
}
@Override
public void onCatalystInstanceDestroy() {
super.onCatalystInstanceDestroy();
gModules = null;
}
private static void sendEvent(String eventName, Bundle bundle) {
if (gModules != null){
WritableMap message = Arguments.fromBundle(bundle);
DeviceEventManagerModule.RCTDeviceEventEmitter emitter = gModules.getReactApplicationContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
emitter.emit(eventName, message);
return;
}
}
private static final String TAG = "JPushReceiver";
public static void onReceive(Context context, Intent intent) {
// NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Bundle bundle = intent.getExtras();
if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
Log.d(TAG, "接受到推送下来的自定义消息");
JPushModule.sendEvent("kJPFNetworkDidReceiveCustomMessageNotification", bundle);
String title = bundle.getString(JPushInterface.EXTRA_TITLE);
String msg = bundle.getString(JPushInterface.EXTRA_MESSAGE);
String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
long ID = bundle.getLong(JPushInterface.EXTRA_NOTIFICATION_ID);
if (title == null) {
title = "";
}
if (msg == null) {
msg = "";
}
if (extras == null) {
extras = "";
}
JPushLocalNotification ln = new JPushLocalNotification();
ln.setBuilderId(0);
ln.setContent(msg);
ln.setTitle(title);
ln.setNotificationId(ID);
ln.setExtras(extras);
ln.setNotificationId(System.currentTimeMillis());
JPushInterface.addLocalNotification(context.getApplicationContext(), ln);
} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
Log.d(TAG, "接受到推送下来的通知");
JPushModule.sendEvent("kJPFNetworkDidReceiveMessageNotification", bundle);
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
Log.d(TAG, "用户点击打开了通知");
JPushModule.sendEvent("kJPFNetworkDidOpenMessageNotification", bundle);
if (gModules != null && gModules.getCurrentActivity()!=null) {
Intent mIntent = new Intent(context, gModules.getCurrentActivity().getClass());
mIntent.putExtras(bundle);
mIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mIntent);
}
} else {
Log.d(TAG, "Unhandled intent - " + intent.getAction());
}
}
@ReactMethod
public void stopPush(){
JPushInterface.stopPush(getReactApplicationContext());
}
@ReactMethod
public void getMyHoldMessage(Callback success_callback) {
if(JPushModule.holdMessage != null){
String str = new String(JPushModule.holdMessage);
JPushModule.holdMessage = null;
success_callback.invoke(str);
}else {
success_callback.invoke(new String());
}
}
@ReactMethod
public void resumePush(){
JPushInterface.resumePush(getReactApplicationContext());
}
@ReactMethod
public void setAlias(String alias) {
JPushInterface.setAlias(getReactApplicationContext(), alias, null);
}
@ReactMethod
public void setTags(ReadableArray tags, String alias) {
JPushInterface.setAliasAndTags(getReactApplicationContext(), alias, _stringArrayToSet(tags));
}
@ReactMethod
public void getRegistrationID(Callback callback) {
String registrationID = JPushInterface.getRegistrationID(getReactApplicationContext());
callback.invoke(registrationID == null ? "" : registrationID);
}
@ReactMethod
public void clearLocalNotifications() {
JPushInterface.clearLocalNotifications(getReactApplicationContext());
}
@ReactMethod
public void clearAllNotifications() {
JPushInterface.clearAllNotifications(getReactApplicationContext());
}
@ReactMethod
public void clearNotificationById(int notificationId) {
JPushInterface.clearNotificationById(getReactApplicationContext(), notificationId);
}
@ReactMethod
public void setPushTime
(ReadableArray weaks, int startHour, int endHour) {
JPushInterface.setPushTime(getReactApplicationContext(), _intArrayToSet(weaks), startHour, endHour);
}
@ReactMethod
public void setSilenceTime(int startHour, int startMinute, int endHour, int endMinute) {
JPushInterface.setSilenceTime(getReactApplicationContext(), startHour, startMinute, endHour, endMinute);
}
@ReactMethod
public void setLatestNotificationNumber(int maxNum) {
JPushInterface.setLatestNotificationNumber(getReactApplicationContext(), maxNum);
}
private Set _stringArrayToSet(ReadableArray array) {
Set<String> set = new HashSet<String>();
if (array != null) {
int size = array.size();
for (int i=0;i<size;i++) {
String obj = array.getString(i);
set.add(obj);
}
}
return set;
}
private Set _intArrayToSet(ReadableArray array) {
Set<Integer> set = new HashSet<Integer>();
if (array != null) {
int size = array.size();
for (int i=0;i<size;i++) {
Integer obj = array.getInt(i);
set.add(obj);
}
}
return set;
}
public static class JPushReceiver extends BroadcastReceiver {
public JPushReceiver() {}
@Override
public void onReceive(Context context, Intent intent) {
// Log.e(TAG, "onReceive - " + intent.getAction());
// Log.e(TAG, "onReceive - " + intent.getExtras().toString());
boolean isAppRunning = _isApplicationRunning(context);
// Log.e("onReceive", isAppRunning ? "running" : "not running");
if (isAppRunning) {
JPushModule.onReceive(context, intent);
}
else {
if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
Bundle bundle = intent.getExtras();
JSONObject json = new JSONObject();
Set<String> keys = bundle.keySet();
for (String key : keys) {
try {
json.put(key, bundle.get(key));
} catch(JSONException e) {
}
}
JPushModule.holdMessage = json.toString();
String packageName = context.getApplicationContext().getPackageName();
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
context.startActivity(launchIntent);
}
}
}
private boolean _isApplicationRunning(Context context) {
ActivityManager activityManager = (ActivityManager) context.getApplicationContext().getSystemService(context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> processInfos = activityManager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo processInfo : processInfos) {
if (processInfo.processName.equals(context.getApplicationContext().getPackageName())) {
if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
for (String d: processInfo.pkgList) {
return true;
}
}
}
}
return false;
}
}
}