-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement example of notification support
- Loading branch information
Showing
22 changed files
with
812 additions
and
120 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
81 changes: 81 additions & 0 deletions
81
Sources/Spezi/Capabilities/Notifications/NotificationHandler.swift
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,81 @@ | ||
// | ||
// This source file is part of the Stanford Spezi open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2024 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import UserNotifications | ||
|
||
|
||
/// Get notified about receiving notifications. | ||
public protocol NotificationHandler { | ||
/// Handle user-selected notification action. | ||
/// | ||
/// This method is called with your app in the background to handle the selected user action. | ||
/// | ||
/// For more information refer to [Handle user-selected actions](https://developer.apple.com/documentation/usernotifications/handling-notifications-and-notification-related-actions#Handle-user-selected-actions) | ||
/// and [`userNotificationCenter(_:didReceive:withCompletionHandler:)`](https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate/usernotificationcenter(_:didreceive:withcompletionhandler:)). | ||
/// | ||
/// - Parameter response: The user's response to the notification. | ||
func handleNotificationAction(_ response: UNNotificationResponse) async | ||
|
||
/// Handle incoming notification when the app is running in foreground. | ||
/// | ||
/// This method is called when there is a incoming notification while the app was running in foreground. | ||
/// | ||
/// For more information refer to | ||
/// [Handle notifications while your app runs in the foreground](https://developer.apple.com/documentation/usernotifications/handling-notifications-and-notification-related-actions#Handle-notifications-while-your-app-runs-in-the-foreground) | ||
/// and [`userNotificationCenter(_:willPresent:withCompletionHandler:)`](https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate/usernotificationcenter(_:willpresent:withcompletionhandler:)). | ||
/// | ||
/// - Parameter notification: The notification that is about to be delivered. | ||
/// - Returns: The option for notifying the user. Use `[]` to silence the notification. | ||
func receiveIncomingNotification(_ notification: UNNotification) async -> UNNotificationPresentationOptions | ||
|
||
#if !os(macOS) | ||
/// Handle remote notification when the app is running in background. | ||
/// | ||
/// This method is called when there is a remote notification arriving while the app is running in background. | ||
/// You can use this method to download additional content. | ||
/// | ||
/// For more information refer to | ||
/// [`application(_:didReceiveRemoteNotification:fetchCompletionHandler:)`](https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623013-application) | ||
/// or [`didReceiveRemoteNotification(_:fetchCompletionHandler:)`](https://developer.apple.com/documentation/watchkit/wkextensiondelegate/3152235-didreceiveremotenotification). | ||
/// | ||
/// - Parameter remoteNotification: The data of the notification payload. | ||
/// - Returns: Return the respective ``BackgroundFetchResult``. | ||
func receiveRemoteNotification(_ remoteNotification: [AnyHashable: Any]) async -> BackgroundFetchResult | ||
#else | ||
/// Handle remote notification when the app is running in background. | ||
/// | ||
/// This method is called when there is a remote notification arriving while the app is running in background. | ||
/// You can use this method to download additional content. | ||
/// | ||
/// For more information refer to | ||
/// [`application(_:didReceiveRemoteNotification:)`](https://developer.apple.com/documentation/appkit/nsapplicationdelegate/1428430-application). | ||
/// | ||
/// - Parameter remoteNotification: The data of the notification payload. | ||
func receiveRemoteNotification(_ remoteNotification: [AnyHashable: Any]) | ||
#endif | ||
} | ||
|
||
|
||
extension NotificationHandler { | ||
/// Empty default implementation. | ||
func handleNotificationAction(_ response: UNNotificationResponse) async {} | ||
|
||
/// Empty default implementation. | ||
func receiveIncomingNotification(_ notification: UNNotification) async -> UNNotificationPresentationOptions { | ||
// TODO: is there are better default? | ||
[.badge, .badge, .list, .sound] // default is to fully present the notification | ||
} | ||
|
||
#if !os(macOS) | ||
func receiveRemoteNotification(_ remoteNotification: [AnyHashable: Any]) async -> BackgroundFetchResult { | ||
.noData | ||
} | ||
#else | ||
func receiveRemoteNotification(_ remoteNotification: [AnyHashable: Any]) {} | ||
#endif | ||
} |
28 changes: 28 additions & 0 deletions
28
Sources/Spezi/Capabilities/Notifications/NotificationTokenHandler.swift
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,28 @@ | ||
// | ||
// This source file is part of the Stanford Spezi open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2024 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import Foundation | ||
|
||
|
||
/// Get notified about device token updates for APNs. | ||
/// | ||
/// Use this protocol when your Module needs to be notified about an updated device tokens | ||
/// for the Apple Push Notifications service. | ||
public protocol NotificationTokenHandler { | ||
/// Receive an updated device token for APNs. | ||
/// | ||
/// User this method to be notified about a changed device token for interaction | ||
/// with the Apple Push Notifications service. | ||
/// Use this method to send the updated token to your server-side infrastructure. | ||
/// | ||
/// - Note: Fore more information refer to the documentation of | ||
/// [`application(_:didRegisterForRemoteNotificationsWithDeviceToken:)`](https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1622958-application). | ||
/// - Parameter deviceToken: The globally unique token that identifies this device to APNs. | ||
@MainActor | ||
func receiveUpdatedDeviceToken(_ deviceToken: Data) | ||
} |
Oops, something went wrong.