Je suis en train de coder dans Swift 3 et j'essaye simplement d'envoyer une notification maintenant sans aucun retard ou intervalle. Cependant, la notification ne se déclenche jamais. Voici mon code ..Swift - Notification locale ne se déclenche pas
Le code ViewController
import UserNotifications
class HomeViewController: UIViewController{
var isGrantedNotificationAccess:Bool = false
override func viewDidLoad() {
super.viewDidLoad()
UNUserNotificationCenter.current().requestAuthorization(
options: [.alert,.sound,.badge],
completionHandler: { (granted,error) in
self.isGrantedNotificationAccess = granted
})
if isGrantedNotificationAccess{
triggerNotification()
}
}
//triggerNotification func goes here
}
fonction triggerNotification:
func triggerNotification(){
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey: "Notification Testing", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey: "This is a test", arguments: nil)
content.sound = UNNotificationSound.default()
content.badge = (UIApplication.shared.applicationIconBadgeNumber + 1) as NSNumber;
let trigger = UNTimeIntervalNotificationTrigger(
timeInterval: 1.0,
repeats: false)
let request = UNNotificationRequest.init(identifier: "testTriggerNotif", content: content, trigger: trigger)
let center = UNUserNotificationCenter.current()
center.add(request)
}
Qu'est-ce que je fais mal?
Vous pouvez consulter ce lien - http://stackoverflow.com/questions/37807302/add-local-notification-in-ios10-swift-3 – Nilesh
Vos notifications sont fonctionne bien. Pour tester, augmentez l'intervalle de temps à 5 et déplacez l'application en arrière-plan (la bannière de notification s'affichera comme prévu.Le problème est que vous ne traitez pas ce que l'application doit faire, alors la notification utilisateur arrive et l'application est à l'avant-plan) –
de [Affichage d'une bannière de notification iOS stock lorsque votre application est ouverte et au premier plan?] (https://stackoverflow.com/questions/30852870/displaying-a-stock-ios-notification-banner-when-your-app- is-open-and-in-the-fore) – Bill