Skip to content

Instantly share code, notes, and snippets.

@anirudhamahale
Created January 15, 2020 09:59
Show Gist options
  • Save anirudhamahale/08d6d264edf15f154ccd6dbe83aa2766 to your computer and use it in GitHub Desktop.
Save anirudhamahale/08d6d264edf15f154ccd6dbe83aa2766 to your computer and use it in GitHub Desktop.
Manager to handle local notifications.
import UIKit
import UserNotifications
class LocalNotificationManager {
var notifications = [Notification]()
func scheduledNotifications(completion: ([Notification])->()) {
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().getPendingNotificationRequests { notifications in
print(notifications)
// let notificationObject = notifications.map ({ item -> Notification in
// let notification = Notification(id: item.identifier, title: item.content.title, datetime: <#T##DateComponents#>)
// })
}
completion([])
} else {
// Fallback on earlier versions
UIApplication.shared.scheduledLocalNotifications?.forEach { notification in
print(notification)
completion([])
}
}
}
func schedule() {
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().getNotificationSettings { settings in
switch settings.authorizationStatus {
case .notDetermined:
self.requestAuthorization()
case .authorized, .provisional:
self.scheduleNotifications()
default:
break // Do nothing
}
}
} else {
// Fallback on earlier versions
}
}
}
/// MARK:- PRIVATE METHODS
extension LocalNotificationManager {
private func requestAuthorization(){
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in
if granted == true && error == nil {
self.scheduleNotifications()
}
}
} else {
// Fallback on earlier versions
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
UIApplication.shared.registerUserNotificationSettings(settings)
}
}
private func scheduleNotifications() {
if #available(iOS 10.0, *) {
notifications.forEach { item in
let content = UNMutableNotificationContent()
content.title = item.title
content.sound = .default
content.userInfo = ["type": "local"]
let trigger = UNCalendarNotificationTrigger(dateMatching: item.datetime, repeats: false)
let request = UNNotificationRequest(identifier: item.id, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { error in
guard error == nil else { return }
print("Notification scheduled! --- ID = \(item.id)")
}
}
} else {
// Fallback on earlier versions
let notificationsObject = notifications.map({ item -> UILocalNotification in
let notification = UILocalNotification()
notification.alertTitle = item.title
notification.userInfo = ["type": "local"]
notification.fireDate = item.datetime.date
return notification
})
UIApplication.shared.cancelAllLocalNotifications()
UIApplication.shared.scheduledLocalNotifications = notificationsObject
}
}
}
struct Notification {
var id: String
var title: String
var datetime: DateComponents
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment