-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ios): add local and remote notification router (#3796)
Co-authored-by: Ian Keith <[email protected]>
- Loading branch information
Showing
9 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import Foundation | ||
|
||
@objc(CAPNotificationHandlerProtocol) public protocol NotificationHandlerProtocol { | ||
func willPresent(notification: UNNotification) -> UNNotificationPresentationOptions | ||
func didReceive(response: UNNotificationResponse) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import Foundation | ||
|
||
@objc(CAPNotificationRouter) public class NotificationRouter: NSObject, UNUserNotificationCenterDelegate { | ||
var handleApplicationNotifications: Bool { | ||
get { | ||
return UNUserNotificationCenter.current().delegate === self | ||
} | ||
set { | ||
let center = UNUserNotificationCenter.current() | ||
|
||
if newValue { | ||
center.delegate = self | ||
} else if center.delegate === self { | ||
center.delegate = nil | ||
} | ||
} | ||
} | ||
|
||
public weak var pushNotificationHandler: NotificationHandlerProtocol? { | ||
didSet { | ||
if pushNotificationHandler != nil, oldValue != nil { | ||
CAPLog.print("Push notification handler overriding previous instance: \(String(describing: type(of: oldValue)))") | ||
} | ||
} | ||
} | ||
|
||
public weak var localNotificationHandler: NotificationHandlerProtocol? { | ||
didSet { | ||
if localNotificationHandler != nil, oldValue != nil { | ||
CAPLog.print("Local notification handler overriding previous instance: \(String(describing: type(of: oldValue)))") | ||
} | ||
} | ||
} | ||
|
||
public func userNotificationCenter(_ center: UNUserNotificationCenter, | ||
willPresent notification: UNNotification, | ||
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { | ||
let presentationOptions: UNNotificationPresentationOptions? | ||
|
||
if notification.request.trigger?.isKind(of: UNPushNotificationTrigger.self) == true { | ||
presentationOptions = pushNotificationHandler?.willPresent(notification: notification) | ||
} else { | ||
presentationOptions = localNotificationHandler?.willPresent(notification: notification) | ||
} | ||
|
||
completionHandler(presentationOptions ?? []) | ||
} | ||
|
||
public func userNotificationCenter(_ center: UNUserNotificationCenter, | ||
didReceive response: UNNotificationResponse, | ||
withCompletionHandler completionHandler: @escaping () -> Void) { | ||
if response.notification.request.trigger?.isKind(of: UNPushNotificationTrigger.self) == true { | ||
pushNotificationHandler?.didReceive(response: response) | ||
} else { | ||
localNotificationHandler?.didReceive(response: response) | ||
} | ||
|
||
completionHandler() | ||
} | ||
} |