Skip to content

Commit 16f63f6

Browse files
dejan2ke7mac
andauthored
Lp 11236 fix push metrics (#386)
* fixing remote notifications metrics * prevent sending open status to server when app is in foreground and there is no users action * remove logs and no needed code * set appWasActivatedByReceivingPushNotification to NO when code goes through the logic to handle opening of push notificaiton * fix tests * added logs for push received and opened Co-authored-by: Mayank Sanganeria <[email protected]>
1 parent e5a4972 commit 16f63f6

File tree

4 files changed

+79
-13
lines changed

4 files changed

+79
-13
lines changed

Example/Tests/Classes/LPPushNotificationsHandlerTest.m

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919
#import "LPPushNotificationsManager.h"
2020
#import "LPNotificationsManager.h"
2121

22-
2322
@interface LPPushNotificationsHandler (Test)
2423
- (void)requireMessageContent:(NSString *)messageId
2524
withCompletionBlock:(LeanplumVariablesChangedBlock)onCompleted;
25+
- (void)handleNotificationResponse:(NSDictionary *)userInfo
26+
completionHandler:(LeanplumFetchCompletionBlock)completionHandler;
2627
@end
2728

2829
@interface LPPushNotificationsHandlerTest : XCTestCase
@@ -90,16 +91,27 @@ - (void) test_receive_notification
9091
@"_lpx": @"test_action",
9192
@"aps" : @{@"alert": @"test"}};
9293

93-
XCTestExpectation* expectation = [self expectationWithDescription:@"notification"];
94+
XCTestExpectation* userNotificationCenterExpectation = [self expectationWithDescription:@"UNUserNotificationCenter_notification"];
95+
XCTestExpectation* applicationNotificationExpectation = [self expectationWithDescription:@"UIApplication_notification"];
9496

9597
LPPushNotificationsHandler *handler = [[LPPushNotificationsHandler alloc] init];
9698

99+
//test when UNUserNotificationCenter.currentNotificationCenter.delegate is set
100+
if (UNUserNotificationCenter.currentNotificationCenter.delegate != nil) {
101+
[handler handleNotificationResponse:userInfo completionHandler:^(LeanplumUIBackgroundFetchResult result) {
102+
[userNotificationCenterExpectation fulfill];
103+
}];
104+
}
105+
106+
//test when UNUserNotificationCenter.currentNotificationCenter.delegate is nil
107+
UNUserNotificationCenter.currentNotificationCenter.delegate = nil;
97108
[handler didReceiveRemoteNotification:userInfo
98109
withAction:@"test_action"
99110
fetchCompletionHandler: ^(LeanplumUIBackgroundFetchResult result) {
100-
[expectation fulfill];
111+
[applicationNotificationExpectation fulfill];
101112

102-
}];
113+
}];
114+
103115
[self waitForExpectationsWithTimeout:10 handler:nil];
104116
}
105117

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ NS_ASSUME_NONNULL_BEGIN
1414
@interface LPPushNotificationsHandler : NSObject
1515

1616
@property (nonatomic, strong) LeanplumShouldHandleNotificationBlock shouldHandleNotification;
17+
@property (nonatomic, assign) BOOL appWasActivatedByReceivingPushNotification;
1718

