Слышать, у меня больше сомнений по поводу этого локального уведомления
- Как установить локальное уведомление без appdelegate?
- Как вызвать делегата с отдельным контроллером?
Вот мой код !!
import Foundation
import UserNotifications
class NotificationController {
private static var privateShared: NotificationController?
static var shared: NotificationController {
if privateShared == nil {
privateShared = NotificationController()
}
return privateShared!
}
class func destroy() {
privateShared = nil
}
private let notificationCenter = UNUserNotificationCenter.current()
func registerLocalNotification(controller: UIViewController){
notificationCenter.delegate = self
let options: UNAuthorizationOptions = [.alert, .sound, .badge]
notificationCenter.requestAuthorization(options: options) {
(didAllow, error) in
if !didAllow {
print("User has declined notifications")
}
}
}
}
Расширение класса уведомлений:
extension NotificationController: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
if response.notification.request.identifier == "Local Notification" {
print("Handling notifications with the Local Notification Identifier")
}
completionHandler()
}
func scheduleNotification(notificationType: String) {
let content = UNMutableNotificationContent() // Содержимое уведомления
let categoryIdentifire = "Delete Notification Type"
content.title = notificationType
content.body = "This is example how to create " + notificationType
content.sound = UNNotificationSound.default
content.badge = 1
content.categoryIdentifier = categoryIdentifire
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let identifier = "Local Notification"
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
notificationCenter.add(request) { (error) in
if let error = error {
print("Error \(error.localizedDescription)")
}
}
let snoozeAction = UNNotificationAction(identifier: "Snooze", title: "Snooze", options: [])
let deleteAction = UNNotificationAction(identifier: "DeleteAction", title: "Delete", options: [.destructive])
let category = UNNotificationCategory(identifier: categoryIdentifire,
actions: [snoozeAction, deleteAction],
intentIdentifiers: [],
options: [])
notificationCenter.setNotificationCategories([category])
}
}
Я не могу настроить локальное уведомление без appdelegate
что-нибудь пропущено на моей стороне?
Любая ссылка оцените !!
Спасибо.