2017-09-04 55 views
0

我知道如何在Swift 3中創建本地通知(我是這部分中的新功能),但是,我想創建類似於下圖的東西。所有tutorials in the web太舊了,我不該怎麼辦。用新UI在swift 3中的本地通知

Local notification

正如你可以擴展通知之前看到的,有2個按鈕。延長後還有2個紅色和藍色的按鈕。

更新

感謝Joern

的滑動手勢只顯示明確。是否有顯示兩個明確視圖

enter image description here

回答

1

紅色和藍色的按鈕任何設置僅在之前到iOS 10. IOS版本與iOS 10通知設計改變可用。滑動手勢用於標準動作清除查看。自定義操作貪睡確認,當你用力觸摸通知或拉下來(用於設備,而無需觸摸力)將被顯示。如果您正在使用設備強行觸摸查看按鈕可能不會顯示。

的按鈕看起來現在不同了:

enter image description here

所以,這裏是你如何與斯威夫特3/4的實現本地聲明:

對於iOS之前的版本iOS的10:

如果您在iOS10之前支持iOS版本,則必須使用舊版本(不推薦使用iOS 10)UILocalNotification

class AppDelegate: UIResponder, UIApplicationDelegate { 

    var window: UIWindow? 

    func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool { 
     registerLocalNotification() 
     return true 
    } 

    func applicationWillResignActive(_ application: UIApplication) { 
     scheduleLocalNotification() 
    } 

    func scheduleLocalNotification() { 
     let localNotification = UILocalNotification() 
     localNotification.alertTitle = "Buy milk" 
     localNotification.alertBody = "Remember to buy milk from store" 
     localNotification.fireDate = Date(timeIntervalSinceNow: 3) 
     localNotification.soundName = UILocalNotificationDefaultSoundName 
     localNotification.category = "reminderCategory" // Category to use the specified actions 
     UIApplication.shared.scheduleLocalNotification(localNotification) // Scheduling the notification. 
    } 

    func registerLocalNotification() { 
     let reminderActionConfirm = UIMutableUserNotificationAction() 
     reminderActionConfirm.identifier = "Confirm" 
     reminderActionConfirm.title = "Confirm" 
     reminderActionConfirm.activationMode = .background 
     reminderActionConfirm.isDestructive = false 
     reminderActionConfirm.isAuthenticationRequired = false 

     let reminderActionSnooze = UIMutableUserNotificationAction() 
     reminderActionSnooze.identifier = "Snooze" 
     reminderActionSnooze.title = "Snooze" 
     reminderActionSnooze.activationMode = .background 
     reminderActionSnooze.isDestructive = true 
     reminderActionSnooze.isAuthenticationRequired = false 

     // Create a category with the above actions 
     let shoppingListReminderCategory = UIMutableUserNotificationCategory() 
     shoppingListReminderCategory.identifier = "reminderCategory" 
     shoppingListReminderCategory.setActions([reminderActionConfirm, reminderActionSnooze], for: .default) 
     shoppingListReminderCategory.setActions([reminderActionConfirm, reminderActionSnooze], for: .minimal) 

     // Register for notification: This will prompt for the user's consent to receive notifications from this app. 
     let notificationSettings = UIUserNotificationSettings(types: [.alert, .sound, .badge], categories: [shoppingListReminderCategory]) 

     UIApplication.shared.registerUserNotificationSettings(notificationSettings) 
    } 
} 

這將註冊本地通知並觸發用戶關閉後3秒它的應用程序(用於測試目的)

對於iOS 10及更高版本:

如果您將應用iOS版10,你可以使用新的UserNotifications框架:

import UIKit 
import UserNotifications 

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

    var window: UIWindow? 

    func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool { 
     registerUserNotifications() 
     return true 
    } 

    func applicationWillResignActive(_ application: UIApplication) { 
     scheduleLocalNotification() 
    } 

    func registerUserNotifications() { 
     UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in 
      guard granted else { return } 
      self.setNotificationCategories() 
     } 
    } 

    func setNotificationCategories() { 
     // Create the custom actions 
     let snoozeAction = UNNotificationAction(identifier: "SNOOZE_ACTION", 
               title: "Snooze", 
               options: .destructive) 
     let confirmAction = UNNotificationAction(identifier: "CONFIRM_ACTION", 
               title: "Confirm", 
               options: []) 

     let expiredCategory = UNNotificationCategory(identifier: "TIMER_EXPIRED", 
                actions: [snoozeAction, confirmAction], 
                intentIdentifiers: [], 
                options: UNNotificationCategoryOptions(rawValue: 0)) 

     // Register the category. 
     let center = UNUserNotificationCenter.current() 
     center.setNotificationCategories([expiredCategory]) 
    } 

    func scheduleLocalNotification() { 
     let content = UNMutableNotificationContent() 
     content.title = "Buy milk!" 
     content.body = "Remember to buy milk from store!" 
     content.categoryIdentifier = "TIMER_EXPIRED" 

     let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 3, repeats: false) 

     // Create the request object. 
     let request = UNNotificationRequest(identifier: "Milk reminder", content: content, trigger: trigger) 

     // Schedule the request. 
     let center = UNUserNotificationCenter.current() 
     center.add(request) { (error : Error?) in 
      if let theError = error { 
       print(theError.localizedDescription) 
      } 
     } 
    } 
} 

您可以檢查出使用了UserNotifications FRA演示應用程序mework here

+0

Merci,很清楚!但是,滑動手勢只顯示**清除**請參閱我更新的問題。是否有顯示**清除**和**視圖**的任何設置** –

+1

您是否在具有強制觸摸的設備上運行應用程序?在這種情況下,有時不顯示* view *按鈕。我添加了一個使用UserNotifications框架的演示應用程序github:https://github.com/pixeldock/LocalNotificationDemo – joern

+0

強力觸摸是平等的3D觸摸?我在iphone 7 plus上運行(在模擬器上)。非常感謝github項目。 –