1

Je veux tirer la notification locale chaque année à des dates spécifiques, mais toutes les notifications ont un titre et un corps différents, voici mon code que j'ai écrit et qui fonctionne parfaitement, mais pour une notification seulement et la seconde une partie est après avoir obtenu notification mon icône de l'application ont 1, signifie une notification là comment supprimer ce? ..Notification locale à différentes dates Swift 3

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
     // Override point for customization after application launch. 

     UIApplication.shared.statusBarStyle = .default 
      UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]){ (allowed, error) in 
       UNUserNotificationCenter.current().delegate = self 
     scheduleNotification() 
     return true 
    } 



func scheduleNotification() { 

    var date = DateComponents() 
    date.year = 2017 
    date.month = 6 
    date.day = 12 
    date.hour = 22 
    date.minute = 39 

      let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: false) 
      let content = UNMutableNotificationContent() 
      content.title = "Schedule Notification" 
      content.body = "Today is my Birthday" 
      content.sound = UNNotificationSound.default() 
      content.badge = 1 
      let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger) 
      UNUserNotificationCenter.current().removeAllPendingNotificationRequests() 
      UNUserNotificationCenter.current().add(request) {(error) in 
       if let error = error { 
        print("error: \(error)") 
       } 
    } 

apprécier et merci à l'avance

+0

Vous devez planifier plusieurs notifications; un pour chaque date requise. Si vous ne voulez pas badger l'icône de l'application, ne définissez pas la propriété 'badge' – Paulw11

+0

merci @ Paulw11 donc votre réponse rapide .. – King

Répondre

0

pour les notifications répétées, vous devez planifier plusieurs notifications. Tu peux faire comme ça.

func scheduleNotification(_ title:String, body:String, dateComponents :DateComponents) {   
    let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false) 
    let content = UNMutableNotificationContent() 
    content.title = title 
    content.body = body 
    content.sound = UNNotificationSound.default() 
    content.badge = 1 
    let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger) 
    UNUserNotificationCenter.current().add(request) {(error) in 
     if let error = error { 
      print("error: \(error)") 
     } 
    } 
} 

Et utilisez la fonction ci-dessus pour planifier plusieurs notifications comme celle-ci.

var date = DateComponents() 
date.year = 2017 
date.month = 6 
date.day = 12 
date.hour = 22 
date.minute = 39  

UNUserNotificationCenter.current().removeAllPendingNotificationRequests() 
scheduleNotification("Schedule Notification", body: "Today is my Birthday", dateComponents: date) 
scheduleNotification("Schedule Notification 2", body: "Today is my Birthday 2", dateComponents: date2) 
scheduleNotification("Schedule Notification 3", body: "Today is my Birthday 3", dateComponents: date3) 

Deuxième partie de votre question comment supprimer l'icône de badge. Ajoutez le code suivant quelque part dans votre applicationDidFinishLaunching ou applicationDidBecomeActive.

UIApplication.shared.applicationIconBadgeNumber = 0; 
+0

Merci beaucoup @Bilal pour votre réponse rapide et la solution .... – King

+0

son pas fonctionnant, je crée la nouvelle fonction appelée notificationLauncher() et dans cette fonction je crée trois différents composants de date et puis j'ai appelé la fonction scheduleNotification avec des paramètres de titre, corps et composants, puis j'ai appelé cette fonction dans l'application déléguéDidFinisgLaunchingWithOptions :( – King

+0

fonctionne pas multiple même quand j'essaye plusieurs fonctions pour appeler la notification multiple je ne reçois même pas une notification :( – King