【问题标题】:Schedule notification not working for alarm app计划通知不适用于警报应用程序
【发布时间】:2019-02-06 12:18:53
【问题描述】:

您好,我正在尝试制作闹钟应用程序,我正在使用此帖子https://stackoverflow.com/a/53902489 使用日程安排通知我已设置时间但一无所获,我想设置时间并提醒某些内容并在特定时间响铃

    scheduleNotification(at: createDate(date : 6, month : 2, hour: 17, minute: 15, year: 2019), identifierUnic: stringUUID(), body: "Notification day", titles: "Notification titles1")


      func createDate(date: Int, month : Int, hour: Int, minute: Int, year: Int)->Date {

            var components = DateComponents()
            components.hour = hour
            components.minute = minute
            components.year = year
            components.day = date
            components.month = month

            components.timeZone = .current

            let calendar = Calendar(identifier: .gregorian)
            return calendar.date(from: components)!
        }


   func scheduleNotification(at date: Date, identifierUnic : String, body: String, titles:String) {
        let center = UNUserNotificationCenter.current()
        center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in }

         var anniversary = DateComponents()
        anniversary.day = 6
        anniversary.month = 2
        anniversary.hour = 5
        anniversary.minute = 44
        anniversary.second = 0

        let trigger = UNCalendarNotificationTrigger(dateMatching: anniversary, repeats: true)
        let content = UNMutableNotificationContent()
        content.title = titles
        content.body = body
        content.sound = UNNotificationSound.default
        content.categoryIdentifier = identifierUnic

        let request = UNNotificationRequest(identifier: identifierUnic, content: content, trigger: trigger)

        UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate

        UNUserNotificationCenter.current().add(request) {(error) in
            if let error = error {
                print(" We had an error: \(error)")
            }}
    }


      func registerLocal() {
            let center = UNUserNotificationCenter.current()

            center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
                if granted {
                    print("Yay!")
                } else {
                    print("D'oh")
                }
            }
        }

【问题讨论】:

    标签: swift uilocalnotification alarm


    【解决方案1】:

    您能否尝试为您的请求创建一个唯一的 ID,并在其中添加您的通知。

    这是我的通知,对我来说效果很好

    1. 创建中心并检查授权

      let center = UNUserNotificationCenter.current()
      center.requestAuthorization(options: [.alert, .sound]) {
          (granted, error) in
      }
      
    2. 创建内容

      let content = UNMutableNotificationContent()
      content.title = "Title"
      content.subtitle = "Subtitle."
      content.body = "Body"
      
    3. 创建日期组件

      var anniversary = DateComponents()
      anniversary.day = 1
      anniversary.month = 1
      anniversary.hour = 12
      anniversary.minute = 0
      anniversary.second = 0
      
    4. 创建触发器

      let trigger = UNCalendarNotificationTrigger(dateMatching: anniversary, repeats: true)
      
    5. 创建唯一 ID 并请求

      let uuid = UUID().uuidString
      let request = UNNotificationRequest(identifier: uuid, content: content, trigger: trigger)
      
    6. 将您的请求添加到中心并处理您的错误

      center.add(request) {
          (error) in
         //Write your handle function here..
         //print(error) for example
      }
      

    希望对你有帮助……

    【讨论】:

    • 兄弟仍然无法工作我没有任何错误权限授予检查更新的代码
    • 我不知道出了什么问题。是否有错误消息打印出来,或者构建成功并且应用程序没有发送通知?
    • app运行成功,没有报错,也没有及时通知
    • 在 request.identifier 中预定但触发
    猜你喜欢
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-16
    相关资源
    最近更新 更多