@@ -95,20 +95,20 @@ final class PushNotificationsManager: PushNotesManager {
9595//
9696extension PushNotificationsManager {
9797
98- /// Requests Authorization to receive Push Notifications, *only* when the current Status is not determined.
98+ /// Requests Authorization to receive Push Notifications, *only* when the current Status is not determined or provisional .
9999 ///
100100 /// - Parameter onCompletion: Closure to be executed on completion. Receives a Boolean indicating if we've got Push Permission.
101101 ///
102- func ensureAuthorizationIsRequested( onCompletion: ( ( Bool ) -> Void ) ? = nil ) {
102+ func ensureAuthorizationIsRequested( includesProvisionalAuth : Bool = false , onCompletion: ( ( Bool ) -> Void ) ? = nil ) {
103103 let nc = configuration. userNotificationsCenter
104104
105105 nc. loadAuthorizationStatus ( queue: . main) { status in
106- guard status == . notDetermined else {
106+ guard status == . notDetermined || status == . provisional else {
107107 onCompletion ? ( status == . authorized)
108108 return
109109 }
110110
111- nc. requestAuthorization ( queue: . main) { allowed in
111+ nc. requestAuthorization ( queue: . main, includesProvisionalAuth : includesProvisionalAuth ) { allowed in
112112 let stat : WooAnalyticsStat = allowed ? . pushNotificationOSAlertAllowed : . pushNotificationOSAlertDenied
113113 ServiceLocator . analytics. track ( stat)
114114
@@ -261,6 +261,54 @@ extension PushNotificationsManager {
261261 }
262262 }
263263 }
264+
265+ func requestLocalNotification( _ notification: LocalNotification , trigger: UNNotificationTrigger ? ) {
266+ Task {
267+ // TODO: 7318 - tech debt - replace `UNUserNotificationCenter.current()` with
268+ // `configuration.userNotificationsCenter` for unit testing
269+ let center = UNUserNotificationCenter . current ( )
270+ let settings = await center. notificationSettings ( )
271+ guard settings. authorizationStatus == . authorized || settings. authorizationStatus == . provisional else {
272+ DDLogError ( " ⛔️ Unable to request a local notification due to invalid authorization status: \( settings. authorizationStatus) " )
273+ return
274+ }
275+
276+ let content = UNMutableNotificationContent ( )
277+ content. title = notification. title
278+ content. body = notification. body
279+
280+ if let categoryAndActions = notification. actions {
281+ let categoryIdentifier = categoryAndActions. category. rawValue
282+ let actions = categoryAndActions. actions. map {
283+ UNNotificationAction ( identifier: $0. rawValue,
284+ title: $0. title,
285+ options: . foreground)
286+ }
287+ let category = UNNotificationCategory ( identifier: categoryIdentifier,
288+ actions: actions,
289+ intentIdentifiers: [ ] ,
290+ hiddenPreviewsBodyPlaceholder: nil ,
291+ categorySummaryFormat: nil ,
292+ options: . allowAnnouncement)
293+ center. setNotificationCategories ( [ category] )
294+ content. categoryIdentifier = categoryIdentifier
295+ }
296+
297+ let request = UNNotificationRequest ( identifier: notification. scenario. rawValue,
298+ content: content,
299+ trigger: trigger)
300+ do {
301+ try await center. add ( request)
302+ } catch {
303+ DDLogError ( " ⛔️ Unable to request a local notification: \( error) " )
304+ }
305+ }
306+ }
307+
308+ func cancelLocalNotification( scenarios: [ LocalNotification . Scenario ] ) {
309+ let center = UNUserNotificationCenter . current ( )
310+ center. removePendingNotificationRequests ( withIdentifiers: scenarios. map { $0. rawValue } )
311+ }
264312}
265313
266314// MARK: - Notification count & app badge number update
0 commit comments