-
Notifications
You must be signed in to change notification settings - Fork 0
/
HabitsNotificationsDelegate.swift
48 lines (40 loc) · 1.27 KB
/
HabitsNotificationsDelegate.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//
// HabitsNotificationsDelegate.swift
// Habits
//
// Created by Mathis Le Bonniec on 24/02/2024.
//
import UIKit
import SwiftUI
class HabitsNotificationsDelegate: NSObject, ObservableObject {
// MARK: Published Properties
@Published var shownDetails: HabitsIds?
// MARK: Lifecycle
override init() {
super.init()
NotificationsHelper.currentNotificationCenter.delegate = self
}
}
extension HabitsNotificationsDelegate: UNUserNotificationCenterDelegate {
// MARK: Methods
func userNotificationCenter(
_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
) {
completionHandler([.banner, .sound])
}
func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void
) {
let userInfo: [AnyHashable: Any] = response.notification.request.content.userInfo
guard let stringId: String = userInfo[NotificationsHelper.UserInfo.Keys.id.rawValue] as? String,
let habitId: HabitsIds = HabitsIds(rawValue: stringId) else {
return
}
shownDetails = habitId
completionHandler()
}
}