1819
- (void)didReceiveRemoteNotification:(NSDictionary *)userInfo;
1920
- (void)didReceiveRemoteNotification:(NSDictionary *)userInfo
@@ -23,6 +24,7 @@ NS_ASSUME_NONNULL_BEGIN
2324
fetchCompletionHandler:(LeanplumFetchCompletionBlock __nullable)completionHandler;
2425
- (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)token;
2526
- (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error;
27+
- (void)handleWillPresentNotification:(NSDictionary *)userInfo;
2628
- (void)willPresentNotification:(UNNotification *)notification
2729
withCompletionHandler:(void(^)(UNNotificationPresentationOptions options))completionHandler API_AVAILABLE(ios(10.0));
2830
- (void)didReceiveNotificationResponse:(UNNotificationResponse *)response

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

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,18 @@ - (void)didReceiveRemoteNotification:(NSDictionary *)userInfo
4444

4545
-(void)didReceiveRemoteNotification:(NSDictionary *)userInfo withAction:(NSString *__nullable)action fetchCompletionHandler:(LeanplumFetchCompletionBlock __nullable)completionHandler
4646
{
47+
if (@available(iOS 10, *)) {
48+
if ([UNUserNotificationCenter currentNotificationCenter].delegate != nil) {
49+
if (UIApplication.sharedApplication.applicationState != UIApplicationStateBackground) {
50+
return;
51+
}
52+
}
53+
}
54+
4755
[self.countAggregator incrementCount:@"did_receive_remote_notification"];
4856

4957
// If app was inactive, then handle notification because the user tapped it.
50-
if ([[UIApplication sharedApplication] applicationState] != UIApplicationStateActive) {
58+
if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateInactive) {
5159
[self handleNotification:userInfo
5260
withAction:action
5361
appActive:NO
@@ -184,19 +192,26 @@ - (void)handleNotification:(NSDictionary *)userInfo
184192
if (messageId == nil) {
185193
return;
186194
}
187-
195+
188196
void (^onContent)(void) = ^{
189197
if (completionHandler) {
190198
completionHandler(UIBackgroundFetchResultNewData);
191199
}
200+
192201
BOOL hasAlert = userInfo[@"aps"][@"alert"] != nil;
193202
if (hasAlert) {
194-
UIApplicationState appState = [[UIApplication sharedApplication] applicationState];
195-
if (appState != UIApplicationStateBackground) {
196-
[self maybePerformNotificationActions:userInfo action:action active:active];
197-
}
203+
[self maybePerformNotificationActions:userInfo action:action active:active];
198204
}
199205
};
206+
207+
if (UIApplication.sharedApplication.applicationState == UIApplicationStateInactive && !self.appWasActivatedByReceivingPushNotification) {
208+
[self setAppWasActivatedByReceivingPushNotification:NO];
209+
onContent();
210+
return;
211+
}
212+
[self setAppWasActivatedByReceivingPushNotification:NO];
213+
214+
NSLog(@"Push RECEIVED");
200215

201216
[Leanplum onStartIssued:^() {
202217
if ([self areActionsEmbedded:userInfo]) {
@@ -214,6 +229,11 @@ - (void)maybePerformNotificationActions:(NSDictionary *)userInfo
214229
action:(NSString *)action
215230
active:(BOOL)active
216231
{
232+
// Do not perform the action if the app is in background
233+
if (UIApplication.sharedApplication.applicationState == UIApplicationStateBackground) {
234+
return;
235+
}
236+
217237
// Don't handle duplicate notifications.
218238
if ([self isDuplicateNotification:userInfo]) {
219239
return;
@@ -226,6 +246,7 @@ - (void)maybePerformNotificationActions:(NSDictionary *)userInfo
226246
}
227247
}
228248

249+
NSLog(@"Push OPENED");
229250
LPLog(LPInfo, @"Handling push notification");
230251
NSString *messageId = [[LPNotificationsManager shared] messageIdFromUserInfo:userInfo];
231252
NSString *actionName;
@@ -389,10 +410,20 @@ - (void)handleNotificationResponse:(NSDictionary *)userInfo
389410
}
390411
BOOL hasAlert = userInfo[@"aps"][@"alert"] != nil;
391412
if (hasAlert) {
392-
[self maybePerformNotificationActions:userInfo action:nil active:NO];
413+
BOOL active = UIApplication.sharedApplication.applicationState == UIApplicationStateActive;
414+
[self maybePerformNotificationActions:userInfo action:nil active:active];
393415
}
394416
};
395-
417+
418+
if (UIApplication.sharedApplication.applicationState == UIApplicationStateInactive || self.appWasActivatedByReceivingPushNotification) {
419+
[self setAppWasActivatedByReceivingPushNotification:NO];
420+
onContent();
421+
return;
422+
}
423+
[self setAppWasActivatedByReceivingPushNotification:NO];
424+
425+
NSLog(@"Push RECEIVED");
426+
396427
if ([[UIApplication sharedApplication] applicationState] != UIApplicationStateActive) {
397428
[Leanplum onStartIssued:^() {
398429
if ([self areActionsEmbedded:userInfo]) {
@@ -408,6 +439,13 @@ - (void)handleNotificationResponse:(NSDictionary *)userInfo
408439
}
409440
}
410441

442+
- (void)handleWillPresentNotification:(NSDictionary *)userInfo
443+
{
444+
if(@available(iOS 10, *)) {
445+
[self handleWillPresentNotification:userInfo withCompletionHandler:nil];
446+
}
447+
}
448+
411449
// Will be called when user receives notification and app is in foreground iff UNUserNotificationCenterDelegate is implemented in UIApplicationDelegate.
412450
// We need to check first whether the action is muted, and depending on it show or silence the action.
413451
- (void)handleWillPresentNotification:(NSDictionary *)userInfo
@@ -425,9 +463,12 @@ - (void)handleWillPresentNotification:(NSDictionary *)userInfo
425463
}
426464
BOOL hasAlert = userInfo[@"aps"][@"alert"] != nil;
427465
if (hasAlert) {
428-
[self maybePerformNotificationActions:userInfo action:nil active:YES];
466+
BOOL active = UIApplication.sharedApplication.applicationState == UIApplicationStateActive;
467+
[self maybePerformNotificationActions:userInfo action:nil active:active];
429468
}
430469
};
470+
471+
NSLog(@"Push RECEIVED");
431472

432473
if (!userInfo[LP_KEY_PUSH_MUTE_IN_APP] && !userInfo[LP_KEY_PUSH_NO_ACTION_MUTE]) {
433474
[Leanplum onStartIssued:^() {

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,17 @@ + (void) load
350350

351351
+ (void)handleApplicationDidBecomeActive:(NSNotification *)notification {
352352
[[LPPushNotificationsManager sharedManager] swizzleAppMethods];
353+
NSDictionary *userInfo = notification.userInfo[@"UIApplicationLaunchOptionsRemoteNotificationKey"];
354+
if (userInfo != nil) {
355+
[[LPPushNotificationsManager sharedManager].handler setAppWasActivatedByReceivingPushNotification:YES];
356+
if (@available(iOS 10, *)) {
357+
if ([UNUserNotificationCenter currentNotificationCenter].delegate != nil) {
358+
[[LPPushNotificationsManager sharedManager].handler handleWillPresentNotification:userInfo];
359+
return;
360+
}
361+
}
362+
[[LPPushNotificationsManager sharedManager].handler didReceiveRemoteNotification:userInfo];
363+
}
353364
}
354365

355366
- (void)swizzleAppMethods

0 commit comments

Comments
 (0)