使用来自领域数据库swift的日期设置UserNotification

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用来自领域数据库swift的日期设置UserNotification相关的知识,希望对你有一定的参考价值。

我正在尝试根据用户为我正在处理的应用程序指定的日期和时间创建通知。我能够保存用户的日期并检索。我现在想要在用户的指定日期到达时弹出通知,就像提醒一样。我可以创建一个带有硬编码值的通知,但现在从Real数据库中恢复以传递到触发器值是我无法做到的。我的代码如下所示

func notifcation() -> Void 

        let calendar = Calendar.current
        let components = DateComponents(year: 2018, month: 09, day: 08, hour: 18, minute: 55) // Set the date here when you want Notification
        let date = calendar.date(from: components)
        let comp2 = calendar.dateComponents([.year,.month,.day,.hour,.minute], from: date!)
        let trigger = UNCalendarNotificationTrigger(dateMatching: comp2, repeats: true)
        let content = UNMutableNotificationContent()
        content.title = "Don't forget"
        content.body = "Buy some milk"
        content.sound = UNNotificationSound.default()
        let identifier = "UYLLocalNotification"
        let request = UNNotificationRequest(identifier: identifier,
                                            content: content, trigger: trigger)

        center.add(request, withCompletionHandler:  (error) in
            if let error = error 
                // Something went wrong
                print(error as Any)
            
        )
    

然后我在viewDidLoad中调用notification()

模型

class ListModel: Object 
    @objc dynamic var createdDate: Date?
    @objc dynamic var remiderDate: Date?

基于Sh回答我的功能

func getDataFromDB() -> Results<ListModel>? 
        let todoListArray: Results<ListModel> = database.objects(ListModel.self)
        return todoListArray
    

我的viewdidLoad现在

TodoListFunctions.instance.getDataFromDB()?.forEach( (list) in

            notifcation(list.remiderDate)
        )

在我的AppDelegate for Permission中

let center = UNUserNotificationCenter.current()
        let options: UNAuthorizationOptions = [.alert, .sound]

        center.requestAuthorization(options: options) 
            (granted, error) in
            if !granted 
                print("Something went wrong")
            
        

将根据要求提供更多代码

答案

你可以试试

let allList = self.database.objects(ListModel.self)
allList.forEach notifcation($0.remiderDate) 

//

func notifcation(_ reminder:Date?) -> Void  

        guard let date = reminder else  return 
        let comp2 = calendar.dateComponents([.year,.month,.day,.hour,.minute], from: date)
        let trigger = UNCalendarNotificationTrigger(dateMatching: comp2, repeats: true)
        let content = UNMutableNotificationContent()
        content.title = "Don't forget"
        content.body = "Buy some milk"
        content.sound = UNNotificationSound.default()
        let identifier = "\(date)"  // identider should be different for each one 
        let request = UNNotificationRequest(identifier: identifier,
                                            content: content, trigger: trigger)

        center.add(request, withCompletionHandler:  (error) in
            if let error = error 
                // Something went wrong
                print(error as Any)
            
        )
    

//

在AppDelegte中设置委托

UNUserNotificationCenter.current().delegate = self

并注意到打印

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) 
  print("notification received")

以上是关于使用来自领域数据库swift的日期设置UserNotification的主要内容,如果未能解决你的问题,请参考以下文章

Swift - 将字符串转换为日期

来自字符串的 Swift DateFormatter 日期在 NSDateFormatter 曾经工作的地方失败

如何设置下载数据的时间限制(来自json数据库)Swift 2

来自 UICollectionView 中的领域同步配置的种子数据

从核心数据中按日期对 TableView 进行排序 - Swift4

如何从swift中的时间戳获取日期格式