Skip to content

Commit a51d7f7

Browse files
committed
fix: Retry IAM fetch when OneSignal ID becomes available
When getInAppMessagesFromServer is called early in the app lifecycle while users are changing (e.g., login is called after initialize), the OneSignal ID may not be available yet, causing the fetch to fail silently. This fix ensures in-app messages are eventually fetched by: - Adding OSUserStateObserver to OSMessagingController - Storing the subscription ID when fetch fails due to missing OneSignal ID - Retrying the fetch when user state changes and OneSignal ID becomes available This prevents in-app messages from never being fetched when users are identified early in the app startup lifecycle.
1 parent f731bc5 commit a51d7f7

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

iOS_SDK/OneSignalSDK/OneSignalInAppMessages/Controller/OSMessagingController.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ NS_ASSUME_NONNULL_BEGIN
3939

4040
@end
4141

42-
@interface OSMessagingController : NSObject <OSInAppMessageViewControllerDelegate, OSTriggerControllerDelegate, OSMessagingControllerDelegate, OSPushSubscriptionObserver>
42+
@interface OSMessagingController : NSObject <OSInAppMessageViewControllerDelegate, OSTriggerControllerDelegate, OSMessagingControllerDelegate, OSPushSubscriptionObserver, OSUserStateObserver>
4343

4444
@property (class, readonly) BOOL isInAppMessagingPaused;
4545

iOS_SDK/OneSignalSDK/OneSignalInAppMessages/Controller/OSMessagingController.m

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ @interface OSMessagingController ()
146146

147147
@property (nonatomic) BOOL calledLoadTags;
148148

149+
/// set when we attempt getInAppMessagesFromServer and no onesignal ID is available yet
150+
@property (strong, nonatomic, nullable) NSString *shouldFetchOnUserChangeWithSubscriptionID;
151+
149152
@end
150153

151154
@implementation OSMessagingController
@@ -175,6 +178,7 @@ + (void)removeInstance {
175178
+ (void)start {
176179
OSMessagingController *shared = OSMessagingController.sharedInstance;
177180
[OneSignalUserManagerImpl.sharedInstance.pushSubscriptionImpl addObserver:shared];
181+
[OneSignalUserManagerImpl.sharedInstance addObserver:shared];
178182
}
179183

180184
static BOOL _isInAppMessagingPaused = false;
@@ -254,8 +258,10 @@ - (void)getInAppMessagesFromServer:(NSString *)subscriptionId {
254258
OSConsistencyManager *consistencyManager = [OSConsistencyManager shared];
255259
NSString *onesignalId = OneSignalUserManagerImpl.sharedInstance.onesignalId;
256260

261+
// NOTE: Check for subscription ID above first, before checking for OneSignal ID next
257262
if (!onesignalId) {
258-
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"Failed to get in app messages due to no OneSignal ID"];
263+
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"Failed to get in app messages due to no OneSignal ID, will reattempt"];
264+
self.shouldFetchOnUserChangeWithSubscriptionID = subscriptionId;
259265
return;
260266
}
261267

@@ -1198,6 +1204,15 @@ - (void)onPushSubscriptionDidChangeWithState:(OSPushSubscriptionChangedState * _
11981204
[self getInAppMessagesFromServer:state.current.id];
11991205
}
12001206

1207+
- (void)onUserStateDidChangeWithState:(OSUserChangedState * _Nonnull)state {
1208+
if (state.current.onesignalId != nil && self.shouldFetchOnUserChangeWithSubscriptionID) {
1209+
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"OSMessagingController onUserStateDidChangeWithState: changed to new valid onesignal id"];
1210+
NSString *subscriptionID = self.shouldFetchOnUserChangeWithSubscriptionID;
1211+
self.shouldFetchOnUserChangeWithSubscriptionID = nil;
1212+
[self getInAppMessagesFromServer:subscriptionID];
1213+
}
1214+
}
1215+
12011216
- (void)dealloc {
12021217
[[NSNotificationCenter defaultCenter] removeObserver:self];
12031218
}

0 commit comments

Comments
 (0)