@adobe/react-native-aepmessaging 1.0.0-beta.2 → 1.0.0

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.
@@ -10,28 +10,244 @@
10
10
  */
11
11
  package com.adobe.marketing.mobile.reactnative.messaging;
12
12
 
13
+ import com.adobe.marketing.mobile.AdobeCallback;
14
+ import com.adobe.marketing.mobile.LoggingMode;
15
+
16
+ import static com.adobe.marketing.mobile.LoggingMode.DEBUG;
17
+ import static com.adobe.marketing.mobile.LoggingMode.VERBOSE;
18
+
19
+ import android.util.Log;
20
+
21
+ import com.adobe.marketing.mobile.Message;
13
22
  import com.adobe.marketing.mobile.Messaging;
23
+ import com.adobe.marketing.mobile.MessagingEdgeEventType;
24
+ import com.adobe.marketing.mobile.MobileCore;
25
+ import com.adobe.marketing.mobile.services.MessagingDelegate;
26
+ import com.adobe.marketing.mobile.services.ui.FullscreenMessage;
27
+ import com.facebook.react.bridge.Arguments;
14
28
  import com.facebook.react.bridge.Promise;
15
29
  import com.facebook.react.bridge.ReactApplicationContext;
16
30
  import com.facebook.react.bridge.ReactContextBaseJavaModule;
17
31
  import com.facebook.react.bridge.ReactMethod;
