在 Iphone 上根据系统时间发送本地通知?

问题描述

在我的应用中,用户会设置提醒的日期和时间,即使应用关闭,手机也需要在该特定时间发送本地通知

能够使用以下方法在特定时间发送通知。但是当应用程序关闭时它不起作用:

public func simpleAddNotification(hour: Int,minute: Int,identifier: String,title: String,body: String) {
    // Initialize User Notification Center Object
    let center = UNUserNotificationCenter.current()

    // The content of the Notification
    let content = UNMutableNotificationContent()
    content.title = title
    content.body = body
    content.sound = .default
    content.badge = 1


    // The selected time to notify the user
    var dateComponents = DateComponents()
    dateComponents.calendar = Calendar.current
    dateComponents.hour = hour
    dateComponents.minute = minute

    // The time/repeat trigger
    let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents,repeats: true)

    // Initializing the Notification Request object to add to the Notification Center
    let request = UNNotificationRequest(identifier: identifier,content: content,trigger: trigger)

    // Adding the notification to the center
    center.add(request) { (error) in
        if (error) != nil {
            print(error!.localizedDescription)
        }
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)