react-native-acoustic-mobile-push-inbox-beta 3.9.37 → 3.9.39

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright © 2019, 2023 Acoustic, L.P. All rights reserved.
2
+ * Copyright © 2019, 2025 Acoustic, L.P. All rights reserved.
3
3
  *
4
4
  * NOTICE: This file contains material that is confidential and proprietary to
5
5
  * Acoustic, L.P. and/or other developers. No license is granted under any intellectual or
@@ -10,10 +10,6 @@
10
10
 
11
11
  package co.acoustic.mobile.push.plugin.inbox;
12
12
 
13
- import com.facebook.react.ReactApplication;
14
- import com.facebook.react.ReactInstanceManager;
15
- import com.facebook.react.ReactNativeHost;
16
- import com.facebook.react.ReactRootView;
17
13
  import com.facebook.react.bridge.LifecycleEventListener;
18
14
  import com.facebook.react.bridge.ReactApplicationContext;
19
15
  import com.facebook.react.bridge.ReactContextBaseJavaModule;
@@ -21,21 +17,13 @@ import com.facebook.react.bridge.ReactMethod;
21
17
  import com.facebook.react.bridge.Callback;
22
18
 
23
19
  import android.app.Activity;
24
- import android.content.Context;
25
- import android.content.Intent;
26
- import android.os.Bundle;
27
- import android.os.Build;
28
20
  import android.view.ViewGroup;
29
21
  import android.view.ViewParent;
30
- import android.view.Window;
31
- import android.widget.FrameLayout;
32
22
  import android.widget.RelativeLayout;
33
-
34
23
  import com.facebook.react.bridge.ReadableArray;
35
24
  import com.facebook.react.bridge.ReadableMap;
36
25
  import com.facebook.react.bridge.ReadableMapKeySetIterator;
37
26
  import com.facebook.react.bridge.ReadableType;
38
- import com.facebook.react.bridge.WritableArray;
39
27
  import com.facebook.react.bridge.WritableMap;
40
28
  import com.facebook.react.bridge.WritableNativeArray;
41
29
  import com.facebook.react.bridge.WritableNativeMap;
@@ -49,574 +37,395 @@ import co.acoustic.mobile.push.sdk.api.attribute.StringAttribute;
49
37
  import co.acoustic.mobile.push.sdk.api.event.Event;
50
38
  import co.acoustic.mobile.push.sdk.api.notification.MceNotificationAction;
51
39
  import co.acoustic.mobile.push.sdk.api.notification.MceNotificationActionRegistry;
52
- import co.acoustic.mobile.push.sdk.api.notification.NotificationDetails;
53
40
  import co.acoustic.mobile.push.sdk.notification.MceNotificationActionImpl;
54
- import co.acoustic.mobile.push.sdk.plugin.inbox.InboxMessageReference;
55
41
  import co.acoustic.mobile.push.sdk.plugin.inbox.InboxMessagesClient;
56
42
  import co.acoustic.mobile.push.sdk.plugin.inbox.RichContent;
57
43
  import co.acoustic.mobile.push.sdk.plugin.inbox.RichContentDatabaseHelper;
58
- import co.acoustic.mobile.push.sdk.notification.ActionImpl;
59
44
  import co.acoustic.mobile.push.sdk.util.Logger;
60
45
  import co.acoustic.mobile.push.sdk.api.message.MessageSync;
61
- import co.acoustic.mobile.push.sdk.plugin.inbox.InboxMessageProcessor;
62
46
  import co.acoustic.mobile.push.sdk.plugin.inbox.InboxEvents;
63
- import co.acoustic.mobile.push.sdk.api.message.MessageProcessor;
64
47
 
65
48
  import org.json.JSONArray;
66
49
  import org.json.JSONException;
67
50
  import org.json.JSONObject;
68
-
69
- import java.util.ArrayList;
70
51
  import java.util.Date;
71
52
  import java.util.HashMap;
72
53
  import java.util.Iterator;
73
54
  import java.util.LinkedList;
74
55
  import java.util.List;
75
- import java.util.Map;
76
56
 
77
57
  import static com.facebook.react.bridge.ReadableType.*;
78
- import static java.lang.Thread.sleep;
79
58
 
