Skip to content

Commit 1e22a38

Browse files
committed
getTags now works when called before device registers
1 parent d469894 commit 1e22a38

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

iOS_SDK/OneSignalSDK/Source/OneSignal.m

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ @implementation OneSignal
118118
static BOOL coldStartFromTapOnNotification = NO;
119119

120120
static NSMutableArray* pendingSendTagCallbacks;
121+
static OSResultSuccessBlock pendingGetTagsSuccessBlock;
122+
static OSFailureBlock pendingGetTagsFailureBlock;
121123

122124
// Has attempted to register for push notifications with Apple since app was installed.
123125
static BOOL registeredWithApple = NO;
@@ -682,8 +684,11 @@ + (void)sendTag:(NSString*)key value:(NSString*)value onSuccess:(OSResultSuccess
682684
}
683685

684686
+ (void)getTags:(OSResultSuccessBlock)successBlock onFailure:(OSFailureBlock)failureBlock {
685-
if (!self.currentSubscriptionState.userId)
687+
if (!self.currentSubscriptionState.userId) {
688+
pendingGetTagsSuccessBlock = successBlock;
689+
pendingGetTagsFailureBlock = failureBlock;
686690
return;
691+
}
687692

688693
NSMutableURLRequest* request;
689694
NSString* path = [NSString stringWithFormat:@"players/%@?app_id=%@", self.currentSubscriptionState.userId, self.app_id];
@@ -1094,6 +1099,13 @@ + (void)registerUserInternal {
10941099
[self fireIdsAvailableCallback];
10951100

10961101
[self sendNotificationTypesUpdate];
1102+
1103+
if (pendingGetTagsSuccessBlock) {
1104+
[OneSignal getTags:pendingGetTagsSuccessBlock onFailure:pendingGetTagsFailureBlock];
1105+
pendingGetTagsSuccessBlock = nil;
1106+
pendingGetTagsFailureBlock = nil;
1107+
}
1108+
10971109
}
10981110
} onFailure:^(NSError* error) {
10991111
waitingForOneSReg = false;

iOS_SDK/OneSignalSDK/UnitTests/UnitTests.m

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1958,6 +1958,7 @@ - (void)testSendTags {
19581958
didRunSuccess3 = true;
19591959
} onFailure:^(NSError *error) {}];
19601960

1961+
[self runBackgroundThreads];
19611962
[NSObjectOverrider runPendingSelectors];
19621963
[self runBackgroundThreads];
19631964

@@ -1977,13 +1978,17 @@ - (void)testDeleteTags {
19771978
[self runBackgroundThreads];
19781979
XCTAssertEqual(networkRequestCount, 1);
19791980

1981+
NSLog(@"Calling sendTag and deleteTag");
19801982
// send 2 tags and delete 1 before they get sent off.
19811983
[OneSignal sendTag:@"key" value:@"value"];
19821984
[OneSignal sendTag:@"key2" value:@"value2"];
19831985
[OneSignal deleteTag:@"key"];
1986+
NSLog(@"Finished calling sendTag and deleteTag");
19841987

19851988
// Make sure only 1 network call is made and only key2 gets sent.
19861989
[NSObjectOverrider runPendingSelectors];
1990+
[self runBackgroundThreads];
1991+
19871992
XCTAssertNil(lastHTTPRequset[@"tags"][@"key"]);
19881993
XCTAssertEqualObjects(lastHTTPRequset[@"tags"][@"key2"], @"value2");
19891994
XCTAssertEqual(networkRequestCount, 2);
@@ -2012,11 +2017,29 @@ - (void)testGetTags {
20122017
XCTAssertTrue(fireGetTags);
20132018
}
20142019

2015-
- (void)testGetTagsWithNestedDelete {
2020+
- (void)testGetTagsBeforePlayerId {
20162021
[self initOneSignal];
20172022
[self runBackgroundThreads];
20182023
XCTAssertEqual(networkRequestCount, 1);
20192024

2025+
__block BOOL fireGetTags = false;
2026+
2027+
[OneSignal getTags:^(NSDictionary *result) {
2028+
NSLog(@"getTags success HERE");
2029+
fireGetTags = true;
2030+
} onFailure:^(NSError *error) {
2031+
NSLog(@"getTags onFailure HERE");
2032+
}];
2033+
2034+
[self runBackgroundThreads];
2035+
2036+
XCTAssertTrue(fireGetTags);
2037+
2038+
}
2039+
2040+
- (void)testGetTagsWithNestedDelete {
2041+
[self initOneSignal];
2042+
20202043
__block BOOL fireDeleteTags = false;
20212044

20222045
[OneSignal getTags:^(NSDictionary *result) {
@@ -2031,9 +2054,14 @@ - (void)testGetTagsWithNestedDelete {
20312054
NSLog(@"getTags onFailure HERE");
20322055
}];
20332056

2057+
2058+
[self runBackgroundThreads];
2059+
20342060
[self runBackgroundThreads];
20352061
[NSObjectOverrider runPendingSelectors];
20362062

2063+
// create, ge tags, then sendTags call.
2064+
XCTAssertEqual(networkRequestCount, 3);
20372065
XCTAssertTrue(fireDeleteTags);
20382066
}
20392067

0 commit comments

Comments
 (0)