计划通知不适用于警报应用

时间:2019-02-06 12:18:53

标签: swift uilocalnotification alarm

您好,我正在尝试使用此通知https://stackoverflow.com/a/53902489使用Schedule Notification,我已经设置了时间但是却一无所获,我想设置时间并在特定时间提醒某些东西并响起声音< / p>

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

1 个答案:

答案 0 :(得分:0)

您可以尝试为您的请求创建唯一的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
    }
    

希望对您有帮助...