Skip to content

Commit

Permalink
feat(inappmessaging): add support for triggering custom events (#4201)
Browse files Browse the repository at this point in the history
* Expose triggerEvent() function for Firebase In App Messaging
* Fix NSString* and add simple test
  • Loading branch information
gilbertl authored Sep 3, 2020
1 parent 873c20e commit fe8cbc1
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ Task<Void> setMessagesDisplaySuppressed(Boolean enabled) {
});
}

Task<Void> triggerEvent(String eventId) {
return Tasks.call(() -> {
FirebaseInAppMessaging.getInstance().triggerEvent(eventId);
return null;
});
}

@Override
public Map<String, Object> getConstants() {
final Map<String, Object> constants = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ public void setMessagesDisplaySuppressed(Boolean enabled, Promise promise) {
});
}

@ReactMethod
public void triggerEvent(String eventId, Promise promise) {
module.triggerEvent(eventId).addOnCompleteListener(task -> {
if (task.isSuccessful()) {
promise.resolve(task.getResult());
} else {
rejectPromiseWithExceptionMap(promise, task.getException());
}
});
}

@Override
public Map<String, Object> getConstants() {
return module.getConstants();
Expand Down
7 changes: 7 additions & 0 deletions packages/in-app-messaging/e2e/fiam.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,11 @@ describe('inAppMessaging()', () => {
}
});
});

xdescribe('triggerEvent()', () => {
it('no exceptions thrown', async () => {
await device.launchApp();
await firebase.inAppMessaging().triggerEvent('eventName');
});
});
});
11 changes: 11 additions & 0 deletions packages/in-app-messaging/ios/RNFBFiam/RNFBFiamModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,15 @@ + (BOOL)requiresMainQueueSetup {
resolve([NSNull null]);
}

RCT_EXPORT_METHOD(triggerEvent:
(NSString*) eventId
resolver:
(RCTPromiseResolveBlock) resolve
rejecter:
(RCTPromiseRejectBlock) reject) {
[[FIRInAppMessaging inAppMessaging] triggerEvent: eventId];
resolve([NSNull null]);
}


@end
14 changes: 14 additions & 0 deletions packages/in-app-messaging/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,20 @@ export namespace FirebaseInAppMessagingTypes {
* @param enabled Whether automatic data collection is enabled.
*/
setAutomaticDataCollectionEnabled(enabled: boolean): Promise<null>;

/**
* Trigger in-app messages programmatically
*
* #### Example
*
* ```js
* // Suppress messages
* await firebase.inAppMessaging().triggerEvent("exampleTrigger");
* ```
*
* @param eventId The id of the event.
*/
triggerEvent(eventId: string): Promise<null>;
}
}

Expand Down
9 changes: 8 additions & 1 deletion packages/in-app-messaging/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
*/

import { isBoolean } from '@react-native-firebase/app/lib/common';
import { isBoolean, isString } from '@react-native-firebase/app/lib/common';
import {
createModuleNamespace,
FirebaseModule,
Expand Down Expand Up @@ -65,6 +65,13 @@ class FirebaseFiamModule extends FirebaseModule {
this._isAutomaticDataCollectionEnabled = enabled;
return this.native.setAutomaticDataCollectionEnabled(enabled);
}

triggerEvent(eventId) {
if (!isString(eventId)) {
throw new Error("firebase.inAppMessaging().triggerEvent(*) 'eventId' must be a string.");
}
return this.native.triggerEvent(eventId);
}
}

// import { SDK_VERSION } from '@react-native-firebase/in-app-messaging';
Expand Down

1 comment on commit fe8cbc1

@vercel
Copy link

@vercel vercel bot commented on fe8cbc1 Sep 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.