【问题标题】:Setting UserNotification with date from a realm database swift使用领域数据库 swift 中的日期设置 UserNotification
【发布时间】:2018-09-08 18:03:27
【问题描述】:

我正在尝试根据用户为我正在处理的应用程序指定的日期和时间创建通知。我能够保存用户的日期并检索。我现在希望在用户指定的日期到达时弹出通知,就像提醒一样。我可以使用硬编码值创建通知,但现在从 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 中获取权限

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

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

可根据要求提供更多代码

【问题讨论】:

    标签: ios swift realm


    【解决方案1】:

    你可以试试

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

    【讨论】:

    • 我在 viewdidload TodoListFunctions.instance.getDataFromDB()?.forEach({ (todoListmodel) in notifcation(todoListmodel.remiderDate) }) 中调用了这个,但没有显示任何通知
    • 应用在前台时不会出现通知,你必须执行 willPresent 看这里thomashanning.com/… 记住你还必须获得权限
    • getDataFromDB()?不是零。仍然没有通知显示
    • 我打印了提醒日期,这就是它显示的内容REMINDER DATE Optional(2018-09-08 11:45:46 +0000)
    • 我想通了。你的方法效果很好。我的代码中有一个错误
    猜你喜欢
    • 2015-12-18
    • 1970-01-01
    • 2019-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    相关资源
    最近更新 更多