Skip to content

Commit f168f1f

Browse files
authored
fix open url/deeplink from push notifications when app is closed (#435)
* fix open url/deeplink from push notifications when app is closed * fix unit test * fix unit test * remove unnecessary code from tests * removing check for hasStarted since onStartResponse will check it itself and will execute the block if Leanplum is started * removing unnecessary and duplicate code
1 parent b727905 commit f168f1f

File tree

4 files changed

+30
-59
lines changed

4 files changed

+30
-59
lines changed

Example/Tests/Classes/LPPushNotificationsHandlerTest.m

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ + (void)setUp
4242
- (void)setUp
4343
{
4444
[super setUp];
45-
// Automatically sets up AppId and AccessKey for development mode.
46-
[LeanplumHelper setup_development_test];
4745
}
4846

4947
- (void)tearDown
@@ -66,6 +64,8 @@ - (void)test_require_message_content
6664

6765
- (void)test_notification_action
6866
{
67+
XCTAssertTrue([LeanplumHelper start_development_test]);
68+
6969
id classMock = OCMClassMock([LPUIAlert class]);
7070

7171
NSDictionary* userInfo = @{
@@ -106,12 +106,6 @@ - (void) test_receive_notification
106106
//test when UNUserNotificationCenter.currentNotificationCenter.delegate is nil
107107
UNUserNotificationCenter.currentNotificationCenter.delegate = nil;
108108

109-
// Requires Leanplum Start
110-
// didReceiveRemoteNotification: UIApplicationState is Active -> calls handleNotification -> runs onContent on startIssued callback
111-
if (!Leanplum.hasStarted){
112-
XCTAssertTrue([LeanplumHelper start_production_test]);
113-
}
114-
115109
// Change messageId so it is not a duplicate notification
116110
NSDictionary* userInfo2 = @{
117111
@"_lpm": @"messageId_2",

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

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

1616
@property (nonatomic, strong) LeanplumShouldHandleNotificationBlock shouldHandleNotification;
17-
@property (nonatomic, assign) BOOL appWasActivatedByReceivingPushNotification;
1817
@property (nonatomic, readonly) NSDictionary *currentUserNotificationSettings;
1918

2019
- (void)didReceiveRemoteNotification:(NSDictionary *)userInfo;

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

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ -(void)didReceiveRemoteNotification:(NSDictionary *)userInfo withAction:(NSStrin
7979

8080
[self.countAggregator incrementCount:@"did_receive_remote_notification"];
8181

82-
// If app was inactive, then handle notification because the user tapped it.
83-
if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateInactive) {
82+
// If app was inactive or in background, then handle notification because the user tapped it.
83+
if ([[UIApplication sharedApplication] applicationState] != UIApplicationStateActive) {
8484
[self handleNotification:userInfo
8585
withAction:action
8686
appActive:NO
@@ -230,10 +230,6 @@ - (void)handleNotification:(NSDictionary *)userInfo
230230
return;
231231
}
232232

233-
if ([self isDuplicateNotification:userInfo]) {
234-
return;
235-
}
236-
237233
void (^onContent)(void) = ^{
238234
if (completionHandler) {
239235
completionHandler(UIBackgroundFetchResultNewData);
@@ -245,13 +241,6 @@ - (void)handleNotification:(NSDictionary *)userInfo
245241
}
246242
};
247243

248-
if (UIApplication.sharedApplication.applicationState == UIApplicationStateInactive && !self.appWasActivatedByReceivingPushNotification) {
249-
[self setAppWasActivatedByReceivingPushNotification:NO];
250-
onContent();
251-
return;
252-
}
253-
[self setAppWasActivatedByReceivingPushNotification:NO];
254-
255244
LPLog(LPDebug, @"Push RECEIVED");
256245

257246
[Leanplum onStartIssued:^() {
@@ -270,6 +259,11 @@ - (void)maybePerformNotificationActions:(NSDictionary *)userInfo
270259
action:(NSString *)action
271260
active:(BOOL)active
272261
{
262+
// Do not perform the action if the app is in background
263+
if (UIApplication.sharedApplication.applicationState == UIApplicationStateBackground) {
264+
return;
265+
}
266+
273267
// Don't handle duplicate notifications.
274268
if ([self isDuplicateNotification:userInfo]) {
275269
return;
@@ -322,20 +316,22 @@ - (void)maybePerformNotificationActions:(NSDictionary *)userInfo
322316
userInfo[LP_KEY_PUSH_NO_ACTION_MUTE]) {
323317
handleNotificationBlock();
324318
} else {
325-
id message = userInfo[@"aps"][@"alert"];
326-
if ([message isKindOfClass:NSDictionary.class]) {
327-
message = message[@"body"];
328-
}
329-
if (message) {
330-
[LPUIAlert showWithTitle:APP_NAME
331-
message:message
332-
cancelButtonTitle:NSLocalizedString(@"Cancel", nil)
333-
otherButtonTitles:@[NSLocalizedString(@"View", nil)]
334-
block:^(NSInteger buttonIndex) {
335-
if (buttonIndex == 1) {
336-
handleNotificationBlock();
337-
}
338-
}];
319+
if ([Leanplum hasStarted]) {
320+
id message = userInfo[@"aps"][@"alert"];
321+
if ([message isKindOfClass:NSDictionary.class]) {
322+
message = message[@"body"];
323+
}
324+
if (message) {
325+
[LPUIAlert showWithTitle:APP_NAME
326+
message:message
327+
cancelButtonTitle:NSLocalizedString(@"Cancel", nil)
328+
otherButtonTitles:@[NSLocalizedString(@"View", nil)]
329+
block:^(NSInteger buttonIndex) {
330+
if (buttonIndex == 1) {
331+
handleNotificationBlock();
332+
}
333+
}];
334+
}
339335
}
340336
}
341337
}
@@ -450,13 +446,6 @@ - (void)handleNotificationResponse:(NSDictionary *)userInfo
450446
}
451447
};
452448

453-
if (UIApplication.sharedApplication.applicationState == UIApplicationStateInactive || self.appWasActivatedByReceivingPushNotification) {
454-
[self setAppWasActivatedByReceivingPushNotification:NO];
455-
onContent();
456-
return;
457-
}
458-
[self setAppWasActivatedByReceivingPushNotification:NO];
459-
460449
LPLog(LPDebug, @"Push RECEIVED");
461450

462451
if ([[UIApplication sharedApplication] applicationState] != UIApplicationStateActive) {

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

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,8 @@ + (void) load
352352

353353
+ (void)handleApplicationDidBecomeActive:(NSNotification *)notification {
354354
[[LPPushNotificationsManager sharedManager] swizzleAppMethods];
355-
NSDictionary *userInfo = notification.userInfo[@"UIApplicationLaunchOptionsRemoteNotificationKey"];
355+
NSDictionary *userInfo = notification.userInfo[UIApplicationLaunchOptionsRemoteNotificationKey];
356356
if (userInfo != nil) {
357-
[[LPPushNotificationsManager sharedManager].handler setAppWasActivatedByReceivingPushNotification:YES];
358357
if (@available(iOS 10, *)) {
359358
if ([UNUserNotificationCenter currentNotificationCenter].delegate != nil) {
360359
[[LPPushNotificationsManager sharedManager].handler handleWillPresentNotification:userInfo];
@@ -502,20 +501,6 @@ - (void)swizzleAppMethods
502501
{
503502
LPLog(LPInfo, @"Method swizzling is disabled, make sure to manually call Leanplum methods.");
504503
}
505-
506-
// Detect receiving notifications.
507-
[[NSNotificationCenter defaultCenter]
508-
addObserverForName:UIApplicationDidFinishLaunchingNotification object:nil queue:nil
509-
usingBlock:^(NSNotification *notification) {
510-
if (notification.userInfo) {
511-
NSDictionary *userInfo = notification.userInfo
512-
[UIApplicationLaunchOptionsRemoteNotificationKey];
513-
[self.handler handleNotification:userInfo
514-
withAction:nil
515-
appActive:NO
516-
completionHandler:nil];
517-
}
518-
}];
519504
}
520505

521506
// Block to run to decide whether to show the notification
@@ -525,4 +510,8 @@ - (void)setShouldHandleNotification:(LeanplumShouldHandleNotificationBlock)block
525510
self.handler.shouldHandleNotification = block;
526511
}
527512

513+
- (void) dealloc {
514+
[[NSNotificationCenter defaultCenter] removeObserver:self];
515+
}
516+
528517
@end

0 commit comments

Comments
 (0)