Last active
January 25, 2022 06:49
-
-
Save tigi44/5a457075bde06a79ccab617441331596 to your computer and use it in GitHub Desktop.
Local Notification on SwiftUI
This file contains hidden or 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
import SwiftUI | |
@main | |
struct NotificationSwiftUIApp: App { | |
var body: some Scene { | |
WindowGroup { | |
ContentView() | |
.task { | |
await NotificationHelper.shared.requestAuth() | |
} | |
.onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { _ in | |
UIApplication.shared.applicationIconBadgeNumber = 0 | |
Task { | |
await NotificationHelper.shared.addNotification() | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or 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
import SwiftUI | |
protocol Notification { | |
static var shared: Notification { get } | |
func requestAuth() async | |
func addNotification() async | |
func removeNotification() | |
} | |
class NotificationHelper: NSObject, Notification { | |
static let shared: Notification = NotificationHelper() | |
private override init() { | |
super.init() | |
UNUserNotificationCenter.current().delegate = self | |
} | |
func requestAuth() async { | |
do { | |
try await UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) | |
} catch { | |
} | |
} | |
func addNotification() async { | |
let settings = await UNUserNotificationCenter.current().notificationSettings() | |
guard settings.authorizationStatus == .authorized else { | |
removeNotification() | |
return | |
} | |
let content = UNMutableNotificationContent() | |
content.title = "Test Title" | |
content.body = "test message" | |
content.sound = UNNotificationSound.default | |
content.badge = 1 | |
let dateInfo = Calendar.current.dateComponents([.weekday, .hour, .minute, .second], from: Date()) | |
let trigger = UNCalendarNotificationTrigger(dateMatching: dateInfo, repeats: true) | |
let request = UNNotificationRequest(identifier: "identifier", content: content, trigger: trigger) | |
do { | |
try await UNUserNotificationCenter.current().add(request) | |
} catch { | |
} | |
} | |
func removeNotification() { | |
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: ["identifier"]) | |
// UNUserNotificationCenter.current().removeAllPendingNotificationRequests() | |
} | |
} | |
extension NotificationHelper: UNUserNotificationCenterDelegate { | |
func userNotificationCenter(_ center: UNUserNotificationCenter, | |
openSettingsFor notification: UNNotification?) { | |
} | |
func userNotificationCenter(_ center: UNUserNotificationCenter, | |
didReceive response: UNNotificationResponse, | |
withCompletionHandler completionHandler: @escaping () -> Void) { | |
completionHandler() | |
} | |
func userNotificationCenter(_ center: UNUserNotificationCenter, | |
willPresent notification: UNNotification, | |
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { | |
return [.sound, .banner] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment