|
1 | 1 |
|
2 | 2 | package cn.jiguang.plugins.push; |
3 | | - |
| 3 | +import android.app.NotificationChannel; |
| 4 | +import android.app.NotificationManager; |
| 5 | +import android.net.Uri; |
| 6 | +import android.os.Build; |
4 | 7 | import android.app.Activity; |
5 | 8 | import android.app.Application; |
6 | 9 | import android.os.Bundle; |
|
14 | 17 | import com.facebook.react.bridge.ReactMethod; |
15 | 18 | import com.facebook.react.bridge.ReadableArray; |
16 | 19 | import com.facebook.react.bridge.ReadableMap; |
| 20 | +import com.facebook.react.bridge.ReadableNativeMap; |
17 | 21 | import com.facebook.react.bridge.WritableMap; |
18 | 22 |
|
19 | 23 | import org.json.JSONObject; |
20 | 24 |
|
| 25 | +import java.util.HashMap; |
21 | 26 | import java.util.HashSet; |
22 | 27 | import java.util.Set; |
| 28 | +import java.lang.*; |
23 | 29 |
|
24 | 30 | import cn.jiguang.plugins.push.common.JConstants; |
25 | 31 | import cn.jiguang.plugins.push.common.JLogger; |
26 | 32 | import cn.jiguang.plugins.push.helper.JPushHelper; |
27 | 33 | import cn.jiguang.plugins.push.receiver.JPushBroadcastReceiver; |
28 | 34 | import cn.jpush.android.api.BasicPushNotificationBuilder; |
29 | 35 | import cn.jpush.android.api.JPushInterface; |
| 36 | +import cn.jpush.android.data.JPushCollectControl; |
30 | 37 | import cn.jpush.android.data.JPushLocalNotification; |
31 | 38 |
|
32 | 39 | public class JPushModule extends ReactContextBaseJavaModule { |
@@ -59,6 +66,7 @@ public void init() { |
59 | 66 | JPushHelper.sendEvent(JConstants.NOTIFICATION_EVENT, writableMap); |
60 | 67 | JPushBroadcastReceiver.NOTIFICATION_BUNDLE = null; |
61 | 68 | } |
| 69 | + JPushInterface.setNotificationCallBackEnable(reactContext,true); |
62 | 70 | } |
63 | 71 |
|
64 | 72 | @ReactMethod |
@@ -95,6 +103,92 @@ public void setChannel(ReadableMap readableMap) { |
95 | 103 | } |
96 | 104 | } |
97 | 105 | @ReactMethod |
| 106 | + public void setChannelAndSound(ReadableMap readableMap) { |
| 107 | + if (readableMap == null) { |
| 108 | + JLogger.w(JConstants.PARAMS_NULL); |
| 109 | + return; |
| 110 | + } |
| 111 | + String channel = readableMap.getString(JConstants.CHANNEL); |
| 112 | + String sound = readableMap.getString(JConstants.SOUND); |
| 113 | + String channelId = readableMap.getString(JConstants.CHANNELID); |
| 114 | + try { |
| 115 | + NotificationManager manager= (NotificationManager) reactContext.getSystemService("notification"); |
| 116 | + if(Build.VERSION.SDK_INT<26){ |
| 117 | + return; |
| 118 | + } |
| 119 | + if(TextUtils.isEmpty(channel)||TextUtils.isEmpty(channelId)){ |
| 120 | + return; |
| 121 | + } |
| 122 | + NotificationChannel channel1=new NotificationChannel(channelId,channel, NotificationManager.IMPORTANCE_HIGH); |
| 123 | + if(!TextUtils.isEmpty(sound)){ |
| 124 | + channel1.setSound(Uri.parse("android.resource://"+reactContext.getPackageName()+"/raw/"+sound),null); |
| 125 | + } |
| 126 | + manager.createNotificationChannel(channel1); |
| 127 | + JPushInterface.setChannel(reactContext,channel); |
| 128 | + }catch (Throwable throwable){ |
| 129 | + } |
| 130 | + } |
| 131 | + @ReactMethod |
| 132 | + public void setLinkMergeEnable(boolean enable) { |
| 133 | + JPushInterface.setLinkMergeEnable(reactContext, enable); |
| 134 | + } |
| 135 | + |
| 136 | + @ReactMethod |
| 137 | + public void setSmartPushEnable(boolean enable) { |
| 138 | + JPushInterface.setSmartPushEnable(reactContext, enable); |
| 139 | + } |
| 140 | + |
| 141 | + @ReactMethod |
| 142 | + public void setDataInsightsEnable(boolean enable) { |
| 143 | + JPushInterface.setDataInsightsEnable(reactContext, enable); |
| 144 | + } |
| 145 | + |
| 146 | + @ReactMethod |
| 147 | + public void setGeofenceEnable(boolean enable) { |
| 148 | + JPushInterface.setGeofenceEnable(reactContext, enable); |
| 149 | + } |
| 150 | + |
| 151 | + @ReactMethod |
| 152 | + public void setCollectControl(ReadableMap readableMap) { |
| 153 | + if (readableMap == null) { |
| 154 | + JLogger.w(JConstants.PARAMS_NULL); |
| 155 | + return; |
| 156 | + } |
| 157 | + boolean hadValue = false; |
| 158 | + JPushCollectControl.Builder builder = new JPushCollectControl.Builder(); |
| 159 | + if (readableMap.hasKey(JConstants.IMEI)) { |
| 160 | + hadValue = true; |
| 161 | + builder.imei(readableMap.getBoolean(JConstants.IMEI)); |
| 162 | + } |
| 163 | + if (readableMap.hasKey(JConstants.IMSI)) { |
| 164 | + hadValue = true; |
| 165 | + builder.imsi(readableMap.getBoolean(JConstants.IMSI)); |
| 166 | + } |
| 167 | + if (readableMap.hasKey(JConstants.MAC)) { |
| 168 | + hadValue = true; |
| 169 | + builder.mac(readableMap.getBoolean(JConstants.MAC)); |
| 170 | + } |
| 171 | + if (readableMap.hasKey(JConstants.WIFI)) { |
| 172 | + hadValue = true; |
| 173 | + builder.wifi(readableMap.getBoolean(JConstants.WIFI)); |
| 174 | + } |
| 175 | + if (readableMap.hasKey(JConstants.BSSID)) { |
| 176 | + hadValue = true; |
| 177 | + builder.bssid(readableMap.getBoolean(JConstants.BSSID)); |
| 178 | + } |
| 179 | + if (readableMap.hasKey(JConstants.SSID)) { |
| 180 | + hadValue = true; |
| 181 | + builder.ssid(readableMap.getBoolean(JConstants.SSID)); |
| 182 | + } |
| 183 | + if (readableMap.hasKey(JConstants.CELL)) { |
| 184 | + hadValue = true; |
| 185 | + builder.cell(readableMap.getBoolean(JConstants.CELL)); |
| 186 | + } |
| 187 | + if (hadValue) { |
| 188 | + JPushInterface.setCollectControl(reactContext, builder.build()); |
| 189 | + } |
| 190 | + } |
| 191 | + @ReactMethod |
98 | 192 | public void setBadgeNumber(ReadableMap readableMap) { |
99 | 193 | if (readableMap == null) { |
100 | 194 | JLogger.w(JConstants.PARAMS_NULL); |
@@ -213,7 +307,48 @@ public void filterValidTags(ReadableMap readableMap, Callback callback) { |
213 | 307 | JLogger.w("there are no " + JConstants.TAGS); |
214 | 308 | } |
215 | 309 | } |
| 310 | + @ReactMethod |
| 311 | + public void setProperties(ReadableMap readableMap) { |
| 312 | + if (readableMap == null) { |
| 313 | + JLogger.w(JConstants.PARAMS_NULL); |
| 314 | + return; |
| 315 | + } |
| 316 | + if (readableMap.hasKey(JConstants.PROPERTIES)) { |
| 317 | + int sequence = readableMap.getInt(JConstants.SEQUENCE); |
| 318 | + ReadableMap readMap = readableMap.getMap(JConstants.PROPERTIES); |
| 319 | + ReadableNativeMap map= (ReadableNativeMap) readMap; |
| 320 | + HashMap properties=map.toHashMap(); |
| 321 | + JPushInterface.setProperties(reactContext,sequence,properties); |
| 322 | + } else { |
| 323 | + JLogger.w("there are no " + JConstants.PROPERTIES); |
| 324 | + } |
| 325 | + } |
| 326 | + @ReactMethod |
| 327 | + public void deleteProperties(ReadableMap readableMap) { |
| 328 | + if (readableMap == null) { |
| 329 | + JLogger.w(JConstants.PARAMS_NULL); |
| 330 | + return; |
| 331 | + } |
| 332 | + if (readableMap.hasKey(JConstants.PROPERTIES)) { |
| 333 | + int sequence = readableMap.getInt(JConstants.SEQUENCE); |
| 334 | + ReadableMap readMap = readableMap.getMap(JConstants.PROPERTIES); |
| 335 | + ReadableNativeMap map= (ReadableNativeMap) readMap; |
| 336 | + HashMap properties=map.toHashMap(); |
| 337 | + JPushInterface.deleteProperties(reactContext,sequence,properties); |
| 338 | + } else { |
| 339 | + JLogger.w("there are no " + JConstants.PROPERTIES); |
| 340 | + } |
216 | 341 |
|
| 342 | + } |
| 343 | + @ReactMethod |
| 344 | + public void cleanProperties(ReadableMap readableMap) { |
| 345 | + if (readableMap == null) { |
| 346 | + JLogger.w(JConstants.PARAMS_NULL); |
| 347 | + return; |
| 348 | + } |
| 349 | + int sequence = readableMap.getInt(JConstants.SEQUENCE); |
| 350 | + JPushInterface.cleanProperties(reactContext,sequence); |
| 351 | + } |
217 | 352 | @ReactMethod |
218 | 353 | public void setTags(ReadableMap readableMap) { |
219 | 354 | if (readableMap == null) { |
@@ -390,15 +525,61 @@ public void addLocalNotification(ReadableMap readableMap) { |
390 | 525 | int id = Integer.valueOf(notificationID); |
391 | 526 | String notificationTitle = readableMap.hasKey(JConstants.TITLE) ? readableMap.getString(JConstants.TITLE) : reactContext.getPackageName(); |
392 | 527 | String notificationContent = readableMap.hasKey(JConstants.CONTENT) ? readableMap.getString(JConstants.CONTENT) : reactContext.getPackageName(); |
| 528 | + String broadcastTime = readableMap.hasKey(JConstants.BROADCAST_TIME) ? readableMap.getString(JConstants.BROADCAST_TIME) : "0"; |
393 | 529 | JPushLocalNotification notification = new JPushLocalNotification(); |
394 | 530 | notification.setNotificationId(id); |
395 | 531 | notification.setTitle(notificationTitle); |
396 | 532 | notification.setContent(notificationContent); |
| 533 | + try { |
| 534 | + notification.setBroadcastTime(Long.parseLong(broadcastTime)); |
| 535 | + }catch (Throwable throwable){ |
| 536 | + } |
397 | 537 | if (readableMap.hasKey(JConstants.EXTRAS)) { |
398 | 538 | ReadableMap notificationExtra = readableMap.getMap(JConstants.EXTRAS); |
399 | 539 | JSONObject notificationExtraJson = new JSONObject(notificationExtra.toHashMap()); |
400 | 540 | notification.setExtras(notificationExtraJson.toString()); |
401 | 541 | } |
| 542 | + |
| 543 | + // 设置BuilderId |
| 544 | + if (readableMap.hasKey(JConstants.BUILDER_NAME)) { |
| 545 | + try { |
| 546 | + String layoutFileName = readableMap.getString(JConstants.BUILDER_NAME); |
| 547 | + if (!TextUtils.isEmpty(layoutFileName)) { |
| 548 | + // 通过布局文件名获取资源ID |
| 549 | + int builderId = reactContext.getResources().getIdentifier( |
| 550 | + layoutFileName, |
| 551 | + "layout", |
| 552 | + reactContext.getPackageName() |
| 553 | + ); |
| 554 | + if (builderId != 0) { |
| 555 | + notification.setBuilderId(builderId); |
| 556 | + } else { |
| 557 | + JLogger.w("Layout file not found: " + layoutFileName); |
| 558 | + } |
| 559 | + } |
| 560 | + } catch (Exception e) { |
| 561 | + JLogger.w("Failed to set BuilderId: " + e.getMessage()); |
| 562 | + } |
| 563 | + } |
| 564 | + |
| 565 | + // 设置Category |
| 566 | + if (readableMap.hasKey(JConstants.CATEGORY)) { |
| 567 | + String category = readableMap.getString(JConstants.CATEGORY); |
| 568 | + if (!TextUtils.isEmpty(category)) { |
| 569 | + notification.setCategory(category); |
| 570 | + } |
| 571 | + } |
| 572 | + |
| 573 | + // 设置Priority |
| 574 | + if (readableMap.hasKey(JConstants.PRIORITY)) { |
| 575 | + try { |
| 576 | + int priority = readableMap.getInt(JConstants.PRIORITY); |
| 577 | + notification.setPriority(priority); |
| 578 | + } catch (Exception e) { |
| 579 | + JLogger.w("Priority must be a number"); |
| 580 | + } |
| 581 | + } |
| 582 | + |
402 | 583 | JPushInterface.addLocalNotification(reactContext, notification); |
403 | 584 | } |
404 | 585 |
|
|
0 commit comments