80
59
  public class RNAcousticMobilePushInboxModule extends ReactContextBaseJavaModule implements LifecycleEventListener {
81
- private static final String TYPE = "openInboxMessage";
82
- private static String TAG = "RNAcousticMobilePushInboxModule";
83
- private static ReactApplicationContext reactContext;
84
- private static String inboxActionModule;
85
- RelativeLayout relativeLayout = null;
86
-
87
- // Send event to javascript
88
- static protected void sendEvent(String eventName, WritableMap params) {
89
- reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, params);
90
- }
91
-
92
- public RNAcousticMobilePushInboxModule(ReactApplicationContext reactContext) {
93
- super(reactContext);
94
- RNAcousticMobilePushInboxModule.reactContext = reactContext;
95
- reactContext.addLifecycleEventListener(this);
96
- }
97
-
98
- @Override
99
- public String getName() {
100
- return "RNAcousticMobilePushInbox";
101
- }
102
-
103
- @ReactMethod
104
- public void registerInboxComponent(String component) {
105
- inboxActionModule = component;
106
- registerInboxComponent();
107
- }
108
-
109
- void registerInboxComponent() {
110
- MceNotificationActionRegistry.registerNotificationAction(reactContext, TYPE, new MceNotificationAction() {
111
-
112
- @Override
113
- public void handleAction(final Context context, final String type, final String name, final String attribution, final String mailingId, final Map<String, String> payload, final boolean fromNotification) {
114
-
115
- Activity currentActivity = getCurrentActivity();
116
- if(currentActivity == null) {
117
- Logger.i(TAG, "Can't find activity, starting");
118
-
119
- if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
120
- Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
121
- context.sendBroadcast(it);
122
- }
123
-
124
- Intent intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
125
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
126
- context.startActivity(intent);
127
- return;
128
- }
129
-
130
- final Activity activity = currentActivity;
131
- final InboxMessageReference messageReference = new InboxMessageReference(payload.get("value"), payload.get(InboxMessageReference.INBOX_MESSAGE_ID_KEY));
132
- if (messageReference.hasReference()) {
133
- final RichContent inboxMessage = messageReference.getMessageFromDb(reactContext);
134
- if (inboxMessage == null) {
135
- Logger.d(TAG, "Inbox message not found");
136
- InboxMessageProcessor.addMessageToLoad(messageReference);
137
- MessageSync.syncMessages(reactContext, new OperationCallback<MessageSync.SyncReport>() {
138
- @Override
139
- public void onSuccess(MessageSync.SyncReport syncReport, OperationResult result) {
140
- Logger.i(TAG, "Downloaded messages");
141
- InboxMessageProcessor.Report report = null;
142
- for(MessageProcessor.ProcessReport processReport: syncReport.getReports()) {
143
- if(processReport instanceof InboxMessageProcessor.Report) {
144
- report = (InboxMessageProcessor.Report)processReport;
145
- }
146
- }
147
- for(int i=0; i<report.getNewMessages().size(); i++) {
148
- RichContent message = report.getNewMessages().get(i);
149
- if(message.getMessageId().equals(messageReference.getInboxMessageId())) {
150
- Logger.i(TAG, "Downloaded message");
151
- final RichContent msg = message;
152
- activity.runOnUiThread(new Runnable() {
153
- @Override
154
- public void run() {
155
- internalHideInbox();
156
-
157
- showInboxMessage(msg, activity);
158
- }
159
- });
160
- if (fromNotification) {
161
- InboxEvents.sendInboxNotificationOpenedEvent(context, new ActionImpl(type, name, payload), attribution, mailingId);
162
- }
163
- return;
164
- }
165
- }
166
- Logger.e(TAG, "Could not find downloaded message");
167
- }
168
-
169
- @Override
170
- public void onFailure(MessageSync.SyncReport syncReport, OperationResult result) {
171
- Logger.e(TAG, "Could not download message");
172
- }
173
- });
174
- } else {
175
- activity.runOnUiThread(new Runnable() {
176
- @Override
177
- public void run() {
178
- internalHideInbox();
179
- showInboxMessage(inboxMessage, activity);
180
- }
181
- });
182
- if (fromNotification) {
183
- InboxEvents.sendInboxNotificationOpenedEvent(context, new ActionImpl(type, name, payload), attribution, mailingId);
184
- }
185
- }
186
- }
187
- }
188
-
189
- @Override
190
- public void init(Context context, JSONObject jsonObject) {
191
-
192
- }
193
-
194
- @Override
195
- public void update(Context context, JSONObject jsonObject) {
196
-
197
- }
198
-
199
- @Override
200
- public boolean shouldDisplayNotification(Context context, NotificationDetails notificationDetails, Bundle bundle) {
201
- return true;
202
- }
203
-
204
- @Override
205
- public boolean shouldSendDefaultEvent(Context context) {
206
- return false;
207
- }
208
- });
209
- }
210
-
211
- @ReactMethod
212
- public void hideInbox() {
213
- final Activity activity = getCurrentActivity();
214
- if(activity == null) {
215
- Logger.e(TAG, "Can't find activity");
216
- return;
217
- }
218
-
219
- activity.runOnUiThread(new Runnable() {
220
- @Override
221
- public void run() {
222
- internalHideInbox();
223
- }
224
- });
225
- }
226
-
227
-
228
- // Needs to be run on the main thread.
229
- private void internalHideInbox() {
230
- if(this.relativeLayout != null) {
231
- ViewParent parent = this.relativeLayout.getParent();
232
- if(parent instanceof ViewGroup) {
233
- ViewGroup group = (ViewGroup) parent;
234
- group.removeView(this.relativeLayout);
235
- } else {
236
- Logger.e(TAG, "InApp Parent is not a ViewGroup!");
237
- }
238
- }
239
- this.relativeLayout = null;
240
- }
241
-
242
- // Needs to be run on the main thread.
243
- private void showInboxMessage(RichContent inboxMessage, Activity activity) {
244
- if(inboxActionModule==null) {
245
- Logger.e(TAG, "inbox action module is not registered");
246
- return;
247
- }
248
-
249
- this.relativeLayout = new RelativeLayout(reactContext);
250
- relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT));
251
- RelativeLayout.LayoutParams viewLayout = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
252
-
253
- viewLayout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
254
-
255
- ReactApplication application = (ReactApplication) activity.getApplication();
256
- ReactNativeHost reactNativeHost = application.getReactNativeHost();
257
- ReactInstanceManager reactInstanceManager = reactNativeHost.getReactInstanceManager();
258
-
259
- Bundle messageBundle = new Bundle();
260
- messageBundle.putLong("sendDate", inboxMessage.getSendDate().getTime());
261
- messageBundle.putLong("expirationDate", inboxMessage.getExpirationDate().getTime());
262
- messageBundle.putBoolean("isDeleted", inboxMessage.getIsDeleted());
263
- messageBundle.putBoolean("isRead", inboxMessage.getIsRead());
264
- messageBundle.putBoolean("isExpired", inboxMessage.getIsExpired());
265
- messageBundle.putString("templateName", inboxMessage.getTemplate());
266
- messageBundle.putString("attribution", inboxMessage.getAttribution());
267
- messageBundle.putString("mailingId", inboxMessage.getMessageId());
268
- messageBundle.putString("inboxMessageId", inboxMessage.getMessageId());
269
- messageBundle.putString("richContentId", inboxMessage.getContentId());
270
-
271
- try {
272
- JSONObject content = inboxMessage.getContent();
273
- if (content != null) {
274
- messageBundle.putBundle("content", convertJsonObjectToBundle(content));
275
- }
276
- } catch (Exception ex) {
277
- Logger.d(TAG, "Couldn't convert inbox json content", ex);
278
- }
279
-
280
- Bundle initialProperties = new Bundle();
281
- initialProperties.putBundle("message", messageBundle);
282
-
283
- ReactRootView reactRootView = new ReactRootView(reactContext);
284
- reactRootView.setLayoutParams(viewLayout);
285
- reactRootView.startReactApplication(reactInstanceManager, inboxActionModule, initialProperties);
286
- relativeLayout.addView(reactRootView);
287
-
288
- Window window = activity.getWindow();
289
- FrameLayout.LayoutParams relativeLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.FILL_PARENT );
290
- window.addContentView(relativeLayout, relativeLayoutParams);
291
- }
292
-
293
- @ReactMethod
294
- public void inboxMessageCount(Callback callback) {
295
- RichContentDatabaseHelper.MessageCursor messageCursor = RichContentDatabaseHelper.getRichContentDatabaseHelper(reactContext).getMessages();
296
- int messages = 0;
297
- int unread = 0;
298
- while(messageCursor.moveToNext())
299
- {
300
- RichContent message = messageCursor.getRichContent();
301
- messages++;
302
- if(!message.getIsRead()) {
303
- unread++;
304
- }
305
- }
306
-
307
- WritableNativeMap map = new WritableNativeMap();
308
- map.putInt("messages", messages);
309
- map.putInt("unread", unread);
310
- callback.invoke(map);
311
- }
312
-
313
- @ReactMethod
314
- public void deleteInboxMessage(String inboxMessageId) {
315
- InboxMessagesClient.deleteMessageById(reactContext, inboxMessageId);
316
- }
317
-
318
- @ReactMethod
319
- public void readInboxMessage(String inboxMessageId) {
60
+ private static final String TYPE = "openInboxMessage";
61
+ private static String TAG = "RNAcousticMobilePushInboxModule";
62
+
63
+ private ReactApplicationContext reactContext;
64
+ private static String inboxActionModule;
65
+ private MceNotificationAction inboxCustomAction;
66
+
67
+ public static RelativeLayout relativeLayout = null;
68
+
69
+ // Send event to javascript
70
+ protected void sendEvent(String eventName, WritableMap params) {
71
+ reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, params);
72
+ }
73
+
74
+ public RNAcousticMobilePushInboxModule() {}
75
+
76
+ public RNAcousticMobilePushInboxModule(ReactApplicationContext reactContext) {
77
+ super(reactContext);
78
+ this.reactContext = reactContext;
79
+ reactContext.addLifecycleEventListener(this);
80
+
81
+ registerInboxComponent();
82
+ }
83
+
84
+ @Override
85
+ public String getName() {
86
+ return "RNAcousticMobilePushInbox";
87
+ }
88
+
89
+ @ReactMethod
90
+ public void registerInboxComponent(String component) {
91
+ inboxActionModule = component;
92
+ registerInboxComponent();
93
+ }
94
+
95
+ private void registerInboxComponent() {
96
+ // Create an instance of the custom action handler
97
+ // if (inboxActionModule == null) {
98
+ inboxCustomAction = new RNPushNotificationInboxCustomAction(reactContext, inboxActionModule);
99
+ // }
100
+
101
+ /**
102
+ * A custom action handler for RNPushNotificationInbox, responsible for processing inbox messages and navigating to the inbox screen when a push notification is clicked.
103
+ */
104
+ MceNotificationActionRegistry.registerNotificationAction(reactContext, TYPE, inboxCustomAction);
105
+ }
106
+
107
+
108
+ @ReactMethod
109
+ public void inboxMessageCount(Callback callback) {
110
+ RichContentDatabaseHelper.MessageCursor messageCursor = RichContentDatabaseHelper.getRichContentDatabaseHelper(reactContext).getMessages();
111
+ int messages = 0;
112
+ int unread = 0;
113
+ while (messageCursor.moveToNext()) {
114
+ RichContent message = messageCursor.getRichContent();
115
+ messages++;
116
+ if (!message.getIsRead()) {
117
+ unread++;
118
+ }
119
+ }
120
+
121
+ WritableNativeMap map = new WritableNativeMap();
122
+ map.putInt("messages", messages);
123
+ map.putInt("unread", unread);
124
+ callback.invoke(map);
125
+ }
126
+
127
+ @ReactMethod
128
+ public void deleteInboxMessage(String inboxMessageId) {
129
+ InboxMessagesClient.deleteMessageById(reactContext, inboxMessageId);
130
+ }
131
+
132
+ @ReactMethod
133
+ public void readInboxMessage(String inboxMessageId) {
320
134
  RichContentDatabaseHelper.MessageCursor messageCursor = RichContentDatabaseHelper.getRichContentDatabaseHelper(reactContext).getMessagesByMessageId(inboxMessageId);
321
- messageCursor.moveToFirst();
322
- RichContent message = messageCursor.getRichContent();
135
+ messageCursor.moveToFirst();
136
+ RichContent message = messageCursor.getRichContent();
323
137
  InboxEvents.sendInboxMessageOpenedEvent(reactContext, message);
324
- InboxMessagesClient.setMessageReadById(reactContext, inboxMessageId);
325
- }
326
-
327
- @ReactMethod
328
- public void syncInboxMessages() {
329
- MessageSync.syncMessages(reactContext, new OperationCallback<MessageSync.SyncReport>() {
330
- @Override
331
- public void onSuccess(MessageSync.SyncReport syncReport, OperationResult result) {
332
- sendEvent("SyncInbox", null);
333
- }
334
-
335
- @Override
336
- public void onFailure(final MessageSync.SyncReport syncReport, final OperationResult result) {
337
- sendEvent("SyncInbox", null);
338
- }
339
- });
340
- }
341
-
342
- @ReactMethod
343
- public void listInboxMessages(boolean direction, Callback callback) {
344
- WritableNativeArray messages = new WritableNativeArray();
345
- RichContentDatabaseHelper.MessageCursor messageCursor = RichContentDatabaseHelper.getRichContentDatabaseHelper(reactContext).getMessages();
346
- while(messageCursor.moveToNext()) {
347
- RichContent message = messageCursor.getRichContent();
348
- WritableMap messageMap = new WritableNativeMap();
349
- messageMap.putString("inboxMessageId", message.getMessageId());
350
- messageMap.putString("richContentId", message.getContentId());
351
- messageMap.putString("templateName", message.getTemplate());
352
- messageMap.putString("attribution", message.getAttribution());
353
- messageMap.putString("mailingId", message.getMessageId());
354
- messageMap.putDouble("sendDate", message.getSendDate().getTime());
355
- messageMap.putDouble("expirationDate", message.getExpirationDate().getTime());
356
- messageMap.putBoolean("isDeleted", message.getIsDeleted());
357
- messageMap.putBoolean("isRead", message.getIsRead());
358
- messageMap.putBoolean("isExpired", message.getIsExpired());
359
-
360
- try {
361
- JSONObject content = message.getContent();
362
- if (content != null) {
363
- messageMap.putMap("content", convertJsonObject(content) );
364
- }
365
- } catch (Exception ex) {
366
- Logger.d(TAG, "Couldn't convert inbox json content", ex);
367
- }
368
-
369
- messages.pushMap(messageMap);
370
- }
371
- callback.invoke(messages);
372
- }
373
-
374
- HashMap<String,String> convertFromMapToHash(ReadableMap map) {
375
- HashMap<String, String> hash = new HashMap<String, String>();
376
- ReadableMapKeySetIterator keys = map.keySetIterator();
377
- while(keys.hasNextKey()) {
378
- String key = keys.nextKey();
379
- ReadableType type = map.getType(key);
380
- switch(type) {
381
- case Array:
382
- try {
383
- JSONArray json = convertReadableArray(map.getArray(key));
384
- hash.put(key, json.toString() );
385
- } catch (JSONException e) {
386
- e.printStackTrace();
387
- }
388
- break;
389
- case Boolean:
390
- Boolean bool = new Boolean(map.getBoolean(key));
391
- hash.put(key, bool.toString());
392
- break;
393
- case Map:
394
- try {
395
- JSONObject json = convertReadableMap(map.getMap(key));
396
- hash.put(key, json.toString() );
397
- } catch (JSONException e) {
398
- e.printStackTrace();
399
- }
400
- break;
401
- case Null:
402
- hash.put(key, "<NULL>");
403
- break;
404
- case Number:
405
- hash.put(key, Double.toString( map.getDouble(key) ));
406
- break;
407
- case String:
408
- hash.put(key, map.getString(key));
409
- break;
410
- }
411
- }
412
- return hash;
413
- }
414
-
415
- @ReactMethod
416
- public void clickInboxAction(ReadableMap action, String inboxMessageId) {
417
- RichContentDatabaseHelper.MessageCursor messageCursor = RichContentDatabaseHelper.getRichContentDatabaseHelper(reactContext).getMessagesByMessageId(inboxMessageId);
418
- messageCursor.moveToFirst();
419
- RichContent message = messageCursor.getRichContent();
420
- String richContentId = message.getContentId();
421
- String attribution = message.getAttribution();
422
-
423
- // get richContentId and attribution
424
- // put in event
425
- String actionType = action.getString("type");
426
-
427
- MceNotificationAction actionImpl = MceNotificationActionRegistry.getNotificationAction(reactContext, actionType);
428
- if (actionImpl != null) {
429
- HashMap<String,String> payload = convertFromMapToHash(action);
430
-
431
- actionImpl.handleAction(reactContext, actionType, null, null, null, payload, false);
432
-
433
- List<Attribute> eventAttributes = new LinkedList<Attribute>();
434
- eventAttributes.add(new StringAttribute("richContentId", richContentId));
435
- eventAttributes.add(new StringAttribute("inboxMessageId", inboxMessageId));
436
- eventAttributes.add(new StringAttribute("actionTaken", actionType));
437
-
438
- String name = actionType;
439
- MceNotificationActionImpl.ClickEventDetails clickEventDetails = MceNotificationActionImpl.getClickEventDetails(actionType);
440
- if(clickEventDetails != null) {
441
- name = clickEventDetails.eventName;
442
- String value = payload.get("value");
443
- eventAttributes.add(new StringAttribute(clickEventDetails.valueName, value));
444
- } else {
445
- for(String key : payload.keySet()) {
446
- eventAttributes.add(new StringAttribute(key, payload.get(key)));
447
- }
448
- }
138
+ InboxMessagesClient.setMessageReadById(reactContext, inboxMessageId);
139
+ }
140
+
141
+ @ReactMethod
142
+ public void syncInboxMessages() {
143
+ MessageSync.syncMessages(reactContext, new OperationCallback<MessageSync.SyncReport>() {
144
+ @Override
145
+ public void onSuccess(MessageSync.SyncReport syncReport, OperationResult result) {
146
+ sendEvent("SyncInbox", null);
147
+ }
148
+
149
+ @Override
150
+ public void onFailure(final MessageSync.SyncReport syncReport, final OperationResult result) {
151
+ sendEvent("SyncInbox", null);
152
+ }
153
+ });
154
+ }
155
+
156
+ @ReactMethod
157
+ public void listInboxMessages(boolean direction, Callback callback) {
158
+ WritableNativeArray messages = new WritableNativeArray();
159
+ RichContentDatabaseHelper.MessageCursor messageCursor = RichContentDatabaseHelper.getRichContentDatabaseHelper(reactContext).getMessages();
160
+ while (messageCursor.moveToNext()) {
161
+ RichContent message = messageCursor.getRichContent();
162
+ WritableMap messageMap = new WritableNativeMap();
163
+ messageMap.putString("inboxMessageId", message.getMessageId());
164
+ messageMap.putString("richContentId", message.getContentId());
165
+ messageMap.putString("templateName", message.getTemplate());
166
+ messageMap.putString("attribution", message.getAttribution());
167
+ messageMap.putString("mailingId", message.getMessageId());
168
+ messageMap.putDouble("sendDate", message.getSendDate().getTime());
169
+ messageMap.putDouble("expirationDate", message.getExpirationDate().getTime());
170
+ messageMap.putBoolean("isDeleted", message.getIsDeleted());
171
+ messageMap.putBoolean("isRead", message.getIsRead());
172
+ messageMap.putBoolean("isExpired", message.getIsExpired());
173
+
174
+ try {
175
+ JSONObject content = message.getContent();
176
+ if (content != null) {
177
+ messageMap.putMap("content", convertJsonObject(content));
178
+ }
179
+ } catch (Exception ex) {
180
+ Logger.d(TAG, "Couldn't convert inbox json content", ex);
181
+ }
182
+
183
+ messages.pushMap(messageMap);
184
+ }
185
+ callback.invoke(messages);
186
+ }
187
+
188
+ HashMap<String, String> convertFromMapToHash(ReadableMap map) {
189
+ HashMap<String, String> hash = new HashMap<String, String>();
190
+ ReadableMapKeySetIterator keys = map.keySetIterator();
191
+ while (keys.hasNextKey()) {
192
+ String key = keys.nextKey();
193
+ ReadableType type = map.getType(key);
194
+ switch (type) {
195
+ case Array:
196
+ try {
197
+ JSONArray json = convertReadableArray(map.getArray(key));
198
+ hash.put(key, json.toString());
199
+ } catch (JSONException e) {
200
+ e.printStackTrace();
201
+ }
202
+ break;
203
+ case Boolean:
204
+ Boolean bool = new Boolean(map.getBoolean(key));
205
+ hash.put(key, bool.toString());
206
+ break;
207
+ case Map:
208
+ try {
209
+ JSONObject json = convertReadableMap(map.getMap(key));
210
+ hash.put(key, json.toString());
211
+ } catch (JSONException e) {
212
+ e.printStackTrace();
213
+ }
214
+ break;
215
+ case Null:
216
+ hash.put(key, "<NULL>");
217
+ break;
218
+ case Number:
219
+ hash.put(key, Double.toString(map.getDouble(key)));
220
+ break;
221
+ case String:
222
+ hash.put(key, map.getString(key));
223
+ break;
224
+ }
225
+ }
226
+ return hash;
227
+ }
228
+
229
+ @ReactMethod
230
+ public void clickInboxAction(ReadableMap action, String inboxMessageId) {
231
+ RichContentDatabaseHelper.MessageCursor messageCursor = RichContentDatabaseHelper.getRichContentDatabaseHelper(reactContext).getMessagesByMessageId(inboxMessageId);
232
+ messageCursor.moveToFirst();
233
+ RichContent message = messageCursor.getRichContent();
234
+ String richContentId = message.getContentId();
235
+ String attribution = message.getAttribution();
236
+
237
+ // get richContentId and attribution
238
+ // put in event
239
+ String actionType = action.getString("type");
240
+
241
+ MceNotificationAction actionImpl = MceNotificationActionRegistry.getNotificationAction(reactContext, actionType);
242
+ if (actionImpl != null) {
243
+ HashMap<String, String> payload = convertFromMapToHash(action);
244
+
245
+ actionImpl.handleAction(reactContext, actionType, null, null, null, payload, false);
246
+
247
+ List<Attribute> eventAttributes = new LinkedList<Attribute>();
248
+ eventAttributes.add(new StringAttribute("richContentId", richContentId));
249
+ eventAttributes.add(new StringAttribute("inboxMessageId", inboxMessageId));
250
+ eventAttributes.add(new StringAttribute("actionTaken", actionType));
251
+
252
+ String name = actionType;
253
+ MceNotificationActionImpl.ClickEventDetails clickEventDetails = MceNotificationActionImpl.getClickEventDetails(actionType);
254
+ if (clickEventDetails != null) {
255
+ name = clickEventDetails.eventName;
256
+ String value = payload.get("value");
257
+ eventAttributes.add(new StringAttribute(clickEventDetails.valueName, value));
258
+ } else {
259
+ for (String key : payload.keySet()) {
260
+ eventAttributes.add(new StringAttribute(key, payload.get(key)));
261
+ }
262
+ }
449
263
 
450
264
  Event event = new Event("inboxMessage", name, new Date(), eventAttributes, attribution, null);
451
265
 
452
- MceSdk.getEventsClient(false).sendEvent(reactContext, event, new OperationCallback<Event>() {
453
- @Override
454
- public void onSuccess(Event event, OperationResult result) {
455
-
456
- }
457
-
458
- @Override
459
- public void onFailure(Event event, OperationResult result) {
460
- MceSdk.getQueuedEventsClient().sendEvent(reactContext, event);
461
- }
462
- });
463
- }
464
- }
465
-
466
- public static JSONArray convertReadableArray(ReadableArray readableArray) throws JSONException {
467
- JSONArray jsonArray = new JSONArray();
468
- for(int i=0;i<readableArray.size();i++) {
469
- ReadableType type = readableArray.getType(i);
470
- if(type == String) {
471
- jsonArray.put( readableArray.getString(i) );
472
- } else if (type == Boolean) {
473
- jsonArray.put( readableArray.getBoolean(i) );
474
- } else if (type == Null) {
475
- jsonArray.put( JSONObject.NULL );
476
- } else if (type == Number) {
477
- jsonArray.put( readableArray.getDouble(i) );
478
- } else if (type == Map) {
479
- jsonArray.put( convertReadableMap( readableArray.getMap(i) ) );
480
- } else if (type == Array) {
481
- jsonArray.put( convertReadableArray( readableArray.getArray(i) ) );
482
- }
483
- }
484
- return jsonArray;
485
- }
486
-
487
- public static JSONObject convertReadableMap(ReadableMap readableMap) throws JSONException {
488
- JSONObject jsonObject = new JSONObject();
489
- ReadableMapKeySetIterator keys = readableMap.keySetIterator();
490
- while(keys.hasNextKey()) {
491
- String key = keys.nextKey();
492
- ReadableType type = readableMap.getType(key);
493
- if(type == String) {
494
- jsonObject.put(key, readableMap.getString(key) );
495
- } else if (type == Boolean) {
496
- jsonObject.put(key, readableMap.getBoolean(key) );
497
- } else if (type == Null) {
498
- jsonObject.put(key, JSONObject.NULL );
499
- } else if (type == Number) {
500
- jsonObject.put(key, readableMap.getDouble(key) );
501
- } else if (type == Map) {
502
- jsonObject.put(key, convertReadableMap( readableMap.getMap(key) ) );
503
- } else if (type == Array) {
504
- jsonObject.put(key, convertReadableArray( readableMap.getArray(key) ) );
505
- }
506
- }
507
- return jsonObject;
508
- }
509
-
510
- public static WritableNativeArray convertJsonArray(JSONArray jsonArray) throws JSONException {
511
- WritableNativeArray array = new WritableNativeArray();
512
- for (int i = 0; i < jsonArray.length(); i++) {
513
- Object value = jsonArray.get(i);
514
-
515
- if (value == null || value == JSONObject.NULL) {
516
- array.pushNull();
517
- } else if(value instanceof Boolean) {
518
- array.pushBoolean((Boolean) value);
519
- } else if(value instanceof Integer) {
520
- array.pushInt( (Integer) value);
521
- } else if(value instanceof Long) {
522
- Long longValue = (Long) value;
523
- array.pushDouble( longValue.doubleValue());
524
- } else if(value instanceof Double) {
525
- array.pushDouble( (Double) value);
526
- } else if(value instanceof String) {
527
- array.pushString( (String) value);
528
- } else if (value instanceof JSONObject) {
529
- array.pushMap( convertJsonObject( (JSONObject)value ));
530
- } else if(value instanceof JSONArray) {
531
- array.pushArray( convertJsonArray( (JSONArray)value ));
532
- } else {
533
- throw new IllegalArgumentException("Unsupported type: " + value.getClass());
534
- }
535
- }
536
- return array;
537
- }
538
-
539
- public static WritableNativeMap convertJsonObject(JSONObject jsonObject) throws JSONException {
540
- WritableNativeMap map = new WritableNativeMap();
541
- Iterator<String> jsonIterator = jsonObject.keys();
542
- while (jsonIterator.hasNext()) {
543
- String key = jsonIterator.next();
544
- Object value = jsonObject.get(key);
545
-
546
- if (value == null || value == JSONObject.NULL) {
547
- map.putNull(key);
548
- } else if(value instanceof Boolean) {
549
- map.putBoolean(key, (Boolean) value);
550
- } else if(value instanceof Integer) {
551
- map.putInt(key, (Integer) value);
552
- } else if(value instanceof Long) {
553
- Long longValue = (Long) value;
554
- map.putDouble(key, longValue.doubleValue());
555
- } else if(value instanceof Double) {
556
- map.putDouble(key, (Double) value);
557
- } else if(value instanceof String) {
558
- map.putString(key, (String) value);
559
- } else if (value instanceof JSONObject) {
560
- map.putMap(key, convertJsonObject( (JSONObject)value ));
561
- } else if(value instanceof JSONArray) {
562
- map.putArray(key, convertJsonArray( (JSONArray)value ));
563
- } else {
564
- throw new IllegalArgumentException("Unsupported type: " + value.getClass());
565
- }
566
- }
567
-
568
- return map;
569
- }
570
-
571
- public Bundle convertJsonObjectToBundle(JSONObject jsonObject) throws JSONException {
572
- Bundle bundle = new Bundle();
573
- Iterator<String> jsonIterator = jsonObject.keys();
574
- while (jsonIterator.hasNext()) {
575
- String key = jsonIterator.next();
576
- Object value = jsonObject.get(key);
577
-
578
- if(value instanceof Boolean) {
579
- Boolean booleanValue = (Boolean)value;
580
- bundle.putShort(key, (short) (booleanValue.booleanValue() ? 1 : 0));
581
- } else if(value instanceof Integer) {
582
- Integer integerValue = (Integer)value;
583
- bundle.putFloat(key, integerValue.floatValue());
584
- } else if(value instanceof Long) {
585
- Long longValue = (Long)value;
586
- bundle.putFloat(key, longValue.floatValue());
587
- } else if(value instanceof Double) {
588
- Double doubleValue = (Double) value;
589
- bundle.putFloat(key, doubleValue.floatValue());
590
- } else if(value instanceof String) {
591
- bundle.putString(key, (String) value);
592
- } else if (value instanceof JSONObject) {
593
- bundle.putBundle(key, convertJsonObjectToBundle( (JSONObject)value ));
594
- } else if(value instanceof JSONArray) {
595
- ArrayList<String> array = new ArrayList<String>();
596
- JSONArray jsonArray = (JSONArray) value;
597
- for (int i = 0; i < jsonArray.length(); i++) {
598
- Object arrayValue = jsonArray.get(i);
599
- array.add(arrayValue.toString());
600
- }
601
- bundle.putStringArrayList(key, array);
602
- }
603
- }
604
- return bundle;
605
- }
606
-
607
- @Override
608
- public void onHostResume() {
266
+ MceSdk.getEventsClient(false).sendEvent(reactContext, event, new OperationCallback<Event>() {
267
+ @Override
268
+ public void onSuccess(Event event, OperationResult result) {
269
+
270
+ }
271
+
272
+ @Override
273
+ public void onFailure(Event event, OperationResult result) {
274
+ MceSdk.getQueuedEventsClient().sendEvent(reactContext, event);
275
+ }
276
+ });
277
+ }
278
+ }
279
+
280
+ @ReactMethod
281
+ public void hideInbox() {
282
+ final Activity activity = reactContext.getCurrentActivity();
283
+ if (activity == null) {
284
+ Logger.e(TAG, "Can't find activity");
285
+ return;
286
+ }
287
+
288
+ activity.runOnUiThread(new Runnable() {
289
+ @Override
290
+ public void run() {
291
+ internalHideInbox();
292
+ }
293
+ });
294
+ }
295
+
296
+
297
+ // Needs to be run on the main thread.
298
+ private void internalHideInbox() {
299
+ if (this.relativeLayout != null) {
300
+ ViewParent parent = this.relativeLayout.getParent();
301
+ if (parent instanceof ViewGroup) {
302
+ ViewGroup group = (ViewGroup) parent;
303
+ group.removeView(this.relativeLayout);
304
+ } else {
305
+ Logger.e(TAG, "InApp Parent is not a ViewGroup!");
306
+ }
307
+ }
308
+ this.relativeLayout = null;
309
+ }
310
+
311
+ public static JSONArray convertReadableArray(ReadableArray readableArray) throws JSONException {
312
+ JSONArray jsonArray = new JSONArray();
313
+ for (int i = 0; i < readableArray.size(); i++) {
314
+ ReadableType type = readableArray.getType(i);
315
+ if (type == String) {
316
+ jsonArray.put(readableArray.getString(i));
317
+ } else if (type == Boolean) {
318
+ jsonArray.put(readableArray.getBoolean(i));
319
+ } else if (type == Null) {
320
+ jsonArray.put(JSONObject.NULL);
321
+ } else if (type == Number) {
322
+ jsonArray.put(readableArray.getDouble(i));
323
+ } else if (type == Map) {
324
+ jsonArray.put(convertReadableMap(readableArray.getMap(i)));
325
+ } else if (type == Array) {
326
+ jsonArray.put(convertReadableArray(readableArray.getArray(i)));
327
+ }
328
+ }
329
+ return jsonArray;
330
+ }
331
+
332
+ public static JSONObject convertReadableMap(ReadableMap readableMap) throws JSONException {
333
+ JSONObject jsonObject = new JSONObject();
334
+ ReadableMapKeySetIterator keys = readableMap.keySetIterator();
335
+ while (keys.hasNextKey()) {
336
+ String key = keys.nextKey();
337
+ ReadableType type = readableMap.getType(key);
338
+ if (type == String) {
339
+ jsonObject.put(key, readableMap.getString(key));
340
+ } else if (type == Boolean) {
341
+ jsonObject.put(key, readableMap.getBoolean(key));
342
+ } else if (type == Null) {
343
+ jsonObject.put(key, JSONObject.NULL);
344
+ } else if (type == Number) {
345
+ jsonObject.put(key, readableMap.getDouble(key));
346
+ } else if (type == Map) {
347
+ jsonObject.put(key, convertReadableMap(readableMap.getMap(key)));
348
+ } else if (type == Array) {
349
+ jsonObject.put(key, convertReadableArray(readableMap.getArray(key)));
350
+ }
351
+ }
352
+ return jsonObject;
353
+ }
354
+
355
+ public static WritableNativeArray convertJsonArray(JSONArray jsonArray) throws JSONException {
356
+ WritableNativeArray array = new WritableNativeArray();
357
+ for (int i = 0; i < jsonArray.length(); i++) {
358
+ Object value = jsonArray.get(i);
359
+
360
+ if (value == null || value == JSONObject.NULL) {
361
+ array.pushNull();
362
+ } else if (value instanceof Boolean) {
363
+ array.pushBoolean((Boolean) value);
364
+ } else if (value instanceof Integer) {
365
+ array.pushInt((Integer) value);
366
+ } else if (value instanceof Long) {
367
+ Long longValue = (Long) value;
368
+ array.pushDouble(longValue.doubleValue());
369
+ } else if (value instanceof Double) {
370
+ array.pushDouble((Double) value);
371
+ } else if (value instanceof String) {
372
+ array.pushString((String) value);
373
+ } else if (value instanceof JSONObject) {
374
+ array.pushMap(convertJsonObject((JSONObject) value));
375
+ } else if (value instanceof JSONArray) {
376
+ array.pushArray(convertJsonArray((JSONArray) value));
377
+ } else {
378
+ throw new IllegalArgumentException("Unsupported type: " + value.getClass());
379
+ }
380
+ }
381
+ return array;
382
+ }
383
+
384
+ public static WritableNativeMap convertJsonObject(JSONObject jsonObject) throws JSONException {
385
+ WritableNativeMap map = new WritableNativeMap();
386
+ Iterator<String> jsonIterator = jsonObject.keys();
387
+ while (jsonIterator.hasNext()) {
388
+ String key = jsonIterator.next();
389
+ Object value = jsonObject.get(key);
390
+
391
+ if (value == null || value == JSONObject.NULL) {
392
+ map.putNull(key);
393
+ } else if (value instanceof Boolean) {
394
+ map.putBoolean(key, (Boolean) value);
395
+ } else if (value instanceof Integer) {
396
+ map.putInt(key, (Integer) value);
397
+ } else if (value instanceof Long) {
398
+ Long longValue = (Long) value;
399
+ map.putDouble(key, longValue.doubleValue());
400
+ } else if (value instanceof Double) {
401
+ map.putDouble(key, (Double) value);
402
+ } else if (value instanceof String) {
403
+ map.putString(key, (String) value);
404
+ } else if (value instanceof JSONObject) {
405
+ map.putMap(key, convertJsonObject((JSONObject) value));
406
+ } else if (value instanceof JSONArray) {
407
+ map.putArray(key, convertJsonArray((JSONArray) value));
408
+ } else {
409
+ throw new IllegalArgumentException("Unsupported type: " + value.getClass());
410
+ }
411
+ }
412
+
413
+ return map;
414
+ }
415
+
416
+ @Override
417
+ public void onHostResume() {
609
418
  registerInboxComponent();
610
- }
419
+ }
611
420
 
612
- @Override
613
- public void onHostPause() {
614
- }
421
+ @Override
422
+ public void onHostPause() {
423
+ }
615
424
 
616
- @Override
617
- public void onHostDestroy() {
425
+ @Override
426
+ public void onHostDestroy() {
618
427
 
619
- }
428
+ }
620
429
 
621
430
  @ReactMethod
622
431
  public void addListener(String eventName) {