J'ai récemment mis à jour mon projet vers IOS 10, ce qui a empêché toute notification locale enregistrée. J'ai essayé de convertir la notification locale en notifications utilisateur, mais cela n'a pas le même effet. Voici le code original que j'ai essayé de convertir. Au sein viewDidLoad:Swift: Conversion d'une notification locale en notification utilisateur IOS 10
guard let settings = UIApplication.shared.currentUserNotificationSettings else { return
}
if settings.types == UIUserNotificationType() {
let ac = UIAlertController(title: "Cant Schedule", message: "No Permission", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
present(ac, animated: true, completion: nil)
return
}
var dateComp: DateComponents = DateComponents()
dateComp.year = 2017;
dateComp.month = 01;
dateComp.day = 28;
dateComp.hour = 11;
dateComp.minute = 0;
(dateComp as NSDateComponents).timeZone = TimeZone.current
let calender:Calendar = Calendar(identifier: Calendar.Identifier.gregorian)
let date:Date = calender.date(from: dateComp)!
let notification = UILocalNotification()
notification.fireDate = date
notification.alertBody = "Notification!"
notification.alertAction = "You just Received a notification!"
notification.soundName = UILocalNotificationDefaultSoundName
notification.userInfo = ["customField1": "w00t"]
notification.repeatInterval = NSCalendar.Unit.weekOfYear
UIApplication.shared.scheduleLocalNotification(notification)
Cela a provoqué une notification locale au feu à 11h00 tous les samedis.
Voici le code que je l'habitude d'essayer et obtenir le même effet dans l'IOS 10:
func scheduleNotification(at date: Date) {
let calendar = Calendar(identifier: .gregorian)
let components = calendar.dateComponents(in: .current, from: date)
let newComponents = DateComponents(calendar: calendar, timeZone: .current, month: components.month, day: components.day, hour: components.hour, minute: components.minute)
let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: true)
let content = UNMutableNotificationContent()
content.title = "Notification"
content.body = "You just Received a notification!"
content.sound = UNNotificationSound.default()
let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print("Error")
}
}
}
En viewDidLoad:
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) {(accepted, error) in
if !accepted {
print("Error")
}
}
Ce code fonctionne très bien si l'utilisateur sélectionne la date à l'aide d'un datePicker, mais je n'arrive pas à comprendre comment régler par programme la date de la notification à la même date que celle de la notification locale (2017-28-01 11:00), et aussi comment faire en sorte que la notification se déclenche chaque semaine à en même temps qu'il n'y a pas de propriété repeatInterval.
"comment faire en sorte que la notification se déclenche chaque semaine en même temps": http://stackoverflow.com/a/40856269/341994 – matt
Merci. Pourriez-vous expliquer comment vous ne pouvez spécifier que par exemple lundi à 11h00? Je ne comprends pas très bien comment vous ne feriez que nourrir cette information dans la variable. –
composants de date avec heure 11 et jour de semaine 2 – matt