Skip to content

Commit ac76834

Browse files
authored
Fixed iOS params issue related to the second init for wrappers (#594)
* Fixed params issue related to the second init * On first init call the app id is null form some wrappers * Now every time init is called we try to download the params if the `didCallDownloadParams` false * Fix for OneSignal wrappers in native init * Wrapper SDKs call init twice, once with a null app id and once with a valid app id - On first init: null app id prevents iOS params and initDone will be set true - On second init: valid app id will allow iOS params to be pulled down and init is done already * Log level is now being set to default VERBOSE in NSE - This is so any logs get recorded for the device even when coming from the NSE - The customer will have control app level for showing OneSignal logs, but NSE will always log out logs * Unit test written for checking that init called twice (once with null app id and once with valid app id) does not effect iOS params request * Updated the log when entering the NSE of the SDK
1 parent 80af6a8 commit ac76834

File tree

6 files changed

+77
-29
lines changed

6 files changed

+77
-29
lines changed
Binary file not shown.

iOS_SDK/OneSignalSDK/Source/OneSignal.m

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,6 @@ @implementation OneSignal
160160
// Under Capabilities is "Background Modes" > "Remote notifications" enabled.
161161
static BOOL backgroundModesEnabled = false;
162162

163-
// indicates if the GetiOSParams request has completed
164-
static BOOL downloadedParameters = false;
165-
static BOOL didCallDownloadParameters = false;
166-
167163
static BOOL promptBeforeOpeningPushURLs = false;
168164

169165

@@ -332,6 +328,24 @@ + (void)setMSubscriptionStatus:(NSNumber*)status {
332328
mSubscriptionStatus = [status intValue];
333329
}
334330

331+
/*
332+
Indicates if the iOS params request has started
333+
Set to true when the method is called and set false if the request's failure callback is triggered
334+
*/
335+
static BOOL _didCallDownloadParameters = false;
336+
+ (BOOL)didCallDownloadParameters {
337+
return _didCallDownloadParameters;
338+
}
339+
340+
/*
341+
Indicates if the iOS params request is complete
342+
Set to true when the request's success callback is triggered
343+
*/
344+
static BOOL _downloadedParameters = false;
345+
+ (BOOL)downloadedParameters {
346+
return _downloadedParameters;
347+
}
348+
335349
static OneSignalReceiveReceiptsController* _receiveReceiptsController;
336350
+ (OneSignalReceiveReceiptsController*)receiveReceiptsController {
337351
if (!_receiveReceiptsController)
@@ -428,7 +442,8 @@ + (void)clearStatics {
428442

429443
_permissionStateChangesObserver = nil;
430444

431-
didCallDownloadParameters = false;
445+
_downloadedParameters = false;
446+
_didCallDownloadParameters = false;
432447

433448
maxApnsWait = APNS_TIMEOUT;
434449
reattemptRegistrationInterval = REGISTRATION_DELAY_SECONDS;
@@ -520,6 +535,11 @@ + (id)initWithLaunchOptions:(NSDictionary*)launchOptions
520535
if (!success)
521536
return self;
522537

538+
// Wrapper SDK's call init twice and pass null as the appId on the first call
539+
// the app ID is required to download parameters, so do not download params until the appID is provided
540+
if (!_didCallDownloadParameters && appId && appId != (id)[NSNull null])
541+
[self downloadIOSParamsWithAppId:appId];
542+
523543
if (initDone)
524544
return self;
525545
initDone = true;
@@ -529,11 +549,6 @@ + (id)initWithLaunchOptions:(NSDictionary*)launchOptions
529549
// Outcomes init
530550
_sessionManager = [[OneSignalSessionManager alloc] init:self];
531551
_outcomeEventsController = [[OneSignalOutcomeEventsController alloc] init:self.sessionManager];
532-
533-
// Some wrapper SDK's call init multiple times and pass nil/NSNull as the appId on the first call
534-
// the app ID is required to download parameters, so do not download params until the appID is provided
535-
if (!didCallDownloadParameters && appId != nil && appId != (id)[NSNull null])
536-
[self downloadIOSParamsWithAppId:appId];
537552

538553
if (appId && mShareLocation)
539554
[OneSignalLocation getLocation:false];
@@ -597,7 +612,6 @@ + (id)initWithLaunchOptions:(NSDictionary*)launchOptions
597612
[self registerUser];
598613
else {
599614
[self.osNotificationSettings getNotificationPermissionState:^(OSPermissionState *state) {
600-
601615
if (state.answeredPrompt) {
602616
[self registerUser];
603617
} else {
@@ -664,6 +678,8 @@ + (bool)initAppId:(NSString*)appId withSettings:(NSDictionary*)settings {
664678
// Will also run the first time OneSignal is initialized
665679
if (app_id && ![app_id isEqualToString:prevAppId]) {
666680
initDone = false;
681+
_downloadedParameters = false;
682+
_didCallDownloadParameters = false;
667683
let sharedUserDefaults = OneSignalUserDefaults.initShared;
668684

669685
[standardUserDefaults saveStringForKey:NSUD_APP_ID withValue:app_id];
@@ -762,9 +778,9 @@ + (void)checkIfApplicationImplementsDeprecatedMethods {
762778
});
763779
}
764780

765-
+(void)downloadIOSParamsWithAppId:(NSString *)appId {
781+
+ (void)downloadIOSParamsWithAppId:(NSString *)appId {
766782
[self onesignal_Log:ONE_S_LL_DEBUG message:@"Downloading iOS parameters for this application"];
767-
didCallDownloadParameters = true;
783+
_didCallDownloadParameters = true;
768784

769785
[OneSignalClient.sharedClient executeRequest:[OSRequestGetIosParams withUserId:self.currentSubscriptionState.userId appId:appId] onSuccess:^(NSDictionary *result) {
770786
if (result[IOS_REQUIRES_EMAIL_AUTHENTICATION]) {
@@ -777,24 +793,22 @@ +(void)downloadIOSParamsWithAppId:(NSString *)appId {
777793
}
778794
}
779795

780-
if (!usesAutoPrompt &&
781-
result[IOS_USES_PROVISIONAL_AUTHORIZATION] &&
782-
result[IOS_USES_PROVISIONAL_AUTHORIZATION] != [NSNull null] &&
783-
[result[IOS_USES_PROVISIONAL_AUTHORIZATION] boolValue]) {
784-
785-
[OneSignalUserDefaults.initStandard saveBoolForKey:USES_PROVISIONAL_AUTHORIZATION withValue:true];
796+
if (!usesAutoPrompt && result[IOS_USES_PROVISIONAL_AUTHORIZATION] != (id)[NSNull null]) {
797+
[OneSignalUserDefaults.initStandard saveBoolForKey:USES_PROVISIONAL_AUTHORIZATION withValue:[result[IOS_USES_PROVISIONAL_AUTHORIZATION] boolValue]];
786798

787799
[self checkProvisionalAuthorizationStatus];
788800
}
789801

790-
if (result[IOS_RECEIVE_RECEIPTS_ENABLE])
791-
[OneSignalUserDefaults.initShared saveBoolForKey:ONESIGNAL_ENABLE_RECEIVE_RECEIPTS withValue:true];
802+
if (result[IOS_RECEIVE_RECEIPTS_ENABLE] != (id)[NSNull null])
803+
[OneSignalUserDefaults.initShared saveBoolForKey:ONESIGNAL_ENABLE_RECEIVE_RECEIPTS withValue:[result[IOS_RECEIVE_RECEIPTS_ENABLE] boolValue]];
792804

793805
[OSOutcomesUtils saveOutcomeParamsForApp:result];
794806
[OneSignalTrackFirebaseAnalytics updateFromDownloadParams:result];
795807

796-
downloadedParameters = true;
797-
} onFailure:nil];
808+
_downloadedParameters = true;
809+
} onFailure:^(NSError *error) {
810+
_didCallDownloadParameters = false;
811+
}];
798812
}
799813

800814
+ (void)setLogLevel:(ONE_S_LOG_LEVEL)nsLogLevel visualLevel:(ONE_S_LOG_LEVEL)visualLogLevel {
@@ -2290,7 +2304,7 @@ + (void)setEmail:(NSString * _Nonnull)email withEmailAuthHashToken:(NSString * _
22902304
and we do not need to delay the request
22912305
*/
22922306

2293-
if (!self.currentSubscriptionState.userId || (downloadedParameters == false && emailAuthToken != nil)) {
2307+
if (!self.currentSubscriptionState.userId || (_downloadedParameters == false && emailAuthToken != nil)) {
22942308
[self onesignal_Log:ONE_S_LL_VERBOSE message:@"iOS Parameters for this application has not yet been downloaded. Delaying call to setEmail: until the parameters have been downloaded."];
22952309
delayedEmailParameters = [OneSignalSetEmailParameters withEmail:email withAuthToken:emailAuthToken withSuccess:successBlock withFailure:failureBlock];
22962310
return;

iOS_SDK/OneSignalSDK/Source/OneSignalInternal.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@
6666

6767
+ (NSDate *)sessionLaunchTime;
6868

69+
// Indicates if the app provides its own custom Notification customization settings UI
70+
// To enable this, set kOSSettingsKeyProvidesAppNotificationSettings to true in init.
71+
+ (BOOL)providesAppNotificationSettings;
72+
73+
@property (class, readonly) BOOL didCallDownloadParameters;
74+
@property (class, readonly) BOOL downloadedParameters;
75+
6976
@property (class) NSObject<OneSignalNotificationSettings>* osNotificationSettings;
7077
@property (class) OSPermissionState* currentPermissionState;
7178

@@ -75,10 +82,6 @@
7582
@property (class) OneSignalSessionManager* sessionManager;
7683
@property (class) OneSignalOutcomeEventsController* outcomeEventsController;
7784

78-
// Indicates if the app provides its own custom Notification customization settings UI
79-
// To enable this, set kOSSettingsKeyProvidesAppNotificationSettings to true in init.
80-
+ (BOOL)providesAppNotificationSettings;
81-
8285
@end
8386

8487

iOS_SDK/OneSignalSDK/Source/OneSignalNotificationServiceExtensionHandler.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ @implementation OneSignalNotificationServiceExtensionHandler
3939

4040
+ (UNMutableNotificationContent*)didReceiveNotificationExtensionRequest:(UNNotificationRequest*)request
4141
withMutableNotificationContent:(UNMutableNotificationContent*)replacementContent {
42+
// Set default log level of NSE to VERBOSE so we get all logs from NSE logic
43+
[OneSignal setLogLevel:ONE_S_LL_VERBOSE visualLevel:ONE_S_LL_NONE];
44+
[OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:@"NSE request received, setting OneSignal log level to VERBOSE!"];
45+
4246
if (!replacementContent)
4347
replacementContent = [request.content mutableCopy];
4448

@@ -50,6 +54,7 @@ + (UNMutableNotificationContent*)didReceiveNotificationExtensionRequest:(UNNotif
5054
// Track receieved
5155
[OneSignalTrackFirebaseAnalytics trackReceivedEvent:payload];
5256

57+
// Get and check the received notification id
5358
let receivedNotificationId = payload.notificationID;
5459
if (receivedNotificationId && ![receivedNotificationId isEqualToString:@""]) {
5560
// Track confirmed delivery

iOS_SDK/OneSignalSDK/Source/OneSignalReceiveReceiptsController.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ - (void)sendReceiveReceiptWithPlayerId:(nonnull NSString *)playerId
7575
[OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:@"Receieve receipts disabled"];
7676
return;
7777
}
78-
78+
7979
let request = [OSRequestReceiveReceipts withPlayerId:playerId notificationId:notificationId appId:appId];
8080
[OneSignalClient.sharedClient executeRequest:request onSuccess:^(NSDictionary *result) {
8181
if (success)

iOS_SDK/OneSignalSDK/UnitTests/UnitTests.m

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#import "OneSignal.h"
3838
#import "OneSignalHelper.h"
3939
#import "OneSignalTracker.h"
40+
#import "OneSignalInternal.h"
4041
#import "NSString+OneSignal.h"
4142
#import "UnitTestCommonMethods.h"
4243
#import "OneSignalSelectorHelpers.h"
@@ -1820,6 +1821,31 @@ - (void) testDidReceiveNotificationExtensionRequestDontOverrideCateogory {
18201821
XCTAssertEqualObjects(content.attachments[0].URL.scheme, @"file");
18211822
}
18221823

1824+
/*
1825+
Wrapper SDKs call OneSignal init method twice:
1826+
1. App id is null
1827+
2. App id should be valid
1828+
NOTE: The init method uses flags initDone, didCallDownloadParameters, downloadedParameters and these prevent code from executing more than once in specific cases
1829+
initDone BOOL is used to return early in the event of init being called more than once
1830+
didCallDownloadParameters BOOL is used to determine whether iOS params have started being pulled down
1831+
downloadedParameters BOOL is used to determine whether iOS params have successfully been pulled down
1832+
*/
1833+
- (void)testiOSParams_withNullAppIdInit_andValidAppIdInit {
1834+
// 1. Open app and init with null app id
1835+
[OneSignal initWithLaunchOptions:nil appId:nil];
1836+
[UnitTestCommonMethods foregroundApp];
1837+
1838+
// 2. Make sure iOS params did not download, since app id was invalid
1839+
XCTAssertFalse(OneSignal.didCallDownloadParameters);
1840+
XCTAssertFalse(OneSignal.downloadedParameters);
1841+
1842+
// 3. Init with valid app id
1843+
[OneSignal initWithLaunchOptions:nil appId:@"b2f7f966-d8cc-11e4-bed1-df8f05be55ba"];
1844+
1845+
// 4. Make sure iOS params have been downloaded, since app_id is valid
1846+
XCTAssertTrue(OneSignal.didCallDownloadParameters);
1847+
XCTAssertTrue(OneSignal.downloadedParameters);
1848+
}
18231849

18241850
- (void)testAddingSharedKeysIfMissing {
18251851
// 1. Init SDK as normal

0 commit comments

Comments
 (0)