2017-09-04 57 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项目。 –