@@ -206,10 +206,12 @@ @implementation OneSignal
206206BOOL requestedProvisionalAuthorization = false ;
207207BOOL usesAutoPrompt = false ;
208208
209+ static BOOL requiresUserIdAuth = false ;
209210static BOOL providesAppNotificationSettings = false ;
210211
211212static BOOL performedOnSessionRequest = false ;
212213static NSString *pendingExternalUserId;
214+ static NSString *pendingExternalUserIdHashToken;
213215
214216static OSNotificationDisplayType _inFocusDisplayType = OSNotificationDisplayTypeInAppAlert;
215217+ (void )setInFocusDisplayType : (OSNotificationDisplayType)value {
@@ -493,6 +495,7 @@ + (void)clearStatics {
493495 sessionLaunchTime = [NSDate date ];
494496 performedOnSessionRequest = false ;
495497 pendingExternalUserId = nil ;
498+ pendingExternalUserIdHashToken = nil ;
496499
497500 _trackerFactory = nil ;
498501 _sessionManager = nil ;
@@ -844,6 +847,9 @@ + (void)downloadIOSParamsWithAppId:(NSString *)appId {
844847 delayedEmailParameters = nil ;
845848 }
846849 }
850+ if (result[IOS_REQUIRES_USER_ID_AUTHENTICATION]) {
851+ requiresUserIdAuth = [result[IOS_REQUIRES_USER_ID_AUTHENTICATION] boolValue ];
852+ }
847853
848854 if (!usesAutoPrompt && result[IOS_USES_PROVISIONAL_AUTHORIZATION] != (id )[NSNull null ]) {
849855 [OneSignalUserDefaults.initStandard saveBoolForKey: OSUD_USES_PROVISIONAL_PUSH_AUTHORIZATION withValue: [result[IOS_USES_PROVISIONAL_AUTHORIZATION] boolValue ]];
@@ -1676,7 +1682,11 @@ + (void)registerUserInternal {
16761682 if (pendingExternalUserId && ![self .existingPushExternalUserId isEqualToString: pendingExternalUserId])
16771683 dataDic[@" external_user_id" ] = pendingExternalUserId;
16781684
1685+ if (pendingExternalUserIdHashToken)
1686+ dataDic[@" external_user_id_auth_hash" ] = pendingExternalUserIdHashToken;
1687+
16791688 pendingExternalUserId = nil ;
1689+ pendingExternalUserIdHashToken = nil ;
16801690
16811691 let deviceModel = [OneSignalHelper getDeviceVariant ];
16821692 if (deviceModel)
@@ -2609,41 +2619,56 @@ + (void)setExternalUserId:(NSString * _Nonnull)externalId {
26092619 if ([self shouldLogMissingPrivacyConsentErrorWithMethodName: @" setExternalUserId:" ])
26102620 return ;
26112621
2612- [self setExternalUserId: externalId withCompletion: nil ];
2622+ [self setExternalUserId: externalId withSuccess: nil withFailure: nil ];
2623+ }
2624+
2625+ + (void )setExternalUserId : (NSString * _Nonnull)externalId withSuccess : (OSUpdateExternalUserIdSuccessBlock _Nullable)successBlock withFailure : (OSUpdateExternalUserIdFailureBlock _Nullable)failureBlock {
2626+ // return if the user has not granted privacy permissions
2627+ if ([self shouldLogMissingPrivacyConsentErrorWithMethodName: @" setExternalUserId:withSuccess:withFailure:" ])
2628+ return ;
2629+
2630+ [self setExternalUserId: externalId withExternalIdAuthHashToken: nil withSuccess: successBlock withFailure: failureBlock];
26132631}
26142632
2615- + (void )setExternalUserId : (NSString * _Nonnull )externalId withCompletion : (OSUpdateExternalUserIdBlock _Nullable)completionBlock {
2633+ + (void )setExternalUserId : (NSString *)externalId withExternalIdAuthHashToken : ( NSString *) hashToken withSuccess : (OSUpdateExternalUserIdSuccessBlock _Nullable)successBlock withFailure : (OSUpdateExternalUserIdFailureBlock _Nullable) failureBlock {
26162634
26172635 // return if the user has not granted privacy permissions
2618- if ([self shouldLogMissingPrivacyConsentErrorWithMethodName: @" setExternalUserId:withCompletion :" ])
2636+ if ([self shouldLogMissingPrivacyConsentErrorWithMethodName: @" setExternalUserId:withExternalIdAuthHashToken:withSuccess:withFailure :" ])
26192637 return ;
26202638
26212639 // Can't set the external id if init is not done or the app id or user id has not ben set yet
26222640 if (!performedOnSessionRequest) {
26232641 // will be sent as part of the registration/on_session request
26242642 pendingExternalUserId = externalId;
2643+ pendingExternalUserIdHashToken = hashToken;
26252644 return ;
26262645 } else if (!self.currentSubscriptionState .userId || !self.app_id ) {
26272646 [OneSignal onesignal_Log: ONE_S_LL_WARN message: [NSString stringWithFormat: @" Attempted to set external user id, but %@ is not set" , self .app_id == nil ? @" app_id" : @" user_id" ]];
2647+ if (failureBlock)
2648+ failureBlock ([NSError errorWithDomain: @" com.onesignal" code: 0 userInfo: @{@" error" : [NSString stringWithFormat: @" %@ is not set" , self .app_id == nil ? @" app_id" : @" user_id" ]}]);
2649+ return ;
2650+ } else if (requiresUserIdAuth && (!hashToken || hashToken.length == 0 )) {
2651+ [OneSignal onesignal_Log: ONE_S_LL_ERROR message: @" External Id authentication (auth token) is set to REQUIRED for this application. Please provide an auth token from your backend server or change the setting in the OneSignal dashboard." ];
2652+ if (failureBlock)
2653+ failureBlock ([NSError errorWithDomain: @" com.onesignal.externalUserId" code: 0 userInfo: @{@" error" : @" External User Id authentication (auth token) is set to REQUIRED for this application. Please provide an auth token from your backend server or change the setting in the OneSignal dashboard." }]);
26282654 return ;
26292655 }
26302656
26312657 // Begin constructing the request for the external id update
26322658 let requests = [NSMutableDictionary new ];
2633- requests[@" push" ] = [OSRequestUpdateExternalUserId withUserId: externalId withOneSignalUserId: self .currentSubscriptionState.userId appId: self .app_id];
2659+ requests[@" push" ] = [OSRequestUpdateExternalUserId withUserId: externalId withUserIdHashToken: hashToken withOneSignalUserId: self .currentSubscriptionState.userId appId: self .app_id];
26342660
26352661 // Check if the email has been set, this will decide on updtaing the external id for the email channel
26362662 if ([self isEmailSetup ])
2637- requests[@" email" ] = [OSRequestUpdateExternalUserId withUserId: externalId withOneSignalUserId: self .currentEmailSubscriptionState.emailUserId appId: self .app_id];
2663+ requests[@" email" ] = [OSRequestUpdateExternalUserId withUserId: externalId withUserIdHashToken: hashToken withOneSignalUserId: self .currentEmailSubscriptionState.emailUserId appId: self .app_id];
26382664
26392665 // Make sure this is not a duplicate request, if the email and push channels are aligned correctly with the same external id
26402666 if (![self shouldUpdateExternalUserId: externalId withRequests: requests]) {
26412667 // Use callback to return success for both cases here, since push and
26422668 // email (if email is not setup, email is not included) have been set already
26432669 let results = [self getDuplicateExternalUserIdResponse: externalId withRequests: requests];
2644- if (completionBlock)
2645- completionBlock (results);
2646-
2670+ if (successBlock)
2671+ successBlock (results);
26472672 return ;
26482673 }
26492674
@@ -2654,8 +2679,8 @@ + (void)setExternalUserId:(NSString * _Nonnull)externalId withCompletion:(OSUpda
26542679 if (results[@" email" ] && results[@" email" ][@" success" ] && [results[@" email" ][@" success" ] boolValue ])
26552680 [OneSignalUserDefaults.initStandard saveStringForKey: OSUD_EMAIL_EXTERNAL_USER_ID withValue: externalId];
26562681
2657- if (completionBlock )
2658- completionBlock (results);
2682+ if (successBlock )
2683+ successBlock (results);
26592684 }];
26602685}
26612686
@@ -2664,15 +2689,15 @@ + (void)removeExternalUserId {
26642689 if ([self shouldLogMissingPrivacyConsentErrorWithMethodName: @" removeExternalUserId" ])
26652690 return ;
26662691
2667- [self setExternalUserId: @" " withCompletion: nil ];
2692+ [self setExternalUserId: @" " ];
26682693}
26692694
2670- + (void )removeExternalUserId : (OSUpdateExternalUserIdBlock _Nullable)completionBlock {
2695+ + (void )removeExternalUserId : (OSUpdateExternalUserIdSuccessBlock _Nullable)successBlock withFailure : (OSUpdateExternalUserIdFailureBlock _Nullable) failureBlock {
26712696 // return if the user has not granted privacy permissions
26722697 if ([self shouldLogMissingPrivacyConsentErrorWithMethodName: @" removeExternalUserId:" ])
26732698 return ;
26742699
2675- [self setExternalUserId: @" " withCompletion: completionBlock ];
2700+ [self setExternalUserId: @" " withSuccess: successBlock withFailure: failureBlock ];
26762701}
26772702
26782703+ (NSString *)existingPushExternalUserId {
0 commit comments