项目作者: Sweefties

项目描述 :
iOS 10~ Experiments - New API Components - New User Notifications with 3DTouch and iPhone 7.
高级语言: Swift
项目地址: git://github.com/Sweefties/iOS10-NewAPI-UserNotifications-Example.git




iOS 10 - New API - User Notifications - 3DTouch Example

iOS 10~ Experiments - New API Components - New User Notifications with 3DTouch and iPhone 7.

Example

Requirements

  • = XCode 8.0

  • = Swift 3.

  • = iOS 10.0.

  • = 3D Touch Devices or iOS 10 supported.

Tested on iPhone SE, iPhone 6, iPhone 7 iOS 10.0 Simulators and physicals iPhone 7, iPhone 6, iPhone SE.

Important

This is a Xcode 8+ / Swift 3+ project.
To run on physicals devices, change the team provisioning profile.

References

Read : UNUserNotificationCenter

API Reference : UserNotifications

Usage

To run the example project, download or clone the repo.

Example Code!

Import Framework :

  1. import UserNotifications

Register UNUserNotificationCenter

  1. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
  2. let center = UNUserNotificationCenter.current()
  3. // define actions
  4. let ac1 = setAction(id: UNIdentifiers.reply, title: "Reply")
  5. let ac2 = setAction(id: UNIdentifiers.share, title: "Share")
  6. let ac3 = setAction(id: UNIdentifiers.follow, title: "Follow")
  7. let ac4 = setAction(id: UNIdentifiers.destructive, title: "Cancel", options: .destructive)
  8. let ac5 = setAction(id: UNIdentifiers.direction, title: "Get Direction")
  9. // define categories
  10. let cat1 = setCategory(identifier: UNIdentifiers.category, action: [ac1, ac2, ac3, ac4], intentIdentifiers: [])
  11. let cat2 = setCategory(identifier: UNIdentifiers.customContent, action: [ac5, ac4], intentIdentifiers: [])
  12. let cat3 = setCategory(identifier: UNIdentifiers.image, action: [ac2], intentIdentifiers: [], options: .allowInCarPlay)
  13. // Registers your app’s notification types and the custom actions that they support.
  14. center.setNotificationCategories([cat1, cat2, cat3])
  15. // Requests authorization to interact with the user when local and remote notifications arrive.
  16. center.requestAuthorization(options: [.badge, .alert , .sound]) { (success, error) in
  17. print(error?.localizedDescription)
  18. }
  19. return true
  20. }

Set Actions and Categories :

  1. /// Set User Notifications Action.
  2. ///
  3. /// - Parameter id: `String` identifier string value
  4. /// - Parameter title: `String` title string value
  5. /// - Parameter options: `UNNotificationActionOptions` bevavior to the action as `OptionSet`
  6. ///
  7. /// - Returns: `UNNotificationAction`
  8. ///
  9. private func setAction(id: String, title: String, options: UNNotificationActionOptions = []) -> UNNotificationAction {
  10. let action = UNNotificationAction(identifier: id, title: title, options: options)
  11. return action
  12. }
  13. /// Set User Notifications Category.
  14. ///
  15. /// - Parameter identifier: `String`
  16. /// - Parameter action: `[UNNotificationAction]` ask to perform in response to
  17. /// a delivered notification
  18. /// - Parameter intentIdentifiers: `[String]` array of `String`
  19. /// - Parameter options: `[UNNotificationCategoryOptions]` handle notifications,
  20. /// associated with this category `OptionSet`
  21. ///
  22. /// - Returns: `UNNotificationCategory`
  23. ///
  24. private func setCategory(identifier: String, action:[UNNotificationAction], intentIdentifiers: [String], options: UNNotificationCategoryOptions = []) -> UNNotificationCategory {
  25. let category = UNNotificationCategory(identifier: identifier, actions: action, intentIdentifiers: intentIdentifiers, options: options)
  26. return category
  27. }

Add request to current UNUserNotificationCenter

  1. let request = UNNotificationRequest(identifier: UNIdentifiers.request, content: content[type.rawValue], trigger: nil)
  2. UNUserNotificationCenter.current().add(request) { error in
  3. UNUserNotificationCenter.current().delegate = self
  4. if (error != nil){
  5. //handle here
  6. }
  7. }

Et Voilà!

  • Build and Run!
  • By pressing lightly (Peek) and pressing a little more firmly to actually open content (Pop)
  • Optimized for Devices : iPhone 6s and others 3D Touch devices!