2017-03-11 3 views
1

Ich möchte die benutzerdefinierte Benachrichtigungsschaltfläche für Benachrichtigungen anzeigen, wenn meine App eine Benachrichtigung erhält. Aber nichts ist passiert! Kann mir jemand helfen? Danke.ios10 zeigt keine Schaltfläche für benutzerdefinierte Benachrichtigungen an.

Das ist mein AppDelegate.swift Code:

class AppDelegate: UIResponder, UIApplicationDelegate { 

    var window: UIWindow? 

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 

     // Register ourselves as a delegate so we can be notified when actions pressed. 
     UNUserNotificationCenter.current().delegate = self 

     // Request permission. 
     UNUserNotificationCenter.current().requestAuthorization(options: [.alert]) { 
      granted, error in 
      if granted { 
       print("Approval granted to send notifications") 
      } 
     } 

     self.addCategory() 

     return true 
    } 

    func addCategory() { 
     // Add action. 
     let stopAction = UNNotificationAction(identifier: "stop", title: "Stop", options: []) 
     let snoozeAction = UNNotificationAction(identifier: "snooze", title: "Snooze", options: []) 

     // Create category. 
     let category = UNNotificationCategory(identifier: "reminder", actions: [stopAction, snoozeAction], intentIdentifiers: [], options: []) 

     UNUserNotificationCenter.current().setNotificationCategories([category]) 
    } 
} 

UNUserNotificationCenterDelegate

extension AppDelegate: UNUserNotificationCenterDelegate { 

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 
     completionHandler() 
    } 

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 
     print("Called when a notification is delivered to a foreground app.") 
     NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationPresentAtForeground"), object: nil) 
     completionHandler([.alert]) 
    } 
} 

Antwort

3

ich die answer.I fand nicht meine Benachrichtigung Inhaltskategorie-Kennung gesetzt. Folgendes funktionierte für mich:

content.categoryIdentifier = "reminder"