|
| 1 | +package cn.jiguang.plugins.push.helper; |
| 2 | + |
| 3 | +import android.os.Bundle; |
| 4 | +import android.text.TextUtils; |
| 5 | + |
| 6 | +import com.facebook.react.bridge.Arguments; |
| 7 | +import com.facebook.react.bridge.WritableArray; |
| 8 | +import com.facebook.react.bridge.WritableMap; |
| 9 | +import com.facebook.react.modules.core.DeviceEventManagerModule; |
| 10 | + |
| 11 | +import org.json.JSONObject; |
| 12 | + |
| 13 | +import java.util.Iterator; |
| 14 | +import java.util.Set; |
| 15 | + |
| 16 | +import cn.jiguang.plugins.push.JPushModule; |
| 17 | +import cn.jiguang.plugins.push.common.JPushConstans; |
| 18 | +import cn.jiguang.plugins.push.common.JPushLogger; |
| 19 | +import cn.jpush.android.api.CustomMessage; |
| 20 | +import cn.jpush.android.api.JPushMessage; |
| 21 | +import cn.jpush.android.api.NotificationMessage; |
| 22 | + |
| 23 | +public class JPushHelper { |
| 24 | + |
| 25 | + public static void sendEvent(String eventName, WritableMap params) { |
| 26 | + try { |
| 27 | + JPushModule.reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, params); |
| 28 | + }catch (Throwable throwable){ |
| 29 | + JPushLogger.e("sendEvent error:"+throwable.getMessage()); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + public static WritableMap convertNotificationToMap(String eventType, NotificationMessage message) { |
| 34 | + WritableMap writableMap = Arguments.createMap(); |
| 35 | + writableMap.putString(JPushConstans.NOTIFICATION_EVENT_TYPE, eventType); |
| 36 | + writableMap.putString(JPushConstans.MESSAGE_ID, message.msgId); |
| 37 | + writableMap.putString(JPushConstans.TITLE, message.notificationTitle); |
| 38 | + writableMap.putString(JPushConstans.CONTENT, message.notificationContent); |
| 39 | + convertExtras(message.notificationExtras, writableMap); |
| 40 | + return writableMap; |
| 41 | + } |
| 42 | + |
| 43 | + public static WritableMap convertNotificationBundleToMap(String eventType, Bundle bundle) { |
| 44 | + WritableMap writableMap = Arguments.createMap(); |
| 45 | + writableMap.putString(JPushConstans.NOTIFICATION_EVENT_TYPE, eventType); |
| 46 | + writableMap.putString(JPushConstans.MESSAGE_ID, bundle.getString("cn.jpush.android.MSG_ID","")); |
| 47 | + writableMap.putString(JPushConstans.TITLE, bundle.getString("cn.jpush.android.NOTIFICATION_CONTENT_TITLE","")); |
| 48 | + writableMap.putString(JPushConstans.CONTENT, bundle.getString("cn.jpush.android.ALERT","")); |
| 49 | + convertExtras(bundle.getString("cn.jpush.android.EXTRA",""), writableMap); |
| 50 | + return writableMap; |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | + public static WritableMap convertCustomMessage(CustomMessage customMessage) { |
| 55 | + WritableMap writableMap = Arguments.createMap(); |
| 56 | + writableMap.putString(JPushConstans.MESSAGE_ID, customMessage.messageId); |
| 57 | + writableMap.putString(JPushConstans.CONTENT, customMessage.message); |
| 58 | + convertExtras(customMessage.extra, writableMap); |
| 59 | + return writableMap; |
| 60 | + } |
| 61 | + |
| 62 | + public static WritableMap convertJPushMessageToMap(int type, JPushMessage message) { |
| 63 | + WritableMap writableMap = Arguments.createMap(); |
| 64 | + Set<String> tags = message.getTags(); |
| 65 | + WritableArray tagsArray = Arguments.createArray(); |
| 66 | + for (String tag : tags) { |
| 67 | + tagsArray.pushString(tag); |
| 68 | + } |
| 69 | + writableMap.putInt(JPushConstans.CODE, message.getErrorCode()); |
| 70 | + writableMap.putInt(JPushConstans.SEQUENCE, message.getSequence()); |
| 71 | + switch (type) { |
| 72 | + case 1: |
| 73 | + writableMap.putArray(JPushConstans.TAGS, tagsArray); |
| 74 | + break; |
| 75 | + case 2: |
| 76 | + writableMap.putBoolean(JPushConstans.TAG_ENABLE, message.getTagCheckStateResult()); |
| 77 | + writableMap.putString(JPushConstans.TAG, message.getCheckTag()); |
| 78 | + break; |
| 79 | + case 3: |
| 80 | + writableMap.putString(JPushConstans.ALIAS, message.getAlias()); |
| 81 | + break; |
| 82 | + } |
| 83 | + return writableMap; |
| 84 | + } |
| 85 | + |
| 86 | + public static void convertExtras(String extras, WritableMap writableMap) { |
| 87 | + if (TextUtils.isEmpty(extras) || extras.equals("{}")) return; |
| 88 | + try { |
| 89 | + WritableMap extrasMap = Arguments.createMap(); |
| 90 | + JSONObject jsonObject = new JSONObject(extras); |
| 91 | + Iterator<String> it = jsonObject.keys(); |
| 92 | + while (it.hasNext()) { |
| 93 | + String key = it.next(); |
| 94 | + String value = jsonObject.getString(key); |
| 95 | + extrasMap.putString(key, value); |
| 96 | + } |
| 97 | + writableMap.putMap(JPushConstans.EXTRAS, extrasMap); |
| 98 | + } catch (Throwable throwable) { |
| 99 | + JPushLogger.w("convertExtras error:" + throwable.getMessage()); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments