Fire UILocalNotification in a specific Date

前端 未结 3 1449
太阳男子
太阳男子 2021-01-16 10:32

I want to fire a UILocalNotification in a specific Date. If I use this code:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentif         


        
3条回答
  •  一个人的身影
    2021-01-16 11:09

    In swift 3 - 4 you can use this for specific time local notification.

      func scheduleLocal() {
    
            let dateComponents = DateComponents(year: 2019, month: 1, day: 17, hour: 10, minute: 20)
            let yourFireDate = Calendar.current.date(from: dateComponents)
    
            let notification = UILocalNotification()
            notification.fireDate = yourFireDate
            notification.alertBody = "Hey you! Yeah you! Swipe to unlock!"
            notification.alertAction = "be awesome!"
            notification.soundName = UILocalNotificationDefaultSoundName
            notification.userInfo = ["CustomField1": "w00t"]
            UIApplication.shared.scheduleLocalNotification(notification)
    
            guard let settings = UIApplication.shared.currentUserNotificationSettings else { return }
    
            if settings.types == .none {
                let ac = UIAlertController(title: "Can't schedule", message: "Either we don't have permission to schedule notifications, or we haven't asked yet.", preferredStyle: .alert)
                ac.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
                present(ac, animated: true, completion: nil)
                return
            }
    
        }
    

提交回复
热议问题