A Swift package to elegantly ask for notification permissions just like Apple's apps.
Add https://github.com/FPST-08/NotificationView in the “Swift Package Manager” tab in Xcode.
There are two ways to use. The NotifcationView
accepts a closure for the button and can be presented in other ways like Onboardings. On the other hand the modifier handles that for you and it just has to be presented.
.notificationView
requires a title, subtitle, and a boolean binding to control when the notification view is presented from a parent view.
init(
isPresented: Binding<Bool>, // A binding to control when the notification view is presented from a parent view
title: String, // A short, descriptive title like "Stay Motivated with Fitness Notifications"
subTitle: String, // A longer description of your notifications
notificationCenterOptions: UNAuthorizationOptions // The notification options you want to request (e.g. .alert, .badge, .sound)
)
import SwiftUI
import NotificationView
struct ContentView: View {
@State private var showNotificationView = false
var body: some View {
Button("Enable Notifications") {
showNotificationView.toggle()
}
.notificationView(
isPresented: $showNotificationView,
title: "Stay Motivated with Fitness Notifications",
subTitle: "Notifications can help you close your rings, cheer on your friends, and see what's new with Fitness+.",
notificationCenterOptions: [.alert, .badge, .sound]
)
}
}
The modifier will automatically handle requesting permissions and navigation, dismissing itself when permissions are granted, not allowed, or an error occurs.
`NotificationView' requires a title, subtitle and a closure. Optionally a buttonTitle can be provided.
init(
title: String, // A short, descriptive title like "Stay Motivated with Fitness Notifications"
subTitle: String, // A longer description of your notifications
buttonTitle: String = "Continue", // The text presented on the button like "Continue" or "Okay".
action: @escaping () async -> Void // The closure that is run when the button was pressed. Dismissing or navigation needs to be handled from there
)
struct ContentView: View {
@State private var isPresented = false
var body: some View {
Button("Show sheet") {
isPresented.toggle()
}
.fullScreenCover(isPresented: $isPresented) {
NotificationView(title: "Stay Motivated with Fitness Notifications", subTitle: "Notifications can help you close your rings, cheer on your friends, and see what's new with Fitness+", buttonTitle: "Continue") {
let auth = try? await UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound])
isPresented = false
}
}
}
}
The view will not handle requesting permissions or navigation. Both has to be done from the closure you pass in. If you want to use this view in your Onboarding or in a Sequence with other Views, using NotificationView directly is the better option
Made with contrib.rocks.
NotificationView
is available under the MIT license. See the LICENSE file for more info.