2016-09-28 117 views
0

我正在嘗試將我的應用程序更新到Swift 3.我正在取得進展,但我無法弄清楚如何註冊本地通知設置。這是我的代碼駐留在AppDelegate的文件:如何在Swift 3中創建/註冊本地通知設置?

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

    application.applicationIconBadgeNumber = 0 

    if application.currentUserNotificationSettings?.categories?.count == 0 { 
     // Updated Code 
     let types: UNAuthorizationOptions = [.alert, .badge, .sound] 

     let completeAction = UNNotificationAction(identifier: "complete", title: "Complete", options: []) 

     let ignoreAction = UNNotificationAction(identifier: "ignore", title: "Ignore", options: []) 

     let taskCategory = UNNotificationCategory(identifier: "taskCategory", actions: [completeAction, ignoreAction], intentIdentifiers: [], options: []) 

     // Old Code (stripped for simplicity) 
     let settings = UIUserNotificationSettings(types: _, categories: _) 
     application.registerUserNotificationSettings(settings) 
    } 
    print(application.currentUserNotificationSettings?.categories?.count) 
    return true 
} 
+0

這是推送通知。它不回答我的問題在本地通知。 – Greg

回答

0

嘗試這樣

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool 
{ 
    // Override point for customization after application launch. 
    let center = UNUserNotificationCenter.current() 
    center.requestAuthorization([.alert, .sound]) { (granted, error) in 
    } 
    return true 
} 
+0

謝謝!我其實剛剛遇到了這個以及Apple的其他文檔。我目前正在拼湊我所需要的東西。這並不像我希望的那樣簡單。它需要不同於我以前實施的邏輯。 – Greg