diff --git a/android/src/main/java/com/iterable/reactnative/RNIterableAPIModuleImpl.java b/android/src/main/java/com/iterable/reactnative/RNIterableAPIModuleImpl.java index caef783e6..e78fbb482 100644 --- a/android/src/main/java/com/iterable/reactnative/RNIterableAPIModuleImpl.java +++ b/android/src/main/java/com/iterable/reactnative/RNIterableAPIModuleImpl.java @@ -373,6 +373,11 @@ public void disableDeviceForCurrentUser() { IterableApi.getInstance().disablePush(); } + public void registerForPush() { + IterableLogger.v(TAG, "registerForPush"); + IterableApi.getInstance().registerForPush(); + } + public void handleAppLink(String uri, Promise promise) { IterableLogger.printInfo(); promise.resolve(IterableApi.getInstance().handleAppLink(uri)); diff --git a/android/src/newarch/java/com/RNIterableAPIModule.java b/android/src/newarch/java/com/RNIterableAPIModule.java index 9abe0d070..a8d876922 100644 --- a/android/src/newarch/java/com/RNIterableAPIModule.java +++ b/android/src/newarch/java/com/RNIterableAPIModule.java @@ -137,6 +137,11 @@ public void disableDeviceForCurrentUser() { moduleImpl.disableDeviceForCurrentUser(); } + @Override + public void registerForPush() { + moduleImpl.registerForPush(); + } + @Override public void handleAppLink(String uri, Promise promise) { moduleImpl.handleAppLink(uri, promise); diff --git a/android/src/oldarch/java/com/RNIterableAPIModule.java b/android/src/oldarch/java/com/RNIterableAPIModule.java index ce0a6280c..faee39779 100644 --- a/android/src/oldarch/java/com/RNIterableAPIModule.java +++ b/android/src/oldarch/java/com/RNIterableAPIModule.java @@ -138,6 +138,11 @@ public void disableDeviceForCurrentUser() { moduleImpl.disableDeviceForCurrentUser(); } + @ReactMethod + public void registerForPush() { + moduleImpl.registerForPush(); + } + @ReactMethod public void handleAppLink(String uri, Promise promise) { moduleImpl.handleAppLink(uri, promise); diff --git a/ios/RNIterableAPI/RNIterableAPI.mm b/ios/RNIterableAPI/RNIterableAPI.mm index 025c4dc15..9c940c514 100644 --- a/ios/RNIterableAPI/RNIterableAPI.mm +++ b/ios/RNIterableAPI/RNIterableAPI.mm @@ -230,6 +230,10 @@ - (void)disableDeviceForCurrentUser { [_swiftAPI disableDeviceForCurrentUser]; } +- (void)registerForPush { + [_swiftAPI registerForPush]; +} + - (void)getLastPushPayload:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject { [_swiftAPI getLastPushPayload:resolve rejecter:reject]; @@ -512,6 +516,10 @@ - (void)wakeApp { [_swiftAPI disableDeviceForCurrentUser]; } +RCT_EXPORT_METHOD(registerForPush) { + [_swiftAPI registerForPush]; +} + RCT_EXPORT_METHOD(getLastPushPayload : (RCTPromiseResolveBlock) resolve reject : (RCTPromiseRejectBlock)reject) { [_swiftAPI getLastPushPayload:resolve rejecter:reject]; diff --git a/ios/RNIterableAPI/ReactIterableAPI.swift b/ios/RNIterableAPI/ReactIterableAPI.swift index c7b5fc745..fe6a685eb 100644 --- a/ios/RNIterableAPI/ReactIterableAPI.swift +++ b/ios/RNIterableAPI/ReactIterableAPI.swift @@ -144,6 +144,14 @@ import React IterableAPI.disableDeviceForCurrentUser() } + @objc(registerForPush) + public func registerForPush() { + ITBInfo() + DispatchQueue.main.async { + UIApplication.shared.registerForRemoteNotifications() + } + } + @objc(getLastPushPayload:rejecter:) public func getLastPushPayload(resolver: RCTPromiseResolveBlock, rejecter: RCTPromiseRejectBlock) { diff --git a/src/__mocks__/MockRNIterableAPI.ts b/src/__mocks__/MockRNIterableAPI.ts index 708ab56ad..94fa7f41c 100644 --- a/src/__mocks__/MockRNIterableAPI.ts +++ b/src/__mocks__/MockRNIterableAPI.ts @@ -34,6 +34,8 @@ export class MockRNIterableAPI { static disableDeviceForCurrentUser = jest.fn(); + static registerForPush = jest.fn(); + static trackPushOpenWithCampaignId = jest.fn(); static updateCart = jest.fn(); diff --git a/src/api/NativeRNIterableAPI.ts b/src/api/NativeRNIterableAPI.ts index a804922ae..08bffadf7 100644 --- a/src/api/NativeRNIterableAPI.ts +++ b/src/api/NativeRNIterableAPI.ts @@ -110,6 +110,7 @@ export interface Spec extends TurboModule { // Device management disableDeviceForCurrentUser(): void; + registerForPush(): void; getLastPushPayload(): Promise<{ [key: string]: string | number | boolean; } | null>; diff --git a/src/core/classes/Iterable.test.ts b/src/core/classes/Iterable.test.ts index 20ad805d7..e4e0086dd 100644 --- a/src/core/classes/Iterable.test.ts +++ b/src/core/classes/Iterable.test.ts @@ -142,6 +142,16 @@ describe('Iterable', () => { }); }); + describe('registerForPush', () => { + it('should re-register the device for push notifications', () => { + // GIVEN no parameters + // WHEN Iterable.registerForPush is called + Iterable.registerForPush(); + // THEN corresponding method is called on RNIterableAPI + expect(MockRNIterableAPI.registerForPush).toBeCalled(); + }); + }); + describe('getLastPushPayload', () => { it('should return the last push payload', async () => { const result = { var1: 'val1', var2: true }; diff --git a/src/core/classes/Iterable.ts b/src/core/classes/Iterable.ts index 983fab49b..6a19131dc 100644 --- a/src/core/classes/Iterable.ts +++ b/src/core/classes/Iterable.ts @@ -345,6 +345,26 @@ export class Iterable { IterableApi.disableDeviceForCurrentUser(); } + /** + * Re-register the device for push notifications for the current user. + * + * This method can be used to re-enable push notifications after they have been + * disabled by calling `disableDeviceForCurrentUser`. It triggers the native + * push registration flow, re-registering the device token with Iterable. + * + * @example + * ```typescript + * // First disable push + * Iterable.disableDeviceForCurrentUser(); + * + * // Later, re-enable push + * Iterable.registerForPush(); + * ``` + */ + static registerForPush() { + IterableApi.registerForPush(); + } + /** * Get the payload of the last push notification with which the user * opened the application (by clicking an action button, etc.). diff --git a/src/core/classes/IterableApi.test.ts b/src/core/classes/IterableApi.test.ts index 77a229c58..1427abb58 100644 --- a/src/core/classes/IterableApi.test.ts +++ b/src/core/classes/IterableApi.test.ts @@ -247,6 +247,17 @@ describe('IterableApi', () => { }); }); + describe('registerForPush', () => { + it('should call RNIterableAPI.registerForPush', () => { + // GIVEN no parameters + // WHEN registerForPush is called + IterableApi.registerForPush(); + + // THEN RNIterableAPI.registerForPush is called + expect(MockRNIterableAPI.registerForPush).toBeCalled(); + }); + }); + describe('updateUser', () => { it('should call RNIterableAPI.updateUser with data fields and merge flag', () => { // GIVEN data fields and merge flag diff --git a/src/core/classes/IterableApi.ts b/src/core/classes/IterableApi.ts index 88820c9f3..520aaec89 100644 --- a/src/core/classes/IterableApi.ts +++ b/src/core/classes/IterableApi.ts @@ -137,6 +137,15 @@ export class IterableApi { return RNIterableAPI.disableDeviceForCurrentUser(); } + /** + * Re-register the device for push notifications for the current user. + * This can be used to re-enable push notifications after calling disableDeviceForCurrentUser. + */ + static registerForPush() { + IterableLogger.log('registerForPush'); + return RNIterableAPI.registerForPush(); + } + /** * Save data to the current user's Iterable profile. *