Skip to content

Commit 17248c9

Browse files
authored
Merge pull request #867 from OneSignal/feature/launch_url_suppress
Launch url suppress
2 parents fd02f94 + 25f0c8d commit 17248c9

File tree

7 files changed

+78
-3
lines changed

7 files changed

+78
-3
lines changed

iOS_SDK/OneSignalSDK/Source/OneSignal.m

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,14 @@ + (void)initWithLaunchOptions:(nullable NSDictionary*)newLaunchOptions {
603603
[self init];
604604
}
605605

606+
+ (BOOL)shouldSuppressURL {
607+
// if the plist key does not exist default to false
608+
// the plist value specifies whether the user wants to open an url using default browser or OSWebView
609+
NSDictionary *bundleDict = [[NSBundle mainBundle] infoDictionary];
610+
BOOL shouldSuppress = [bundleDict[ONESIGNAL_SUPRESS_LAUNCH_URLS] boolValue];
611+
return shouldSuppress ?: false;
612+
}
613+
606614
+ (void)setLaunchURLsInApp:(BOOL)launchInApp {
607615
NSMutableDictionary *newSettings = [[NSMutableDictionary alloc] initWithDictionary:appSettings];
608616
newSettings[kOSSettingsKeyInAppLaunchURL] = launchInApp ? @true : @false;
@@ -1970,9 +1978,11 @@ + (void)handleNotificationOpened:(NSDictionary*)messageDict
19701978
onesignal_Log(ONE_S_LL_VERBOSE, [NSString stringWithFormat:@"handleNotificationOpened called! isActive: %@ notificationId: %@",
19711979
isActive ? @"YES" : @"NO", messageId]);
19721980

1973-
// Try to fetch the open url to launch
1974-
[OneSignal launchWebURL:[customDict objectForKey:@"u"]];
1975-
1981+
if ([OneSignal shouldSuppressURL]) {
1982+
// Try to fetch the open url to launch
1983+
[OneSignal launchWebURL:notification.launchURL];
1984+
}
1985+
19761986
[self clearBadgeCount:true];
19771987

19781988
NSString* actionID = NULL;

iOS_SDK/OneSignalSDK/Source/OneSignalCommonDefines.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135

136136
// Info.plist key
137137
#define FALLBACK_TO_SETTINGS_MESSAGE @"Onesignal_settings_fallback_message"
138+
#define ONESIGNAL_SUPRESS_LAUNCH_URLS @"OneSignal_suppress_launch_urls"
138139

139140
// GDPR Privacy Consent
140141
#define GDPR_CONSENT_GRANTED @"GDPR_CONSENT_GRANTED"

iOS_SDK/OneSignalSDK/UnitTestApp/Info.plist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
</array>
2929
<key>UILaunchStoryboardName</key>
3030
<string>LaunchScreen</string>
31+
<key>OneSignal_suppress_launch_urls</key>
32+
<false/>
3133
<key>UIRequiredDeviceCapabilities</key>
3234
<array>
3335
<string>armv7</string>

iOS_SDK/OneSignalSDK/UnitTests/Shadows/OneSignalOverrider.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ NS_ASSUME_NONNULL_BEGIN
3232
@interface OneSignalOverrider : NSObject
3333

3434
@property (class, nonatomic) BOOL shouldOverrideSessionLaunchTime;
35+
@property (class, nonatomic) BOOL shouldOverrideLaunchURL;
36+
@property (class, nonatomic) BOOL launchWebURLWasCalled;
37+
38+
+ (void)reset;
3539

3640
@end
3741

iOS_SDK/OneSignalSDK/UnitTests/Shadows/OneSignalOverrider.m

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ + (NSDate *)sessionLaunchTime;
4040

4141
@implementation OneSignal (Testing)
4242

43+
+ (void)overrideLaunchWebURL:(NSString*)openUrl {
44+
OneSignalOverrider.launchWebURLWasCalled = true;
45+
}
46+
4347
+ (NSDate *)overrideSessionLaunchTime {
4448
if (OneSignalOverrider.shouldOverrideSessionLaunchTime) {
4549
return [NSDate date];
@@ -53,12 +57,20 @@ + (NSDate *)overrideSessionLaunchTime {
5357
@implementation OneSignalOverrider
5458

5559
static BOOL _overrideLaunchTime = false;
60+
static BOOL _overrideLaunchURL = false;
61+
static BOOL _launchWebURLWasCalled = false;
62+
5663

5764
+ (void)load {
5865
swizzleClassMethodWithCategoryImplementation([OneSignal class], @selector(sessionLaunchTime), @selector(overrideSessionLaunchTime));
66+
swizzleClassMethodWithCategoryImplementation([OneSignal class], @selector(launchWebURL:), @selector(overrideLaunchWebURL:));
5967
_overrideLaunchTime = false;
6068
}
6169

70+
+ (void)reset {
71+
_launchWebURLWasCalled = false;
72+
}
73+
6274
+ (BOOL)shouldOverrideSessionLaunchTime {
6375
return _overrideLaunchTime;
6476
}
@@ -67,6 +79,22 @@ + (void)setShouldOverrideSessionLaunchTime:(BOOL)shouldOverrideSessionLaunchTime
6779
_overrideLaunchTime = shouldOverrideSessionLaunchTime;
6880
}
6981

82+
+ (BOOL)shouldOverrideLaunchURL {
83+
return _overrideLaunchURL;
84+
}
85+
86+
+ (void)setShouldOverrideLaunchURL:(BOOL)shouldOverrideLaunchURL {
87+
_overrideLaunchURL = shouldOverrideLaunchURL;
88+
}
89+
90+
+ (BOOL)launchWebURLWasCalled {
91+
return _launchWebURLWasCalled;
92+
}
93+
94+
+ (void)setLaunchWebURLWasCalled:(BOOL)launchWebURLWasCalled {
95+
_launchWebURLWasCalled = launchWebURLWasCalled;
96+
}
97+
7098
@end
7199

72100
#pragma clang diagnostic pop

iOS_SDK/OneSignalSDK/UnitTests/UnitTestCommonMethods.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#import "OSMessagingControllerOverrider.h"
5959
#import "OneSignalLifecycleObserver.h"
6060
#import "OneSignalLocationOverrider.h"
61+
#import "OneSignalOverrider.h"
6162

6263
NSString * serverUrlWithPath(NSString *path) {
6364
return [OS_API_SERVER_URL stringByAppendingString:path];
@@ -246,6 +247,7 @@ + (void) beforeEachTest:(XCTestCase *)testCase {
246247
[self clearStateForAppRestart:testCase];
247248

248249
[NSDateOverrider reset];
250+
[OneSignalOverrider reset];
249251
[OneSignalClientOverrider reset:testCase];
250252
[NSUserDefaultsOverrider clearInternalDictionary];
251253
UNUserNotificationCenterOverrider.notifTypesOverride = 7;

iOS_SDK/OneSignalSDK/UnitTests/UnitTests.m

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
#import "OneSignalLocation.h"
7272
#import "OneSignalLocationOverrider.h"
7373
#import "UIDeviceOverrider.h"
74+
#import "OneSignalOverrider.h"
7475

7576
// Dummies
7677
#import "DummyNotificationCenterDelegate.h"
@@ -106,6 +107,7 @@ @implementation UnitTests
106107
- (void)setUp {
107108
[super setUp];
108109
[UnitTestCommonMethods beforeEachTest:self];
110+
OneSignalOverrider.shouldOverrideLaunchURL = false;
109111

110112
// Only enable remote-notifications in UIBackgroundModes
111113
NSBundleOverrider.nsbundleDictionary = @{@"UIBackgroundModes": @[@"remote-notification"]};
@@ -2992,4 +2994,30 @@ - (void)testNotificationJson {
29922994
XCTAssertEqualObjects(json[@"templateName"], @"Template name");
29932995
}
29942996

2997+
- (void)testLaunchURL {
2998+
2999+
// 1. Init OneSignal with app start
3000+
[UnitTestCommonMethods initOneSignal];
3001+
[UnitTestCommonMethods runBackgroundThreads];
3002+
3003+
// 2. Simulate a notification being opened
3004+
let notifResponse = [self createBasiciOSNotificationResponse];
3005+
let notifCenter = UNUserNotificationCenter.currentNotificationCenter;
3006+
let notifCenterDelegate = notifCenter.delegate;
3007+
[notifCenterDelegate userNotificationCenter:notifCenter didReceiveNotificationResponse:notifResponse withCompletionHandler:^() {}];
3008+
3009+
// 3. Setup OneSignal.setNotificationOpenedHandler
3010+
__block BOOL openedWasFire = false;
3011+
[OneSignal setNotificationOpenedHandler:^(OSNotificationOpenedResult * _Nonnull result) {
3012+
openedWasFire = true;
3013+
}];
3014+
// 4. Wait for open event to fire
3015+
[UnitTestCommonMethods runBackgroundThreads];
3016+
3017+
// 5. Ensure the OneSignal public callback fired
3018+
XCTAssertTrue(openedWasFire);
3019+
3020+
// 6. Ensure the launch URL was not opened
3021+
XCTAssertFalse(OneSignalOverrider.launchWebURLWasCalled);
3022+
}
29953023
@end

0 commit comments

Comments
 (0)