-
Notifications
You must be signed in to change notification settings - Fork 40
[MOB-12264] Add sync messages and get messages to android #746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: loren/embedded/MOB-12265-start-end-session
Are you sure you want to change the base?
Changes from 3 commits
760a9e3
1986caa
94e46a6
bb0c200
0984e9e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -651,6 +651,11 @@ public void onInboxUpdated() { | |
| // --------------------------------------------------------------------------------------- | ||
| // region Embedded messaging | ||
|
|
||
| public void syncEmbeddedMessages() { | ||
| IterableLogger.d(TAG, "syncEmbeddedMessages"); | ||
| IterableApi.getInstance().getEmbeddedManager().syncMessages(); | ||
| } | ||
|
|
||
| public void startEmbeddedSession() { | ||
| IterableLogger.d(TAG, "startEmbeddedSession"); | ||
| IterableApi.getInstance().getEmbeddedManager().getEmbeddedSessionManager().startSession(); | ||
|
|
@@ -678,6 +683,41 @@ public void getEmbeddedPlacementIds(Promise promise) { | |
| } | ||
| } | ||
|
|
||
| public void getEmbeddedMessages(@Nullable ReadableArray placementIds, Promise promise) { | ||
| IterableLogger.d(TAG, "getEmbeddedMessages for placements: " + placementIds); | ||
|
|
||
| try { | ||
| List<IterableEmbeddedMessage> allMessages = new ArrayList<>(); | ||
|
|
||
| if (placementIds == null || placementIds.size() == 0) { | ||
| // If no placement IDs provided, we need to get messages for all possible placements | ||
| // Since the Android SDK requires a placement ID, we'll use 0 as a default | ||
| // This might need to be adjusted based on the actual SDK behavior | ||
| List<IterableEmbeddedMessage> messages = IterableApi.getInstance().getEmbeddedManager().getMessages(0L); | ||
| if (messages != null) { | ||
| allMessages.addAll(messages); | ||
| } | ||
| } else { | ||
| // Convert ReadableArray to individual placement IDs and get messages for each | ||
| for (int i = 0; i < placementIds.size(); i++) { | ||
| long placementId = placementIds.getInt(i); | ||
| List<IterableEmbeddedMessage> messages = IterableApi.getInstance().getEmbeddedManager().getMessages(placementId); | ||
| if (messages != null) { | ||
| allMessages.addAll(messages); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| JSONArray embeddedMessageJsonArray = Serialization.serializeEmbeddedMessages(allMessages); | ||
| IterableLogger.d(TAG, "Messages for placements: " + embeddedMessageJsonArray); | ||
|
|
||
| promise.resolve(Serialization.convertJsonToArray(embeddedMessageJsonArray)); | ||
| } catch (JSONException e) { | ||
| IterableLogger.e(TAG, e.getLocalizedMessage()); | ||
| promise.reject("", "Failed to fetch messages with error " + e.getLocalizedMessage()); | ||
| } | ||
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
|
|
||
| // --------------------------------------------------------------------------------------- | ||
| // endregion | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,15 +1,27 @@ | ||||||||||||
| import { Text, TouchableOpacity, View } from 'react-native'; | ||||||||||||
| import { ScrollView, Text, TouchableOpacity, View } from 'react-native'; | ||||||||||||
| import { useCallback, useState } from 'react'; | ||||||||||||
| import { Iterable } from '@iterable/react-native-sdk'; | ||||||||||||
| import { | ||||||||||||
| Iterable, | ||||||||||||
| type IterableEmbeddedMessage, | ||||||||||||
| } from '@iterable/react-native-sdk'; | ||||||||||||
|
|
||||||||||||
| import styles from './Embedded.styles'; | ||||||||||||
|
|
||||||||||||
| export const Embedded = () => { | ||||||||||||
| const [placementIds, setPlacementIds] = useState<number[]>([]); | ||||||||||||
| const [embeddedMessages, setEmbeddedMessages] = useState< | ||||||||||||
| IterableEmbeddedMessage[] | ||||||||||||
| >([]); | ||||||||||||
|
|
||||||||||||
| const syncEmbeddedMessages = useCallback(() => { | ||||||||||||
| Iterable.embeddedManager.syncMessages(); | ||||||||||||
| }, []); | ||||||||||||
|
|
||||||||||||
| const getPlacementIds = useCallback(() => { | ||||||||||||
| Iterable.embeddedManager.getPlacementIds().then((ids: unknown) => { | ||||||||||||
| return Iterable.embeddedManager.getPlacementIds().then((ids: unknown) => { | ||||||||||||
| console.log(ids); | ||||||||||||
| setPlacementIds(ids as number[]); | ||||||||||||
| return ids; | ||||||||||||
| }); | ||||||||||||
| }, []); | ||||||||||||
|
|
||||||||||||
|
|
@@ -27,28 +39,69 @@ export const Embedded = () => { | |||||||||||
| Iterable.embeddedManager.endSession(); | ||||||||||||
| }, []); | ||||||||||||
|
|
||||||||||||
| const getEmbeddedMessages = useCallback(() => { | ||||||||||||
| getPlacementIds() | ||||||||||||
| .then((ids: number[]) => Iterable.embeddedManager.getMessages(ids)) | ||||||||||||
| .then((messages: IterableEmbeddedMessage[]) => { | ||||||||||||
| setEmbeddedMessages(messages); | ||||||||||||
| console.log(messages); | ||||||||||||
|
||||||||||||
| console.log(messages); | |
| console.log(messages); | |
| }) | |
| .catch((error) => { | |
| console.error('Failed to get embedded messages:', error); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,33 @@ | ||
| import type { TurboModule } from 'react-native'; | ||
| import { TurboModuleRegistry } from 'react-native'; | ||
|
|
||
| // NOTE: No types can be imported because of the way new arch works, so we have | ||
| // to re-define the types here. | ||
| interface EmbeddedMessage { | ||
| metadata: { | ||
| messageId: string; | ||
| placementId: number; | ||
| campaignId?: number | null; | ||
| isProof?: boolean; | ||
| }; | ||
| elements: { | ||
| buttons?: | ||
| | { | ||
| id: string; | ||
| title?: string | null; | ||
| action: { type: string; data?: string } | null; | ||
| }[] | ||
| | null; | ||
| body?: string | null; | ||
| mediaUrl?: string | null; | ||
| mediaUrlCaption?: string | null; | ||
| defaultAction?: { type: string; data?: string } | null; | ||
| text?: { id: string; text?: string | null; label?: string | null }[] | null; | ||
| title?: string | null; | ||
| } | null; | ||
| payload?: { [key: string]: string | number | boolean | null } | null; | ||
|
||
| } | ||
|
|
||
| export interface Spec extends TurboModule { | ||
| // Initialization | ||
| initializeWithApiKey( | ||
|
|
@@ -119,9 +146,13 @@ export interface Spec extends TurboModule { | |
| pauseAuthRetries(pauseRetry: boolean): void; | ||
|
|
||
| // Embedded Messaging | ||
| syncEmbeddedMessages(): void; | ||
| startEmbeddedSession(): void; | ||
| endEmbeddedSession(): void; | ||
| getEmbeddedPlacementIds(): Promise<number[]>; | ||
| getEmbeddedMessages( | ||
| placementIds: number[] | null | ||
| ): Promise<EmbeddedMessage[]>; | ||
|
|
||
| // Wake app -- android only | ||
| wakeApp(): void; | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,4 +1,5 @@ | ||||||||||||||||||||||||||||
| import { IterableApi } from '../../core/classes/IterableApi'; | ||||||||||||||||||||||||||||
| import type { IterableEmbeddedMessage } from '../types/IterableEmbeddedMessage'; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||
| * Manages embedded messages from Iterable. | ||||||||||||||||||||||||||||
|
|
@@ -20,6 +21,29 @@ export class IterableEmbeddedManager { | |||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||
| isEnabled = false; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||
| * Syncs embedded local cache with the server. | ||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||
| * When your app first launches, and each time it comes to the foreground, | ||||||||||||||||||||||||||||
| * Iterable's iOS SDK automatically refresh a local, on-device cache of | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
| * Iterable's iOS SDK automatically refresh a local, on-device cache of | |
| * Iterable's SDK automatically refreshes a local, on-device cache of |
Copilot
AI
Nov 19, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation for getMessages() doesn't explain the behavior when placementIds is null or an empty array. Based on the Android implementation, this is an important detail to document since the behavior is unclear (currently uses placement ID 0 as a default, which may not be correct).
Suggested documentation improvement:
/**
* Retrieves a list of embedded messages the user is eligible to see.
*
* @param placementIds - The placement IDs to retrieve messages for.
* Pass null or an empty array to get messages for all placements.
* @returns A Promise that resolves to an array of embedded messages.
* @example
* ```typescript
* // Get messages for specific placements
* const messages = await Iterable.embeddedManager.getMessages([123, 456]);
*
* // Get messages for all placements
* const allMessages = await Iterable.embeddedManager.getMessages(null);
* ```
*/| * @param placementIds - The placement IDs to retrieve messages for. | |
| * @returns A Promise that resolves to an array of embedded messages. | |
| * @param placementIds - The placement IDs to retrieve messages for. | |
| * Pass `null` or an empty array to get messages for all placements. | |
| * @returns A Promise that resolves to an array of embedded messages. | |
| * @example | |
| * ```typescript | |
| * // Get messages for specific placements | |
| * const messages = await Iterable.embeddedManager.getMessages([123, 456]); | |
| * | |
| * // Get messages for all placements | |
| * const allMessages = await Iterable.embeddedManager.getMessages(null); | |
| * ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| export * from './classes'; | ||
| export * from './types'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
0as a default placement ID when none are provided is questionable and may not work as expected. The comment acknowledges this with "This might need to be adjusted based on the actual SDK behavior."This should either:
getPlacementIds()0is a valid special value in the Iterable Android SDKConsider implementing option 2: