Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,20 @@ public void onTokenRegistrationSuccessful(String authToken) {
sendEvent(EventName.handleAuthSuccessCalled.name(), null);
}

@Override
public void onTokenRegistrationFailed(String reason) {
String failureReason = reason != null ? reason : "unknown reason";
IterableLogger.e(TAG, "Push token registration failed: " + failureReason);
JSONObject eventDataJson = new JSONObject();
try {
eventDataJson.put("reason", failureReason);
WritableMap eventData = Serialization.convertJsonToMap(eventDataJson);
sendEvent(EventName.handleTokenRegistrationFailedCalled.name(), eventData);
} catch (JSONException e) {
IterableLogger.e(TAG, "Failed to send token registration failure event");
}
}

public void addListener(String eventName) {
// Keep: Required for RN built in Event Emitter Calls.
}
Expand Down Expand Up @@ -811,5 +825,6 @@ enum EventName {
handleInAppCalled,
handleUrlCalled,
receivedIterableEmbeddedMessagesChanged,
receivedIterableInboxChanged
receivedIterableInboxChanged,
handleTokenRegistrationFailedCalled
}
9 changes: 9 additions & 0 deletions ios/RNIterableAPI/ReactIterableAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import React
case handleAuthFailureCalled
case handleEmbeddedMessageUpdateCalled
case handleEmbeddedMessagingDisabledCalled
case handleTokenRegistrationFailedCalled
}

@objc public static var supportedEvents: [String] {
Expand Down Expand Up @@ -818,6 +819,14 @@ extension ReactIterableAPI: IterableAuthDelegate {
}

public func onTokenRegistrationFailed(_ reason: String?) {
let failureReason = reason ?? "unknown reason"
ITBError("Push token registration failed: \(failureReason)")
guard shouldEmit else {
return
}
delegate?.sendEvent(
withName: EventName.handleTokenRegistrationFailedCalled.rawValue,
body: ["reason": failureReason] as [String: Any])
}
}

Expand Down
16 changes: 16 additions & 0 deletions src/core/classes/Iterable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,9 @@ export class Iterable {
RNEventEmitter.removeAllListeners(
IterableEventName.handleEmbeddedMessagingDisabledCalled
);
RNEventEmitter.removeAllListeners(
IterableEventName.handleTokenRegistrationFailedCalled
);
}

/**
Expand Down Expand Up @@ -1109,6 +1112,19 @@ export class Iterable {
);
}
}

// Always listen for token registration failures so they are surfaced
RNEventEmitter.addListener(
IterableEventName.handleTokenRegistrationFailedCalled,
(dict: { reason: string }) => {
IterableLogger?.log(
'Push token registration failed: ' + (dict?.reason ?? 'unknown reason')
);
Iterable.savedConfig.onTokenRegistrationFailed?.(
dict?.reason ?? 'unknown reason'
);
}
);
}

/**
Expand Down
19 changes: 19 additions & 0 deletions src/core/classes/IterableConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,25 @@ export class IterableConfig {
*/
onEmbeddedMessagingDisabled?: () => void;

/**
* A callback function that is called when device token registration fails.
*
* This can happen when the pushIntegrationName is invalid or when the
* registerDeviceToken API call fails for any reason.
*
* @param reason - A string describing why the registration failed.
*
* @example
* ```typescript
* const config = new IterableConfig();
* config.onTokenRegistrationFailed = (reason) => {
* console.error('Push registration failed:', reason);
* };
* Iterable.initialize('<YOUR_API_KEY>', config);
* ```
*/
onTokenRegistrationFailed?: (reason: string) => void;

/**
* Converts the IterableConfig instance to a dictionary object.
*
Expand Down
2 changes: 2 additions & 0 deletions src/core/enums/IterableEventName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ export enum IterableEventName {
handleEmbeddedMessageUpdateCalled = 'handleEmbeddedMessageUpdateCalled',
/** Event that fires when embedded messaging is disabled */
handleEmbeddedMessagingDisabledCalled = 'handleEmbeddedMessagingDisabledCalled',
/** Event that fires when push token registration fails */
handleTokenRegistrationFailedCalled = 'handleTokenRegistrationFailedCalled',
}
Loading