From 9762eab8bd8d70bf3e6c17e9d98ffda260ebeccc Mon Sep 17 00:00:00 2001 From: Soner Yuksel Date: Thu, 14 Sep 2023 13:37:10 -0400 Subject: [PATCH 1/2] Canceling remote local default notification in case of possible default browser set --- .../Browser/BrowserViewController.swift | 22 ++++++++++++++++--- Sources/BraveStrings/BraveStrings.swift | 4 ++-- .../Onboarding/OnboardingPreferences.swift | 7 +++++- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/Sources/Brave/Frontend/Browser/BrowserViewController.swift b/Sources/Brave/Frontend/Browser/BrowserViewController.swift index 190437c7000..c6292c31888 100644 --- a/Sources/Brave/Frontend/Browser/BrowserViewController.swift +++ b/Sources/Brave/Frontend/Browser/BrowserViewController.swift @@ -872,9 +872,18 @@ public class BrowserViewController: UIViewController { } } - if !Preferences.DefaultBrowserIntro.defaultBrowserNotificationScheduled.value { + // Schedule Default Browser Local Notification + // If notification is not already scheduled or + // an external URL opened in Brave (which indicates Brave is set as default) + if !Preferences.DefaultBrowserIntro.defaultBrowserNotificationScheduled.value, + Preferences.DefaultBrowserIntro.defaultBrowserNotificationShouldBeShown.value { scheduleDefaultBrowserNotification() } + + // Remove pending notification if default browser is already set + if !Preferences.DefaultBrowserIntro.defaultBrowserNotificationShouldBeShown.value { + cancelScheduleDefaultBrowserNotification() + } privateModeCancellable = privateBrowsingManager .$isPrivateBrowsing @@ -943,7 +952,7 @@ public class BrowserViewController: UIViewController { let content = UNMutableNotificationContent().then { $0.title = Strings.DefaultBrowserCallout.notificationTitle - $0.body = String(format: Strings.DefaultBrowserCallout.notificationBody, String(ProcessInfo().operatingSystemVersion.majorVersion)) + $0.body = Strings.DefaultBrowserCallout.notificationBody } let timeToShow = AppConstants.buildChannel.isPublic ? 2.hours : 2.minutes @@ -966,6 +975,11 @@ public class BrowserViewController: UIViewController { } } + private func cancelScheduleDefaultBrowserNotification() { + let center = UNUserNotificationCenter.current() + center.removePendingNotificationRequests(withIdentifiers: [Self.defaultBrowserNotificationId]) + } + private func executeAfterSetup(_ block: @escaping () -> Void) { if setupTasksCompleted { block() @@ -3168,8 +3182,10 @@ extension BrowserViewController { } public func handleNavigationPath(path: NavigationPath) { - // Remove Default Browser Callout if an external url is triggered + // Remove Default Browser Callout - Do not show scheduled notification + // in case an external url is triggered Preferences.General.defaultBrowserCalloutDismissed.value = true + Preferences.DefaultBrowserIntro.defaultBrowserNotificationShouldBeShown.value = false executeAfterSetup { NavigationPath.handle(nav: path, with: self) diff --git a/Sources/BraveStrings/BraveStrings.swift b/Sources/BraveStrings/BraveStrings.swift index b21d7965620..0ba03caf142 100644 --- a/Sources/BraveStrings/BraveStrings.swift +++ b/Sources/BraveStrings/BraveStrings.swift @@ -184,14 +184,14 @@ extension Strings { NSLocalizedString( "defaultBrowserCallout.notificationTitle", tableName: "BraveShared", bundle: .module, - value: "Brave Web Browser", + value: "Get Brave protection, on every link", comment: "Notification title to promote setting Brave app as default browser") public static let notificationBody = NSLocalizedString( "defaultBrowserCallout.notificationBody", tableName: "BraveShared", bundle: .module, - value: "Optimized for iOS %@. Make Brave your default browser today.", + value: "Set Brave as your default browser", comment: "Notification body to promote setting Brave app as default browser") } } diff --git a/Sources/Onboarding/OnboardingPreferences.swift b/Sources/Onboarding/OnboardingPreferences.swift index 69b27e9741e..313e538feef 100644 --- a/Sources/Onboarding/OnboardingPreferences.swift +++ b/Sources/Onboarding/OnboardingPreferences.swift @@ -84,9 +84,14 @@ extension Preferences { key: "defaultBrowserIntro.intro-completed", default: false) - /// Whether system notification showed or not + /// Whether system notification scheduled or not public static let defaultBrowserNotificationScheduled = Option( key: "general.default-browser-notification-scheduled", default: false) + + /// Whether a default browser local notification should be shown + public static let defaultBrowserNotificationShouldBeShown = Option( + key: "general.default-browser-notification-shown", + default: true) } } From 5a0f5d2f3b5b421b12cf57a366178420d5bc993a Mon Sep 17 00:00:00 2001 From: Soner Yuksel Date: Fri, 15 Sep 2023 16:13:50 -0400 Subject: [PATCH 2/2] Passed in Path URL --- .../Brave/Frontend/Browser/BrowserViewController.swift | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Sources/Brave/Frontend/Browser/BrowserViewController.swift b/Sources/Brave/Frontend/Browser/BrowserViewController.swift index c6292c31888..a958d47cb8b 100644 --- a/Sources/Brave/Frontend/Browser/BrowserViewController.swift +++ b/Sources/Brave/Frontend/Browser/BrowserViewController.swift @@ -3184,8 +3184,12 @@ extension BrowserViewController { public func handleNavigationPath(path: NavigationPath) { // Remove Default Browser Callout - Do not show scheduled notification // in case an external url is triggered - Preferences.General.defaultBrowserCalloutDismissed.value = true - Preferences.DefaultBrowserIntro.defaultBrowserNotificationShouldBeShown.value = false + if case .url(let navigatedURL, _) = path { + if navigatedURL?.isWebPage(includeDataURIs: false) == true { + Preferences.General.defaultBrowserCalloutDismissed.value = true + Preferences.DefaultBrowserIntro.defaultBrowserNotificationShouldBeShown.value = false + } + } executeAfterSetup { NavigationPath.handle(nav: path, with: self)