32
+ import com.facebook.react.bridge.WritableMap;
33
+ import com.facebook.react.modules.core.DeviceEventManagerModule;
34
+
35
+ import java.util.HashMap;
36
+ import java.util.Map;
37
+ import java.util.concurrent.CountDownLatch;
38
+
39
+ public final class RCTAEPMessagingModule extends ReactContextBaseJavaModule implements MessagingDelegate {
40
+
41
+ private static final String TAG = "RCTAEPMessagingModule";
42
+ private final Map<String, Message> messageCache;
43
+ private final ReactApplicationContext reactContext;
44
+ private boolean shouldSaveMessage;
45
+ private boolean shouldShowMessage;
46
+ private CountDownLatch latch = new CountDownLatch(1);
47
+
48
+ public RCTAEPMessagingModule(ReactApplicationContext reactContext) {
49
+ super(reactContext);
50
+ this.reactContext = reactContext;
51
+ this.messageCache = new HashMap<>();
52
+ this.shouldShowMessage = false;
53
+ this.shouldSaveMessage = false;
54
+ }
55
+
56
+ @Override
57
+ public String getName() {
58
+ return "AEPMessaging";
59
+ }
60
+
61
+ // Required for rn built in EventEmitter Calls.
62
+ @ReactMethod
63
+ public void addListener(String eventName) {}
64
+
65
+ @ReactMethod
66
+ public void removeListeners(Integer count) {}
67
+
68
+ @ReactMethod
69
+ public void extensionVersion(final Promise promise) {
70
+ MobileCore.log(VERBOSE, TAG, "extensionVersion is called");
71
+ promise.resolve(Messaging.extensionVersion());
72
+ }
73
+
74
+ @ReactMethod
75
+ public void refreshInAppMessages() {
76
+ MobileCore.log(VERBOSE, TAG, "refreshInAppMessages is called");
77
+ Messaging.refreshInAppMessages();
78
+ }
79
+
80
+ @ReactMethod
81
+ public void setMessagingDelegate() {
82
+ MobileCore.log(VERBOSE, TAG, "setMessagingDelegate is called");
83
+ MobileCore.setMessagingDelegate(this);
84
+ }
85
+
86
+ @ReactMethod
87
+ public void show(final String messageId) {
88
+ if (messageId != null && messageCache.get(messageId) != null) {
89
+ MobileCore.log(VERBOSE, TAG, String.format("show is called with message id: %s", messageId));
90
+ messageCache.get(messageId).show();
91
+ }
92
+ }
93
+
94
+ @ReactMethod
95
+ public void dismiss(final String messageId, final boolean suppressAutoTrack) {
96
+ if (messageId != null && messageCache.get(messageId) != null) {
97
+ MobileCore.log(VERBOSE, TAG, String.format("dismiss is called with message id: %s and suppressAutoTrack: %b", messageId, suppressAutoTrack));
98
+ messageCache.get(messageId).dismiss(suppressAutoTrack);
99
+ }
100
+ }
101
+
102
+ @ReactMethod
103
+ public void track(final String messageId, final String interaction, final int eventType) {
104
+ if (messageId != null && messageCache.get(messageId) != null) {
105
+ MobileCore.log(VERBOSE, TAG, String.format("track is called with message id: %s, interaction: %s and eventType: %d", messageId, interaction, eventType));
106
+ MessagingEdgeEventType edgeEventType = null;
107
+ switch (eventType) {
108
+ case 0:
109
+ edgeEventType = MessagingEdgeEventType.IN_APP_DISMISS;
110
+ break;
111
+ case 1:
112
+ edgeEventType = MessagingEdgeEventType.IN_APP_INTERACT;
113
+ break;
114
+ case 2:
115
+ edgeEventType = MessagingEdgeEventType.IN_APP_TRIGGER;
116
+ break;
117
+ case 3:
118
+ edgeEventType = MessagingEdgeEventType.IN_APP_DISPLAY;
119
+ break;
120
+ case 4:
121
+ edgeEventType = MessagingEdgeEventType.PUSH_APPLICATION_OPENED;
122
+ break;
123
+ case 5:
124
+ edgeEventType = MessagingEdgeEventType.PUSH_CUSTOM_ACTION;
125
+ break;
126
+ default:
127
+ break;
128
+ }
129
+ if (edgeEventType != null) {
130
+ messageCache.get(messageId).track(interaction, edgeEventType);
131
+ } else {
132
+ MobileCore.log(DEBUG, TAG, String.format("Unable to track interaction (%s) because edgeEventType (%d) is invalid ", interaction, eventType));
133
+ }
134
+ }
135
+ }
136
+
137
+ @ReactMethod
138
+ public void handleJavascriptMessage(final String messageId, final String messageName, final Promise promise) {
139
+ if (messageId != null && messageCache.get(messageId) != null) {
140
+ MobileCore.log(VERBOSE, TAG, String.format("handleJavascriptMessage is called with message id: %s and messageName: %s", messageId, messageName));
141
+ messageCache.get(messageId).handleJavascriptMessage(messageName, new AdobeCallback<String>() {
142
+ @Override
143
+ public void call(final String s) {
144
+ if (s != null) {
145
+ promise.resolve(s);
146
+ } else {
147
+ promise.reject("error", "error in handling javascriptMessage " + messageName);
148
+ }
149
+ }
150
+ });
151
+ }
152
+ }
153
+
154
+ @ReactMethod
155
+ public void clear(final String messageId) {
156
+ if (messageId != null) {
157
+ MobileCore.log(VERBOSE, TAG, String.format("clear is called with message id: %s", messageId));
158
+ messageCache.remove(messageId);
159
+ }
160
+ }
161
+
162
+ @ReactMethod
163
+ public void setAutoTrack(final String messageId, final boolean autoTrack) {
164
+ if (messageId != null && messageCache.get(messageId) != null) {
165
+ MobileCore.log(VERBOSE, TAG, String.format("setAutoTrack is called with message id: %s and autoTrack: %b", messageId, autoTrack));
166
+ messageCache.get(messageId).setAutoTrack(autoTrack);
167
+ }
168
+ }
18
169
 
19
- public class RCTAEPMessagingModule extends ReactContextBaseJavaModule {
170
+ @ReactMethod(isBlockingSynchronousMethod = true)
171
+ public void shouldShowMessage(final boolean shouldShowMessage, final boolean shouldSaveMessage) {
172
+ MobileCore.log(VERBOSE, TAG, String.format("shouldShowMessage is called with message id: %b and shouldSaveMessage: %b", shouldShowMessage, shouldSaveMessage));
173
+ this.shouldShowMessage = shouldShowMessage;
174
+ this.shouldSaveMessage = shouldSaveMessage;
175
+ latch.countDown();
176
+ }
20
177
 
21
- private final ReactApplicationContext reactContext;
178
+ //Messaging Delegate functions
179
+ @Override
180
+ public void onShow(final FullscreenMessage fullscreenMessage) {
181
+ MobileCore.log(VERBOSE, TAG, "onShow is called");
182
+ final Message message = (Message) fullscreenMessage.getParent();
183
+ if (message != null) {
184
+ Map<String, String> data = new HashMap<>();
185
+ data.put("id", message.getId());
186
+ data.put("autoTrack", String.valueOf(message.getAutoTrack()));
187
+ emitEvent("onShow", data);
188
+ }
189
+ }
22
190
 
23
- public RCTAEPMessagingModule(ReactApplicationContext reactContext) {
24
- super(reactContext);
25
- this.reactContext = reactContext;
26
- }
191
+ @Override
192
+ public void onDismiss(final FullscreenMessage fullscreenMessage) {
193
+ MobileCore.log(VERBOSE, TAG, "onDismiss is called");
194
+ final Message message = (Message) fullscreenMessage.getParent();
195
+ if (message != null) {
196
+ Map<String, String> data = new HashMap<>();
197
+ data.put("id", message.getId());
198
+ data.put("autoTrack", String.valueOf(message.getAutoTrack()));
199
+ emitEvent("onDismiss", data);
200
+ }
201
+ }
27
202
 
28
- @Override
29
- public String getName() {
30
- return "AEPMessaging";
31
- }
203
+ @Override
204
+ public boolean shouldShowMessage(final FullscreenMessage fullscreenMessage) {
205
+ MobileCore.log(VERBOSE, TAG, "shouldShowMessage is called");
206
+ final Message message = (Message) fullscreenMessage.getParent();
207
+ if (message != null) {
208
+ Map<String, String> data = new HashMap<>();
209
+ data.put("id", message.getId());
210
+ data.put("autoTrack", String.valueOf(message.getAutoTrack()));
211
+ emitEvent("shouldShowMessage", data);
212
+ //Latch stops the thread until the shouldShowMessage value is received from the JS side on thread dedicated to run JS code. The function called from JS that resumes the thread is "shouldShowMessage".
213
+ MobileCore.log(VERBOSE, TAG, "shouldShowMessage: Thread is locked.");
214
+ try {
215
+ latch.await();
216
+ } catch (final InterruptedException e) {
217
+ MobileCore.log(LoggingMode.ERROR, TAG, String.format("CountDownLatch await Interrupted: (%s)", e.getLocalizedMessage()));
218
+ }
219
+ MobileCore.log(VERBOSE, TAG, "shouldShowMessage: Thread is resumed.");
220
+ if (shouldSaveMessage) {
221
+ messageCache.put(message.getId(), message);
222
+ }
223
+ }
224
+ return shouldShowMessage;
225
+ }
226
+
227
+ @Override
228
+ public void urlLoaded(String url, FullscreenMessage fullscreenMessage) {
229
+ MobileCore.log(VERBOSE, TAG, String.format("overrideUrlLoad is called with url: (%s)", url));
230
+ final Message message = (Message) fullscreenMessage.getParent();
231
+ if (message != null) {
232
+ Map<String, String> data = new HashMap<>();
233
+ data.put("id", message.getId());
234
+ data.put("autoTrack", String.valueOf(message.getAutoTrack()));
235
+ data.put("url", url);
236
+ emitEvent("urlLoaded", data);
237
+ }
238
+ }
32
239
 
33
- @ReactMethod
34
- public void extensionVersion(final Promise promise) {
35
- promise.resolve(Messaging.extensionVersion());
36
- }
240
+ /**
241
+ * Emits an event along with data to be handled by the Javascript
242
+ *
243
+ * @param name event name
244
+ * @param data data sent along with event
245
+ */
246
+ private void emitEvent(final String name, final Map<String, String> data) {
247
+ WritableMap eventData = Arguments.createMap();
248
+ for (final Map.Entry<String, String> entry : data.entrySet()) {
249
+ eventData.putString(entry.getKey(), entry.getValue());
250
+ }
251
+ reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(name, eventData);
252
+ }
37
253
  }
@@ -9,9 +9,12 @@
9
9
  governing permissions and limitations under the License.
10
10
  */
11
11
 
12
- #import <React/RCTBridgeModule.h>
13
- #import <Foundation/Foundation.h>
12
+ #import <React/RCTBridgeModule.h>
13
+ #import <Foundation/Foundation.h>
14
+ #import <React/RCTEventEmitter.h>
15
+ #import <React/RCTBridgeModule.h>
16
+ @import AEPServices;
14
17
 
15
- @interface RCTAEPMessaging : NSObject <RCTBridgeModule>
18
+ @interface RCTAEPMessaging : RCTEventEmitter <RCTBridgeModule, AEPMessagingDelegate>
16
19
 
17
20
  @end
@@ -8,11 +8,34 @@
8
8
  OF ANY KIND, either express or implied. See the License for the specific language
9
9
  governing permissions and limitations under the License.
10
10
  */
11
-
12
11
  #import "RCTAEPMessaging.h"
13
12
  @import AEPMessaging;
13
+ @import AEPCore;
14
+
15
+ static NSString* const TAG = @"RCTAEPMessaging";
16
+
17
+ @implementation RCTAEPMessaging {
18
+ NSMutableDictionary<NSString*, AEPMessage*> * cachedMessages;
19
+ dispatch_semaphore_t semaphore;
20
+ bool shouldShowMessage;
21
+ bool hasListeners;
22
+ bool shouldSaveMessage;
23
+ }
14
24
 
15
- @implementation RCTAEPMessaging
25
+ - (instancetype) init {
26
+ self = [super init];
27
+ cachedMessages = [NSMutableDictionary dictionary];
28
+ semaphore = dispatch_semaphore_create(0);
29
+ hasListeners = false;
30
+ shouldShowMessage = true;
31
+ shouldSaveMessage = false;
32
+ return self;
33
+ }
34
+
35
+ + (BOOL)requiresMainQueueSetup
36
+ {
37
+ return NO;
38
+ }
16
39
 
17
40
  RCT_EXPORT_MODULE(AEPMessaging);
18
41
 
@@ -24,5 +47,150 @@ RCT_EXPORT_MODULE(AEPMessaging);
24
47
  RCT_EXPORT_METHOD(extensionVersion: (RCTPromiseResolveBlock) resolve rejecter:(RCTPromiseRejectBlock) reject) {
25
48
  resolve([AEPMobileMessaging extensionVersion]);
26
49
  }
50
+
51
+ RCT_EXPORT_METHOD(refreshInAppMessages) {
52
+ [AEPLog traceWithLabel:TAG message:@"refreshInAppMessages is called."];
53
+ [AEPMobileMessaging refreshInAppMessages];
54
+ }
55
+
56
+ RCT_EXPORT_METHOD(setMessagingDelegate) {
57
+ [AEPLog traceWithLabel:TAG message:@"Messaging Delegate is set."];
58
+ [AEPMobileCore setMessagingDelegate: self];
59
+ }
60
+
61
+ RCT_EXPORT_METHOD(show: (NSString *) messageId) {
62
+ [AEPLog traceWithLabel:TAG message:[NSString stringWithFormat:@"show is called with message id: %@", messageId]];
63
+ AEPMessage * message = [cachedMessages objectForKey:messageId];
64
+ if(message){
65
+ [message show];
66
+ }
67
+ }
68
+
69
+ RCT_EXPORT_METHOD(dismiss: (NSString *) messageId suppressAutoTrack: (BOOL) suppressAutoTrack) {
70
+ [AEPLog traceWithLabel:TAG message:[NSString stringWithFormat:@"dismiss is called with message id: %@ and suppressAutoTrack: %i", messageId, suppressAutoTrack]];
71
+ AEPMessage * message = [cachedMessages objectForKey:messageId];
72
+ if(message){
73
+ [message dismissSuppressingAutoTrack:suppressAutoTrack];
74
+ }
75
+ }
76
+
77
+ RCT_EXPORT_METHOD(track: (NSString *) messageId withInteraction: (NSString *) interaction eventType: (int) eventTypeValue) {
78
+ [AEPLog traceWithLabel:TAG message:[NSString stringWithFormat:@"track is called with message id: %@, withInteraction: %@ and eventType: %i", messageId, interaction, eventTypeValue]];
79
+ AEPMessage * message = [cachedMessages objectForKey:messageId];
80
+ AEPMessagingEdgeEventType messagingEdgeEventType = -1;
81
+
82
+ if(eventTypeValue == 0){
83
+ messagingEdgeEventType = AEPMessagingEdgeEventTypeInappDismiss;
84
+ } else if (eventTypeValue == 1) {
85
+ messagingEdgeEventType = AEPMessagingEdgeEventTypeInappInteract;
86
+ } else if (eventTypeValue == 2) {
87
+ messagingEdgeEventType = AEPMessagingEdgeEventTypeInappTrigger;
88
+ } else if (eventTypeValue == 3) {
89
+ messagingEdgeEventType = AEPMessagingEdgeEventTypeInappDisplay;
90
+ } else if (eventTypeValue == 4) {
91
+ messagingEdgeEventType = AEPMessagingEdgeEventTypePushApplicationOpened;
92
+ } else if (eventTypeValue == 5) {
93
+ messagingEdgeEventType = AEPMessagingEdgeEventTypePushCustomAction;
94
+ }
95
+
96
+ if(messagingEdgeEventType != -1){
97
+ [message trackInteraction:interaction withEdgeEventType:messagingEdgeEventType];
98
+ }
99
+ }
100
+
101
+ RCT_EXPORT_METHOD(handleJavascriptMessage: (NSString *) messageId
102
+ messageName: (NSString *) name resolver: (RCTPromiseResolveBlock) resolve rejector:(RCTPromiseRejectBlock) reject) {
103
+ [AEPLog traceWithLabel:TAG message:[NSString stringWithFormat:@"handleJavascriptMessage is called with message id: %@ and messageName: %@", messageId, name]];
104
+ AEPMessage * message = [cachedMessages objectForKey: messageId];
105
+ [message handleJavascriptMessage:name withHandler:^(id result) {
106
+ if(result) {
107
+ resolve(result);
108
+ } else {
109
+ reject(@"error", @"error in handleJavaScriptMessage", nil);
110
+ }
111
+ }];
112
+ }
113
+
114
+ RCT_EXPORT_METHOD(clear: (NSString *) messageId) {
115
+ [AEPLog traceWithLabel:TAG message:[NSString stringWithFormat:@"clearMessage is called with message id: %@", messageId]];
116
+ [cachedMessages removeObjectForKey:messageId];
117
+ }
118
+
119
+ RCT_EXPORT_METHOD(setAutoTrack: (NSString *) messageId autoTrack: (BOOL) autoTrack) {
120
+ [AEPLog traceWithLabel:TAG message:[NSString stringWithFormat:@"setAutoTrack called for messageId: %@ and autoTrack value: %i", messageId, autoTrack]];
121
+ AEPMessage * messageObj = [cachedMessages objectForKey:messageId];
122
+ if(messageObj) {
123
+ messageObj.autoTrack = autoTrack;
124
+ }
125
+ }
126
+
127
+ RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(shouldShowMessage: (BOOL) shouldShowMessage shouldSaveMessage: (BOOL) saveMessage) {
128
+ [AEPLog traceWithLabel:TAG message:[NSString stringWithFormat:@"shouldShowMessage is called with values (shouldShowMessage: %i) and (shouldSaveMessage: %i)", shouldShowMessage, shouldSaveMessage]];
129
+ self->shouldShowMessage = shouldShowMessage;
130
+ self->shouldSaveMessage = saveMessage;
131
+ dispatch_semaphore_signal(semaphore);
132
+ return nil;
133
+ }
134
+
135
+ //MARK: - AEPMessagingDelegate functions.
136
+ - (void) onDismiss:(id<AEPShowable> _Nonnull) message {
137
+ AEPFullscreenMessage * fullscreenMessage = (AEPFullscreenMessage *) message;
138
+ AEPMessage * messageObj = (AEPMessage *) fullscreenMessage.settings.parent;
139
+ if(messageObj) {
140
+ [self emitEventWithName:@"onDismiss" body:@{@"id":messageObj.id, @"autoTrack":messageObj.autoTrack ? @"true" : @"false"}];
141
+ }
142
+ }
143
+
144
+ - (void) onShow:(id<AEPShowable> _Nonnull)message {
145
+ AEPFullscreenMessage * fullscreenMessage = (AEPFullscreenMessage *) message;
146
+ AEPMessage * messageObj = (AEPMessage *) fullscreenMessage.settings.parent;
147
+ if(messageObj) {
148
+ [self emitEventWithName:@"onShow" body:@{@"id":messageObj.id, @"autoTrack":messageObj.autoTrack ? @"true" : @"false"}];
149
+ }
150
+ }
151
+
152
+ - (BOOL) shouldShowMessage:(id<AEPShowable> _Nonnull)message {
153
+ AEPFullscreenMessage * fullscreenMessage = (AEPFullscreenMessage *) message;
154
+ AEPMessage * messageObj = (AEPMessage *) fullscreenMessage.settings.parent;
155
+ if(messageObj) {
156
+ [self emitEventWithName:@"shouldShowMessage" body:@{@"id":messageObj.id, @"autoTrack":messageObj.autoTrack ? @"true" : @"false"}];
157
+ //Semaphore stops the thread until the value to be returned from this function is received from the JS side on thread dedicated to run JS code. The function called from JS that unlock the Semaphore is "shouldShowMessage".
158
+ [AEPLog traceWithLabel:TAG message:@"Semaphore lock initiated."];
159
+ dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
160
+ [AEPLog traceWithLabel:TAG message:@"Semaphore lock removed."];
161
+ if(self->shouldSaveMessage) {
162
+ [AEPLog traceWithLabel:TAG message:[[NSString alloc] initWithFormat:@"Message is saved with id: %@", messageObj.id]];
163
+ [cachedMessages setObject:messageObj forKey:messageObj.id];
164
+ }
165
+ }
166
+ return self->shouldShowMessage;
167
+ }
168
+
169
+ - (void) urlLoaded:(NSURL *)url byMessage:(id<AEPShowable>)message {
170
+ AEPFullscreenMessage * fullscreenMessage = (AEPFullscreenMessage *) message;
171
+ AEPMessage * messageObj = (AEPMessage *) fullscreenMessage.settings.parent;
172
+ if(messageObj){
173
+ [self emitEventWithName:@"urlLoaded" body:@{@"id":messageObj.id, @"autoTrack":messageObj.autoTrack ? @"true" : @"false", @"url":url.absoluteString}];
174
+ }
175
+ }
176
+
177
+ - (NSArray<NSString *> *) supportedEvents {
178
+ return @[@"onShow", @"onDismiss", @"shouldShowMessage",@"urlLoaded"];
179
+ }
180
+
181
+ - (void) startObserving {
182
+ hasListeners = true;
183
+ }
184
+
185
+ - (void) stopObserving {
186
+ hasListeners = false;
187
+ }
188
+
189
+ - (void) emitEventWithName: (NSString *) name body: (NSDictionary<NSString*, NSString*> *) dictionary {
190
+ if(hasListeners){
191
+ [AEPLog traceWithLabel:TAG message:[NSString stringWithFormat:@"Event emitted with name: %@", name]];
192
+ [self sendEventWithName:name body:dictionary];
193
+ }
194
+ }
195
+
27
196
  @end
28
-
package/js/Messaging.d.ts CHANGED
@@ -1,5 +1,32 @@
1
- interface IMessaging {
1
+ import Message from './models/Message';
2
+ import { MessagingDelegate } from "./models/MessagingDelegate";
3
+ export interface IMessaging {
2
4
  extensionVersion: () => Promise<string>;
5
+ refreshInAppMessages: () => void;
6
+ setMessagingDelegate: (delegate?: MessagingDelegate) => void;
7
+ shouldShowMessage: (shouldShowMessage: boolean, shouldSaveMessage: boolean) => void;
8
+ saveMessage: (message: Message) => void;
3
9
  }
4
- declare const Messaging: IMessaging;
10
+ declare const Messaging: {
11
+ /**
12
+ * Returns the version of the AEPMessaging extension
13
+ * @returns {string} Promise a promise that resolves with the extension version
14
+ */
15
+ extensionVersion(): Promise<string>;
16
+ /**
17
+ * Initiates a network call to retrieve remote In-App Message definitions.
18
+ */
19
+ refreshInAppMessages(): void;
20
+ /**
21
+ * Function to set the UI Message delegate to listen the Message lifecycle events.
22
+ */
23
+ setMessagingDelegate(delegate: MessagingDelegate): void;
24
+ /**
25
+ * Cache the Message object in-memory to use later.
26
+ * If there is a requirement to call the Message functions like show, dismiss etc. The Message object should be saved first before calling it's functions.
27
+ * If the Message is saved using this API then it is responsibility of App to free it after using.
28
+ * To remove the Message from memory call clearMessage function of Message. Failure to call clearMessage will cause the Memory leak of Message object.
29
+ */
30
+ saveMessage(message: Message): void;
31
+ };
5
32
  export default Messaging;
package/js/Messaging.js CHANGED
@@ -11,16 +11,70 @@ OF ANY KIND, either express or implied. See the License for the specific languag
11
11
  governing permissions and limitations under the License.
12
12
  */
13
13
  Object.defineProperty(exports, "__esModule", { value: true });
14
+ const tslib_1 = require("tslib");
14
15
  const react_native_1 = require("react-native");
16
+ const Message_1 = tslib_1.__importDefault(require("./models/Message"));
15
17
  const RCTAEPMessaging = react_native_1.NativeModules.AEPMessaging;
18
+ var messagingDelegate;
19
+ var savedMessageId = null;
16
20
  const Messaging = {
17
21
  /**
18
22
  * Returns the version of the AEPMessaging extension
19
- * @param {string} Promise a promise that resolves with the extension version
23
+ * @returns {string} Promise a promise that resolves with the extension version
20
24
  */
21
25
  extensionVersion() {
22
26
  return Promise.resolve(RCTAEPMessaging.extensionVersion());
23
- }
27
+ },
28
+ /**
29
+ * Initiates a network call to retrieve remote In-App Message definitions.
30
+ */
31
+ refreshInAppMessages() {
32
+ RCTAEPMessaging.refreshInAppMessages();
33
+ },
34
+ /**
35
+ * Function to set the UI Message delegate to listen the Message lifecycle events.
36
+ */
37
+ setMessagingDelegate(delegate) {
38
+ messagingDelegate = delegate;
39
+ RCTAEPMessaging.setMessagingDelegate();
40
+ const eventEmitter = new react_native_1.NativeEventEmitter(RCTAEPMessaging);
41
+ eventEmitter.addListener('onShow', (event) => {
42
+ if (messagingDelegate) {
43
+ const message = new Message_1.default(event.id, event.autoTrack === "true" ? true : false);
44
+ messagingDelegate.onShow(message);
45
+ }
46
+ });
47
+ eventEmitter.addListener('onDismiss', (event) => {
48
+ if (messagingDelegate) {
49
+ const message = new Message_1.default(event.id, event.autoTrack === "true" ? true : false);
50
+ messagingDelegate.onDismiss(message);
51
+ }
52
+ });
53
+ eventEmitter.addListener('shouldShowMessage', (event) => {
54
+ if (messagingDelegate) {
55
+ const message = new Message_1.default(event.id, event.autoTrack === "true" ? true : false);
56
+ const shouldShowMessage = messagingDelegate.shouldShowMessage(message);
57
+ RCTAEPMessaging.shouldShowMessage(shouldShowMessage, savedMessageId ? true : false);
58
+ savedMessageId = null;
59
+ }
60
+ });
61
+ eventEmitter.addListener('urlLoaded', (event) => {
62
+ if (messagingDelegate) {
63
+ const url = event.url;
64
+ const message = new Message_1.default(event.id, event.autoTrack === "true" ? true : false);
65
+ messagingDelegate.urlLoaded(url, message);
66
+ }
67
+ });
68
+ },
69
+ /**
70
+ * Cache the Message object in-memory to use later.
71
+ * If there is a requirement to call the Message functions like show, dismiss etc. The Message object should be saved first before calling it's functions.
72
+ * If the Message is saved using this API then it is responsibility of App to free it after using.
73
+ * To remove the Message from memory call clearMessage function of Message. Failure to call clearMessage will cause the Memory leak of Message object.
74
+ */
75
+ saveMessage(message) {
76
+ savedMessageId = message.id;
77
+ },
24
78
  };
25
79
  exports.default = Messaging;
26
80
  //# sourceMappingURL=Messaging.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"Messaging.js","sourceRoot":"","sources":["../ts/Messaging.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;EAUE;;AAEF,+CAA6C;AAM7C,MAAM,eAAe,GAAe,4BAAa,CAAC,YAAY,CAAC;AAE/D,MAAM,SAAS,GAAe;IAC5B;;;OAGG;IACH,gBAAgB;QACd,OAAO,OAAO,CAAC,OAAO,CAAC,eAAe,CAAC,gBAAgB,EAAE,CAAC,CAAC;IAC7D,CAAC;CACF,CAAC;AAEF,kBAAe,SAAS,CAAC"}
1
+ {"version":3,"file":"Messaging.js","sourceRoot":"","sources":["../ts/Messaging.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;EAUE;;;AAEF,+CAA+E;AAC/E,uEAAuC;AAWvC,MAAM,eAAe,GAA8B,4BAAa,CAAC,YAAY,CAAC;AAK9E,IAAI,iBAAoC,CAAC;AACzC,IAAI,cAAc,GAAW,IAAI,CAAC;AAElC,MAAM,SAAS,GAAG;IAChB;;;OAGG;IACH,gBAAgB;QACd,OAAO,OAAO,CAAC,OAAO,CAAC,eAAe,CAAC,gBAAgB,EAAE,CAAC,CAAC;IAC7D,CAAC;IAED;;MAEE;IACF,oBAAoB;QAClB,eAAe,CAAC,oBAAoB,EAAE,CAAC;IACzC,CAAC;IAED;;MAEE;IACF,oBAAoB,CAAC,QAA2B;QAC9C,iBAAiB,GAAG,QAAQ,CAAC;QAC7B,eAAe,CAAC,oBAAoB,EAAE,CAAC;QACvC,MAAM,YAAY,GAAG,IAAI,iCAAkB,CAAC,eAAe,CAAC,CAAC;QAC7D,YAAY,CAAC,WAAW,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,EAAE;YAC3C,IAAG,iBAAiB,EAAC;gBACnB,MAAM,OAAO,GAAG,IAAI,iBAAO,CAAC,KAAK,CAAC,EAAE,EAAE,KAAK,CAAC,SAAS,KAAK,MAAM,CAAC,CAAC,CAAC,IAAI,CAAA,CAAC,CAAC,KAAK,CAAC,CAAC;gBAChF,iBAAiB,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;aACnC;QACH,CAAC,CAAC,CAAC;QAEH,YAAY,CAAC,WAAW,CAAC,WAAW,EAAE,CAAC,KAAK,EAAE,EAAE;YAC9C,IAAG,iBAAiB,EAAC;gBACnB,MAAM,OAAO,GAAG,IAAI,iBAAO,CAAC,KAAK,CAAC,EAAE,EAAE,KAAK,CAAC,SAAS,KAAK,MAAM,CAAC,CAAC,CAAC,IAAI,CAAA,CAAC,CAAC,KAAK,CAAC,CAAC;gBAChF,iBAAiB,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;aACtC;QACH,CAAC,CAAC,CAAC;QAEH,YAAY,CAAC,WAAW,CAAC,mBAAmB,EAAE,CAAC,KAAK,EAAE,EAAE;YACtD,IAAI,iBAAiB,EAAC;gBACpB,MAAM,OAAO,GAAG,IAAI,iBAAO,CAAC,KAAK,CAAC,EAAE,EAAE,KAAK,CAAC,SAAS,KAAK,MAAM,CAAC,CAAC,CAAC,IAAI,CAAA,CAAC,CAAC,KAAK,CAAC,CAAC;gBAChF,MAAM,iBAAiB,GAAY,iBAAiB,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAC;gBAChF,eAAe,CAAC,iBAAiB,CAAC,iBAAiB,EAAE,cAAc,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;gBACpF,cAAc,GAAG,IAAI,CAAC;aACvB;QACH,CAAC,CAAC,CAAC;QAEH,YAAY,CAAC,WAAW,CAAC,WAAW,EAAE,CAAC,KAAK,EAAE,EAAE;YAC9C,IAAG,iBAAiB,EAAC;gBACnB,MAAM,GAAG,GAAG,KAAK,CAAC,GAAG,CAAC;gBACtB,MAAM,OAAO,GAAG,IAAI,iBAAO,CAAC,KAAK,CAAC,EAAE,EAAE,KAAK,CAAC,SAAS,KAAK,MAAM,CAAC,CAAC,CAAC,IAAI,CAAA,CAAC,CAAC,KAAK,CAAC,CAAC;gBAChF,iBAAiB,CAAC,SAAS,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;aAC3C;QACH,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;;;MAKE;IACF,WAAW,CAAC,OAAgB;QAC1B,cAAc,GAAG,OAAO,CAAC,EAAE,CAAC;IAC9B,CAAC;CACF,CAAC;AAEF,kBAAe,SAAS,CAAC"}
package/js/index.d.ts CHANGED
@@ -1,2 +1,5 @@
1
1
  import Messaging from './Messaging';
2
- export { Messaging };
2
+ import Message from './models/Message';
3
+ import { MessagingDelegate } from './models/MessagingDelegate';
4
+ import MessagingEdgeEventType from './models/MessagingEdgeEventType';
5
+ export { Messaging, Message, MessagingDelegate, MessagingEdgeEventType };
package/js/index.js CHANGED
@@ -11,8 +11,12 @@ OF ANY KIND, either express or implied. See the License for the specific languag
11
11
  governing permissions and limitations under the License.
12
12
  */
13
13
  Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.Messaging = void 0;
14
+ exports.MessagingEdgeEventType = exports.Message = exports.Messaging = void 0;
15
15
  const tslib_1 = require("tslib");
16
- const Messaging_1 = (0, tslib_1.__importDefault)(require("./Messaging"));
16
+ const Messaging_1 = tslib_1.__importDefault(require("./Messaging"));
17
17
  exports.Messaging = Messaging_1.default;
18
+ const Message_1 = tslib_1.__importDefault(require("./models/Message"));
19
+ exports.Message = Message_1.default;
20
+ const MessagingEdgeEventType_1 = tslib_1.__importDefault(require("./models/MessagingEdgeEventType"));
21
+ exports.MessagingEdgeEventType = MessagingEdgeEventType_1.default;
18
22
  //# sourceMappingURL=index.js.map
package/js/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../ts/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;EAUE;;;;AAEF,yEAAoC;AAE3B,oBAFF,mBAAS,CAEE"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../ts/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;EAUE;;;;AAEF,oEAAoC;AAK3B,oBALF,mBAAS,CAKE;AAJlB,uEAAuC;AAInB,kBAJb,iBAAO,CAIa;AAF3B,qGAAqE;AAErB,iCAFzC,gCAAsB,CAEyC"}
@@ -0,0 +1,43 @@
1
+ declare class Message {
2
+ id: string;
3
+ autoTrack: boolean;
4
+ constructor(id: string, autoTrack: boolean);
5
+ /**
6
+ * Update the value of property "autoTrack"
7
+ * This function works only for the Message objects that were saved by calling "Messaging.saveMessage"
8
+ * @param {boolean} autoTrack: New value of property autoTrack.
9
+ */
10
+ setAutoTrack(autoTrack: boolean): void;
11
+ /**
12
+ * Signals to the UIServices that the message should be shown.
13
+ * If autoTrack is true, calling this method will result in an "inapp.display" Edge Event being dispatched.
14
+ */
15
+ show(): void;
16
+ /**
17
+ * Signals to the UIServices that the message should be dismissed.
18
+ * If `autoTrack` is true, calling this method will result in an "inapp.dismiss" Edge Event being dispatched.
19
+ * @param {boolean?} suppressAutoTrack: if set to true, the inapp.dismiss Edge Event will not be sent regardless
20
+ * of the autoTrack setting.
21
+ */
22
+ dismiss(suppressAutoTrack?: boolean): void;
23
+ /**
24
+ * Generates an Edge Event for the provided interaction and eventType.
25
+ * @param {string?} interaction: a custom String value to be recorded in the interaction
26
+ * @param {MessagingEdgeEventType} eventType: the MessagingEdgeEventType to be used for the ensuing Edge Event
27
+ */
28
+ track(interaction: string, eventType: number): void;
29
+ /**
30
+ * Adds a handler for Javascript messages sent from the message's webview.
31
+ * The parameter passed to `handler` will contain the body of the message passed from the webview's Javascript.
32
+ * @param {string} name: the name of the message that should be handled by `handler`
33
+ * @return {Promise<any?>}: the Promise to be resolved with the body of the message passed by the Javascript message in the WebView
34
+ */
35
+ handleJavascriptMessage(name: string): Promise<any>;
36
+ /**
37
+ * Clears the reference to the Message object.
38
+ * This function must be called if Message was saved by calling "Messaging.saveMessage" but no longer needed.
39
+ * Failure to call this function leads to memory leaks.
40
+ */
41
+ clear(): void;
42
+ }
43
+ export default Message;