Skip to content

Commit c3af60b

Browse files
authored
Send push token and settings after start (#431)
1 parent e2748c1 commit c3af60b

File tree

2 files changed

+76
-7
lines changed

2 files changed

+76
-7
lines changed

Example/Tests/Classes/LPPushNotificationsManagerTest.m

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ - (void)test_push_token
133133
/**
134134
* Test iOS Push Types are sent when notifications are registered successfully
135135
*/
136-
- (void)test_push_types
136+
- (void)test_push_types_new_token
137137
{
138138
// Partial mock Action Manager.
139139
LPPushNotificationsManager *manager = [self mockManager];
@@ -142,11 +142,16 @@ - (void)test_push_types
142142
[manager removePushToken];
143143
[self removePushTypes];
144144

145+
if (!Leanplum.hasStarted){
146+
XCTAssertTrue([LeanplumHelper start_production_test]);
147+
}
148+
145149
[self mockUserNotificationSettings:UIUserNotificationTypeAlert withCategoryId:@"testCategory"];
146150

147151
UIApplication *app = [UIApplication sharedApplication];
148152
XCTestExpectation *expectNewToken = [self expectationWithDescription:@"expectNewToken"];
149153
NSData *token = [@"sample" dataUsingEncoding:NSUTF8StringEncoding];
154+
150155
[LPRequestSender validate_request:^BOOL(NSString *method, NSString *apiMethod,
151156
NSDictionary *params) {
152157
XCTAssertTrue([apiMethod isEqual:@"setDeviceAttributes"]);
@@ -159,21 +164,81 @@ - (void)test_push_types
159164

160165
[manager leanplum_application:app didRegisterForRemoteNotificationsWithDeviceToken:token];
161166

162-
[self mockUserNotificationSettings:UIUserNotificationTypeBadge withCategoryId:@"testCategory2"];
163-
// Test categories will be sent even if token is the same
167+
[self waitForExpectationsWithTimeout:2 handler:nil];
168+
}
169+
170+
/**
171+
* Test iOS Push Types are sent when notifications are registered successfully and token is the same
172+
*/
173+
- (void)test_push_types_same_token
174+
{
175+
// Partial mock Action Manager.
176+
LPPushNotificationsManager *manager = [self mockManager];
177+
[self removePushTypes];
178+
179+
[self mockUserNotificationSettings:UIUserNotificationTypeAlert withCategoryId:@"testCategory"];
180+
181+
UIApplication *app = [UIApplication sharedApplication];
182+
183+
XCTestExpectation *expectPushTypes = [self expectationWithDescription:@"expectPushTypes"];
184+
185+
if (!Leanplum.hasStarted){
186+
XCTAssertTrue([LeanplumHelper start_production_test]);
187+
}
188+
189+
// Ensure the same token
190+
NSString *pushToken = @"testToken";
191+
NSData *token = [pushToken dataUsingEncoding:NSUTF8StringEncoding];
192+
NSString *formattedToken = [self formatToken:token];
193+
[manager updatePushToken:formattedToken];
194+
164195
[LPRequestSender validate_request:^BOOL(NSString *method, NSString *apiMethod,
165196
NSDictionary *params) {
166197
XCTAssertTrue([apiMethod isEqual:@"setDeviceAttributes"]);
167198
XCTAssertNil(params[LP_PARAM_DEVICE_PUSH_TOKEN]);
168-
XCTAssertTrue([params[LP_PARAM_DEVICE_USER_NOTIFICATION_TYPES] isEqual:@(UIUserNotificationTypeBadge)]);
169-
XCTAssertTrue([params[LP_PARAM_DEVICE_USER_NOTIFICATION_CATEGORIES] isEqual:[LPJSON stringFromJSON:@[@"testCategory2"]?: @""]]);
199+
XCTAssertTrue([params[LP_PARAM_DEVICE_USER_NOTIFICATION_TYPES] isEqual:@(UIUserNotificationTypeAlert)]);
200+
XCTAssertTrue([params[LP_PARAM_DEVICE_USER_NOTIFICATION_CATEGORIES] isEqual:[LPJSON stringFromJSON:@[@"testCategory"]?: @""]]);
201+
[expectPushTypes fulfill];
170202
return YES;
171203
}];
204+
172205
[manager leanplum_application:app didRegisterForRemoteNotificationsWithDeviceToken:token];
173206

174207
[self waitForExpectationsWithTimeout:2 handler:nil];
175208
}
176209

210+
/**
211+
* Test iOS Push token /and types/ are sent after Leanplum has started
212+
* This will prevent creating the request with empty userId and deviceId
213+
*/
214+
- (void)test_push_token_not_before_start
215+
{
216+
// Partial mock Action Manager.
217+
LPPushNotificationsManager *manager = [self mockManager];
218+
219+
// Remove Push Token.
220+
[manager removePushToken];
221+
222+
if (!Leanplum.hasStarted){
223+
NSData *token = [@"sample" dataUsingEncoding:NSUTF8StringEncoding];
224+
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
225+
[LPRequestSender validate_request:^BOOL(NSString *method, NSString *apiMethod,
226+
NSDictionary *params) {
227+
if ([apiMethod isEqual:@"setDeviceAttributes"]) {
228+
XCTAssertTrue(NO);
229+
dispatch_semaphore_signal(semaphore);
230+
}
231+
}];
232+
233+
UIApplication *app = [UIApplication sharedApplication];
234+
[manager leanplum_application:app didRegisterForRemoteNotificationsWithDeviceToken:token];
235+
236+
long timedOut = dispatch_semaphore_wait(semaphore, [LeanplumHelper default_dispatch_time]);
237+
// Expect to timeout since the request should never be made, but still wait to verify it is not
238+
XCTAssertTrue(timedOut > 0);
239+
}
240+
}
241+
177242
- (void)test_push_types_foreground
178243
{
179244
[self removePushTypes];

Leanplum-SDK/Classes/Notifications/Push/LPPushNotificationsHandler.m

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,12 @@ -(void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)token
133133

134134
// If there are changes to the push token and/or the push types, send a request
135135
if (deviceAttributeParams.count > 0) {
136-
LPRequest *request = [LPRequestFactory setDeviceAttributesWithParams:deviceAttributeParams];
137-
[[LPRequestSender sharedInstance] send:request];
136+
[Leanplum onStartResponse:^(BOOL success) {
137+
LP_END_USER_CODE
138+
LPRequest *request = [LPRequestFactory setDeviceAttributesWithParams:deviceAttributeParams];
139+
[[LPRequestSender sharedInstance] send:request];
140+
LP_BEGIN_USER_CODE
141+
}];
138142
}
139143
LP_END_TRY
140144
}

0 commit comments

Comments
 (0)