-
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 all 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 |
|---|---|---|
|
|
@@ -643,6 +643,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(); | ||
|
|
@@ -670,6 +675,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,16 +1,28 @@ | ||||||||||||
| import { Text, TouchableOpacity } 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 { SafeAreaView } from 'react-native-safe-area-context'; | ||||||||||||
|
|
||||||||||||
| 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; | ||||||||||||
| }); | ||||||||||||
| }, []); | ||||||||||||
|
|
||||||||||||
|
|
@@ -28,28 +40,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; | ||
|
|
||
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: