Я искал похожие вопросы. Но я не понимаю, почему эта работа работает для меня. Пожалуйста, не отмечайте вопрос как дубликат.
Локальные уведомления - отлично работают как на переднем, так и на заднем плане на симуляторе, но не могут заставить их работать на устройстве.
AppDelegate
class AppDelegate: UIResponder, UIApplicationDelegate , UNUserNotificationCenterDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
print("granted: (\(granted)")
}
return true
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print(" Will Present")
completionHandler([.alert,.badge,.sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print( " Did receive" )
completionHandler()
}
}
ViewController
func addNotificationAlert(notificationDate: String , identifier : String){
let dateString: String = notificationDate // "20181026 10:55"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyyMMdd HH:mm"
let formattedDate1: Date = dateFormatter.date(from: dateString)!
let notificationTitleMessage = "Hey this is ~~~ \(identifier)"
let notificationsubTitleMessage = "iOS Development is fun"
let notificationbodyMessage = "We are learning about iOS Local Notification"
let notificationIdentifier = identifier
if #available(iOS 10.0, *) {
// For iOS 10 and above
let center = UNUserNotificationCenter.current()
let mutableNotificationcontent = UNMutableNotificationContent()
mutableNotificationcontent.title = notificationTitleMessage
mutableNotificationcontent.subtitle = notificationsubTitleMessage
mutableNotificationcontent.body = "notificationbodyMessage"
mutableNotificationcontent.userInfo = ["key1" : "value1" , "key2" : "value2"]
mutableNotificationcontent.sound = UNNotificationSound.default()
let dateComponents = Calendar.current.dateComponents([.year,.day,.month,.hour,.minute] , from: formattedDate1)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
let request = UNNotificationRequest(identifier: notificationIdentifier, content: mutableNotificationcontent, trigger: trigger)
center.add(request) { (error) in
if let error = error {
print("\(error)")
} else {
// Request was added successfully
print("Request was added successfully")
}
}
}
else{
// For iOS 9
let notification = UILocalNotification()
notification.fireDate = formattedDate1
notification.alertBody = notificationbodyMessage
notification.alertTitle = notificationTitleMessage
notification.alertAction = "be awesome!"
let dict:NSDictionary = ["key1" : "value1" , "key2" : "value2"]
notification.userInfo = dict as! [String : String]
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.shared.scheduleLocalNotification(notification)
}
}
«Запрос был успешно добавлен» отлично отображается в журнале.
Метод AppDelegate - получение не выполняется на устройстве.
Может кто-нибудь дать мне знать, если я что-то упустил. Любая помощь приветствуется!