Skip to content

Latest commit

 

History

History
100 lines (79 loc) · 4.1 KB

File metadata and controls

100 lines (79 loc) · 4.1 KB

NotificationView

A Swift package to elegantly ask for notification permissions just like Apple's apps.

Screenshots

Simulator Screenshot - iPhone 16 Pro 2 - 2024-10-03 at 16 37 46-portrait Simulator Screenshot - iPhone 16 Pro 2 - 2024-10-03 at 16 37 52-portrait

Installation

Swift Package Manager

Add https://github.com/FPST-08/NotificationView in the “Swift Package Manager” tab in Xcode.

Usage

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.

Using the Modifier

.notificationView requires a title, subtitle, and a boolean binding to control when the notification view is presented from a parent view.

Initialization

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)
)

Example

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]
        )
    }
}

Navigation & UserNotifications

The modifier will automatically handle requesting permissions and navigation, dismissing itself when permissions are granted, not allowed, or an error occurs.

Using the View directly

`NotificationView' requires a title, subtitle and a closure. Optionally a buttonTitle can be provided.

Initialization

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
)

Example

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
             }
         }
     }
 }

Navigation & UserNotifications

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

Contributors

Made with contrib.rocks.

License

NotificationView is available under the MIT license. See the LICENSE file for more info.