领域数据库Swift中设置带日期的UserNotificatio

领域数据库Swift中设置带日期的UserNotificatio

本文介绍了从领域数据库Swift中设置带日期的UserNotification的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据用户为我正在处理的应用程序指定的日期和时间创建通知.我可以保存用户的日期并进行检索.现在,我想在用户指定的日期到来时弹出通知,就像提醒用户一样.我可以创建带有硬编码值的通知,但是现在我无法执行从Real数据库检索并传递到触发值的操作.我的代码在下面指定

I am trying to create a notification based on date and time specified by a user for an application I am working on. I am able to save user's date and retrieve. I now want a notification to pop up when the user's specified date has arrived just like an reminder thing. I can create a notification with with hard coded values but now retriving from the Real database to pass into the trigger value is what I am unable to do. my codes are specified below

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()

then I call notification() in my viewDidLoad

模型

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现在是

my viewdidLoad was now

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

            notifcation(list.remiderDate)
        })

在我的AppDelegate中获得权限

in my 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")
            }
        }

进一步的代码将应要求提供

further codes would be supplied on request

推荐答案

您可以尝试

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中设置委托

Set the delegate inside 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 04:24