Skip to content

Notifications

Ryan Rudes edited this page Nov 7, 2021 · 1 revision

Notifications

MoreUI features the scheduling of local notifications for delivery by exposing an extended UNUserNotificationCenter through a new environment value, \.notificationCenter. See the basic implementation below. For a screen recording demonstrating this code sample, see here.

struct ContentView: View {
    @Environment(\.notificationCenter) var notificationCenter
    
    var body: some View {
        Button("Send Notification") {
            notificationCenter.post(title: "My Notification",
                                    subtitle: "A brief description",
                                    body: "Some more info.",
                                    trigger: {
                let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 3, repeats: false)
                return trigger
            })
        }
    }
}

You can customize all properties exposed by UNNotificationContent, including the badge, sound, optional attachments, and much more. In addition, you can customize the trigger, as in, how the system delivers the notification.

All in all, here's a succinct documentation of the notification delivery method, post():

  • Parameters:
    • title: The localized title, containing the reason for the alert.
    • subtitle: The localized subtitle, containing a secondary description of the reason for the alert.
    • body: The localized message to display in the notification alert.
    • badge: The number to apply to the app’s icon.
    • sound: The sound to play when the system delivers the notification.
    • launchImageName: The name of the custom launch image to display when the system launches your app in response to the notification.
    • userInfo: A dictionary of custom information associated with the notification.
    • attachments: An array of attachments to display in an alert-based notification.
    • summaryArgument: The string the notification adds to the category’s summary format string. This argument has been deprecated starting in iOS 15.0.
    • summaryArgumentCount: The number of items the notification adds to the category’s summary format string. This argument has been deprecated starting in iOS 15.0.
    • categoryIdentifier: The identifier of the category object that represents the notification’s type.
    • threadIdentifier: An identifier that you use to group related notifications together.
    • targetContentIdentifier: A value your app uses to identify the notification content.
    • interruptionLevel: The interruption level determines the degree of interruption associated with the notification.
    • relevanceScore: The value the system uses to sort your app’s notifications.
    • options: The authorization options your app is requesting. You may combine the available constants to request authorization for multiple items. Request only the authorization options that you plan to use. For a list of possible values, see UNAuthorizationOptions. Some notification options are handled automatically based on the specification of other parameters, such as .badge, .sound, .criticalAlert, and .alert. These do not need to be specified manually by the user.
    • trigger: A function returning the condition that causes the system to deliver the notification. Specify nil to deliver the notification right away. See UNNotificationTrigger for documentation on concrete trigger classes.

Clone this wiki locally