Skip to content

Commit 7c16109

Browse files
committed
Fixed issues with calling methods before init
* Fixed crash with calling setSubscription before OneSignal initWithLaunchOptions * Fixed issue with device not subscribing if promptForPushNotificationsWithUserResponse was called before initWithLaunchOptions
1 parent a65b469 commit 7c16109

File tree

5 files changed

+81
-27
lines changed

5 files changed

+81
-27
lines changed

iOS_SDK/OneSignalDevApp/OneSignalDevApp/AppDelegate.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
4444
};
4545

4646

47+
[OneSignal setSubscription:true];
48+
4749
[OneSignal initWithLaunchOptions:launchOptions
4850
appId:@"b2f7f966-d8cc-11e4-bed1-df8f05be55ba"
4951
handleNotificationAction:openNotificationHandler

iOS_SDK/OneSignalDevApp/OneSignalDevApp/ViewController.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ - (IBAction)sendTagButton:(id)sender {
4848

4949
//[OneSignal registerForPushNotifications];
5050

51-
[OneSignal promptForPushNotificationWithUserResponse:^(BOOL accepted) {
52-
NSLog(@"NEW SDK 2.5.0 METHDO: promptForPushNotificationWithUserResponse: %d", accepted);
51+
[OneSignal promptForPushNotificationsWithUserResponse:^(BOOL accepted) {
52+
NSLog(@"NEW SDK 2.5.0 METHDO: promptForPushNotificationsWithUserResponse: %d", accepted);
5353
}];
5454

5555

@@ -59,7 +59,7 @@ - (IBAction)sendTagButton:(id)sender {
5959
onSuccess:^(NSDictionary *result) {
6060
static int successes = 0;
6161
NSLog(@"successes: %d", ++successes);
62-
}
62+
}
6363
onFailure:^(NSError *error) {
6464
static int failures = 0;
6565
NSLog(@"failures: %d", ++failures);

iOS_SDK/OneSignalSDK/Source/OneSignal.m

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ @implementation OneSignal
132132
static NSString* app_id;
133133
NSString* emailToSet;
134134
NSMutableDictionary* tagsToSend;
135-
OneSignalHTTPClient *httpClient;
136135
OSResultSuccessBlock tokenUpdateSuccessBlock;
137136
OSFailureBlock tokenUpdateFailureBlock;
138137

@@ -143,6 +142,15 @@ @implementation OneSignal
143142
BOOL disableBadgeClearing = NO;
144143
BOOL mShareLocation = YES;
145144

145+
146+
147+
static OneSignalHTTPClient *_httpClient;
148+
+ (OneSignalHTTPClient*)httpClient {
149+
if (!_httpClient)
150+
_httpClient = [OneSignalHTTPClient new];
151+
return _httpClient;
152+
}
153+
146154
static OSNotificationDisplayType _inFocusDisplayType = OSNotificationDisplayTypeInAppAlert;
147155
+ (void)setInFocusDisplayType:(OSNotificationDisplayType)value {
148156
NSInteger op = value;
@@ -200,6 +208,7 @@ + (OSSubscriptionState*)currentSubscriptionState {
200208
if (!_currentSubscriptionState) {
201209
_currentSubscriptionState = [OSSubscriptionState alloc];
202210
_currentSubscriptionState = [_currentSubscriptionState initAsToWithPermision:self.currentPermissionState.accepted];
211+
mLastNotificationTypes = _currentPermissionState.notificationTypes;
203212
[self.currentPermissionState.observable addObserver:_currentSubscriptionState];
204213
[_currentSubscriptionState.observable addObserver:[OSSubscriptionChangedInternalObserver alloc]];
205214
}
@@ -271,8 +280,11 @@ + (void) setWaitingForApnsResponse:(BOOL)value {
271280
}
272281

273282
+ (void)clearStatics {
283+
app_id = nil;
284+
_httpClient = nil;
274285
_osNotificationSettings = nil;
275286
waitingForApnsResponse = false;
287+
mLastNotificationTypes = -1;
276288

277289
_lastPermissionState = nil;
278290
_currentPermissionState = nil;
@@ -321,8 +333,6 @@ + (id)initWithLaunchOptions:(NSDictionary*)launchOptions appId:(NSString*)appId
321333
if (self) {
322334
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
323335

324-
httpClient = [[OneSignalHTTPClient alloc] init];
325-
326336
[OneSignalHelper notificationBlocks: receivedCallback : actionCallback];
327337

328338
if (appId)
@@ -633,7 +643,7 @@ + (void) sendTagsToServer {
633643
pendingSendTagCallbacks = nil;
634644

635645

636-
NSMutableURLRequest* request = [httpClient requestWithMethod:@"PUT" path:[NSString stringWithFormat:@"players/%@", self.currentSubscriptionState.userId]];
646+
NSMutableURLRequest* request = [self.httpClient requestWithMethod:@"PUT" path:[NSString stringWithFormat:@"players/%@", self.currentSubscriptionState.userId]];
637647

638648
NSDictionary* dataDic = [NSDictionary dictionaryWithObjectsAndKeys:
639649
app_id, @"app_id",
@@ -676,7 +686,7 @@ + (void)getTags:(OSResultSuccessBlock)successBlock onFailure:(OSFailureBlock)fai
676686
return;
677687

678688
NSMutableURLRequest* request;
679-
request = [httpClient requestWithMethod:@"GET" path:[NSString stringWithFormat:@"players/%@", self.currentSubscriptionState.userId]];
689+
request = [self.httpClient requestWithMethod:@"GET" path:[NSString stringWithFormat:@"players/%@", self.currentSubscriptionState.userId]];
680690

681691
[OneSignalHelper enqueueRequest:request onSuccess:^(NSDictionary* results) {
682692
if ([results objectForKey:@"tags"] != nil)
@@ -735,7 +745,7 @@ + (void)postNotification:(NSDictionary*)jsonData {
735745
}
736746

737747
+ (void)postNotification:(NSDictionary*)jsonData onSuccess:(OSResultSuccessBlock)successBlock onFailure:(OSFailureBlock)failureBlock {
738-
NSMutableURLRequest* request = [httpClient requestWithMethod:@"POST" path:@"notifications"];
748+
NSMutableURLRequest* request = [self.httpClient requestWithMethod:@"POST" path:@"notifications"];
739749

740750
NSMutableDictionary* dataDic = [[NSMutableDictionary alloc] initWithDictionary:jsonData];
741751
dataDic[@"app_id"] = dataDic[@"app_id"] ?: app_id;
@@ -807,7 +817,8 @@ + (void)setSubscription:(BOOL)enable {
807817

808818
self.currentSubscriptionState.userSubscriptionSetting = enable;
809819

810-
[OneSignal sendNotificationTypesUpdate];
820+
if (app_id)
821+
[OneSignal sendNotificationTypesUpdate];
811822
}
812823

813824

@@ -847,11 +858,6 @@ + (void) handleDidFailRegisterForRemoteNotification:(NSError*)err {
847858
[self.osNotificationSettings onAPNsResponse:false];
848859
}
849860

850-
+ (void)registerDeviceToken:(id)inDeviceToken onSuccess:(OSResultSuccessBlock)successBlock onFailure:(OSFailureBlock)failureBlock {
851-
waitingForApnsResponse = false;
852-
[self updateDeviceToken:inDeviceToken onSuccess:successBlock onFailure:failureBlock];
853-
}
854-
855861
+ (void)updateDeviceToken:(NSString*)deviceToken onSuccess:(OSResultSuccessBlock)successBlock onFailure:(OSFailureBlock)failureBlock {
856862
onesignal_Log(ONE_S_LL_VERBOSE, @"updateDeviceToken:onSuccess:onFailure:");
857863

@@ -886,7 +892,7 @@ + (void)updateDeviceToken:(NSString*)deviceToken onSuccess:(OSResultSuccessBlock
886892
self.currentSubscriptionState.pushToken = deviceToken;
887893

888894
NSMutableURLRequest* request;
889-
request = [httpClient requestWithMethod:@"PUT" path:[NSString stringWithFormat:@"players/%@", self.currentSubscriptionState.userId]];
895+
request = [self.httpClient requestWithMethod:@"PUT" path:[NSString stringWithFormat:@"players/%@", self.currentSubscriptionState.userId]];
890896

891897
int notificationTypes = [self getNotificationTypes];
892898

@@ -980,9 +986,9 @@ + (void)registerUserInternal {
980986

981987
NSMutableURLRequest* request;
982988
if (!self.currentSubscriptionState.userId)
983-
request = [httpClient requestWithMethod:@"POST" path:@"players"];
989+
request = [self.httpClient requestWithMethod:@"POST" path:@"players"];
984990
else
985-
request = [httpClient requestWithMethod:@"POST" path:[NSString stringWithFormat:@"players/%@/on_session", self.currentSubscriptionState.userId]];
991+
request = [self.httpClient requestWithMethod:@"POST" path:[NSString stringWithFormat:@"players/%@/on_session", self.currentSubscriptionState.userId]];
986992

987993
NSDictionary* infoDictionary = [[NSBundle mainBundle]infoDictionary];
988994
NSString* build = infoDictionary[(NSString*)kCFBundleVersionKey];
@@ -1115,11 +1121,12 @@ + (BOOL) sendNotificationTypesUpdate {
11151121
}
11161122

11171123
mLastNotificationTypes = [self getNotificationTypes];
1118-
NSMutableURLRequest* request = [httpClient requestWithMethod:@"PUT" path:[NSString stringWithFormat:@"players/%@", self.currentSubscriptionState.userId]];
1124+
NSMutableURLRequest* request = [self.httpClient requestWithMethod:@"PUT" path:[NSString stringWithFormat:@"players/%@", self.currentSubscriptionState.userId]];
11191125
NSDictionary* dataDic = [NSDictionary dictionaryWithObjectsAndKeys:
11201126
app_id, @"app_id",
11211127
@([self getNotificationTypes]), @"notification_types",
11221128
nil];
1129+
NSLog(@"dataDic: %@", dataDic);
11231130
NSData* postData = [NSJSONSerialization dataWithJSONObject:dataDic options:0 error:nil];
11241131
[request setHTTPBody:postData];
11251132

@@ -1138,7 +1145,7 @@ + (void)sendPurchases:(NSArray*)purchases {
11381145
if (!self.currentSubscriptionState.userId)
11391146
return;
11401147

1141-
NSMutableURLRequest* request = [httpClient requestWithMethod:@"POST" path:[NSString stringWithFormat:@"players/%@/on_purchase", self.currentSubscriptionState.userId]];
1148+
NSMutableURLRequest* request = [self.httpClient requestWithMethod:@"POST" path:[NSString stringWithFormat:@"players/%@/on_purchase", self.currentSubscriptionState.userId]];
11421149

11431150
NSDictionary *dataDic = [NSDictionary dictionaryWithObjectsAndKeys:
11441151
app_id, @"app_id",
@@ -1284,7 +1291,7 @@ + (void)submitNotificationOpened:(NSString*)messageId {
12841291
NSString* lastMessageId = [[NSUserDefaults standardUserDefaults] objectForKey:@"GT_LAST_MESSAGE_OPENED_"];
12851292
//Only submit request if messageId not nil and: (lastMessage is nil or not equal to current one)
12861293
if(messageId && (!lastMessageId || ![lastMessageId isEqualToString:messageId])) {
1287-
NSMutableURLRequest* request = [httpClient requestWithMethod:@"PUT" path:[NSString stringWithFormat:@"notifications/%@", messageId]];
1294+
NSMutableURLRequest* request = [self.httpClient requestWithMethod:@"PUT" path:[NSString stringWithFormat:@"notifications/%@", messageId]];
12881295
NSDictionary* dataDic = [NSDictionary dictionaryWithObjectsAndKeys:
12891296
app_id, @"app_id",
12901297
self.currentSubscriptionState.userId, @"player_id",
@@ -1391,8 +1398,15 @@ + (void)updateNotificationTypes:(int)notificationTypes {
13911398
+ (void)didRegisterForRemoteNotifications:(UIApplication*)app deviceToken:(NSData*)inDeviceToken {
13921399
NSString* trimmedDeviceToken = [[inDeviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
13931400
NSString* parsedDeviceToken = [[trimmedDeviceToken componentsSeparatedByString:@" "] componentsJoinedByString:@""];
1401+
13941402
[OneSignal onesignal_Log:ONE_S_LL_INFO message: [NSString stringWithFormat:@"Device Registered with Apple: %@", parsedDeviceToken]];
1395-
[OneSignal registerDeviceToken:parsedDeviceToken onSuccess:^(NSDictionary* results) {
1403+
1404+
waitingForApnsResponse = false;
1405+
1406+
if (!app_id)
1407+
return;
1408+
1409+
[OneSignal updateDeviceToken:parsedDeviceToken onSuccess:^(NSDictionary* results) {
13961410
[OneSignal onesignal_Log:ONE_S_LL_INFO message:[NSString stringWithFormat: @"Device Registered with OneSignal: %@", self.currentSubscriptionState.userId]];
13971411
} onFailure:^(NSError* error) {
13981412
[OneSignal onesignal_Log:ONE_S_LL_ERROR message:[NSString stringWithFormat: @"Error in OneSignal Registration: %@", error]];
@@ -1478,7 +1492,7 @@ + (void)syncHashedEmail:(NSString *)email {
14781492

14791493
onesignal_Log(ONE_S_LL_DEBUG, [NSString stringWithFormat:@"%@ - MD5: %@, SHA1:%@", lowEmail, md5, sha1]);
14801494

1481-
NSMutableURLRequest* request = [httpClient requestWithMethod:@"PUT" path:[NSString stringWithFormat:@"players/%@", self.currentSubscriptionState.userId]];
1495+
NSMutableURLRequest* request = [self.httpClient requestWithMethod:@"PUT" path:[NSString stringWithFormat:@"players/%@", self.currentSubscriptionState.userId]];
14821496
NSDictionary* dataDic = [NSDictionary dictionaryWithObjectsAndKeys:
14831497
app_id, @"app_id",
14841498
md5, @"em_m",

iOS_SDK/OneSignalSDK/Source/UIApplicationDelegate+OneSignal.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,7 @@ + (void)sizzlePreiOS10MethodsPhase2 {
149149
- (void)oneSignalDidRegisterForRemoteNotifications:(UIApplication*)app deviceToken:(NSData*)inDeviceToken {
150150
[OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:@"oneSignalDidRegisterForRemoteNotifications:deviceToken:"];
151151

152-
if ([OneSignal app_id])
153-
[OneSignal didRegisterForRemoteNotifications:app deviceToken:inDeviceToken];
152+
[OneSignal didRegisterForRemoteNotifications:app deviceToken:inDeviceToken];
154153

155154
if ([self respondsToSelector:@selector(oneSignalDidRegisterForRemoteNotifications:deviceToken:)])
156155
[self oneSignalDidRegisterForRemoteNotifications:app deviceToken:inDeviceToken];

iOS_SDK/OneSignalSDK/UnitTests/UnitTests.m

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,11 +531,18 @@ + (NSString*) overrideGetAppName {
531531
}
532532

533533
+ (void)overrideEnqueueRequest:(NSURLRequest*)request onSuccess:(OSResultSuccessBlock)successBlock onFailure:(OSFailureBlock)failureBlock isSynchronous:(BOOL)isSynchronous {
534+
NSError *error = nil;
535+
NSDictionary *parameters = [NSJSONSerialization JSONObjectWithData:[request HTTPBody] options:0 error:&error];
536+
537+
// We should always send an app_id with every request.
538+
if (!parameters[@"app_id"])
539+
_XCTPrimitiveFail(currentTestInstance);
540+
534541
networkRequestCount++;
535542

536-
NSError *error = nil;
543+
544+
537545
id url = [request URL];
538-
NSDictionary *parameters = [NSJSONSerialization JSONObjectWithData:[request HTTPBody] options:0 error:&error];
539546
NSLog(@"url: %@", url);
540547
NSLog(@"parameters: %@", parameters);
541548

@@ -1032,6 +1039,38 @@ - (void)testInitOnSimulator {
10321039
XCTAssertEqual(networkRequestCount, 1);
10331040
}
10341041

1042+
1043+
1044+
- (void)testCallingMethodsBeforeInit {
1045+
[self setCurrentNotificationPermission:true];
1046+
1047+
[OneSignal sendTag:@"key" value:@"value"];
1048+
[OneSignal setSubscription:true];
1049+
[OneSignal promptLocation];
1050+
[OneSignal promptForPushNotificationsWithUserResponse:nil];
1051+
[self runBackgroundThreads];
1052+
1053+
[self initOneSignal];
1054+
[self runBackgroundThreads];
1055+
1056+
XCTAssertEqualObjects(lastHTTPRequset[@"app_id"], @"b2f7f966-d8cc-11e4-bed1-df8f05be55ba");
1057+
XCTAssertEqualObjects(lastHTTPRequset[@"tags"][@"key"], @"value");
1058+
XCTAssertEqual(networkRequestCount, 1);
1059+
1060+
[self clearStateForAppRestart];
1061+
1062+
[OneSignal sendTag:@"key" value:@"value"];
1063+
[OneSignal setSubscription:true];
1064+
[OneSignal promptLocation];
1065+
[OneSignal promptForPushNotificationsWithUserResponse:nil];
1066+
[self runBackgroundThreads];
1067+
1068+
[self initOneSignal];
1069+
[self runBackgroundThreads];
1070+
XCTAssertEqual(networkRequestCount, 0);
1071+
1072+
}
1073+
10351074
- (void)testPermissionChangeObserverIOS10 {
10361075
mockIOSVersion = 10;
10371076
[self sharedTestPermissionChangeObserver];

0 commit comments

Comments
 (0)