- Notifications
You must be signed in to change notification settings - Fork3
Sweefties/iOS10-NewAPI-UserNotifications-Example
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
iOS 10~ Experiments - New API Components - New User Notifications with 3DTouch and iPhone 7.
= 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.
This is a Xcode 8+ / Swift 3+ project.To run on physicals devices, change the team provisioning profile.
Read :UNUserNotificationCenter
API Reference :UserNotifications
To run the example project, download or clone the repo.
Import Framework :
import UserNotifications
RegisterUNUserNotificationCenter
func application(_ application:UIApplication, didFinishLaunchingWithOptions launchOptions:[UIApplicationLaunchOptionsKey:Any]?)->Bool{letcenter=UNUserNotificationCenter.current() // define actionsletac1=setAction(id:UNIdentifiers.reply, title:"Reply")letac2=setAction(id:UNIdentifiers.share, title:"Share")letac3=setAction(id:UNIdentifiers.follow, title:"Follow")letac4=setAction(id:UNIdentifiers.destructive, title:"Cancel", options:.destructive)letac5=setAction(id:UNIdentifiers.direction, title:"Get Direction") // define categoriesletcat1=setCategory(identifier:UNIdentifiers.category, action:[ac1, ac2, ac3, ac4], intentIdentifiers:[])letcat2=setCategory(identifier:UNIdentifiers.customContent, action:[ac5, ac4], intentIdentifiers:[])letcat3=setCategory(identifier:UNIdentifiers.image, action:[ac2], intentIdentifiers:[], options:.allowInCarPlay) // Registers your app’s notification types and the custom actions that they support. center.setNotificationCategories([cat1, cat2, cat3]) // Requests authorization to interact with the user when local and remote notifications arrive. center.requestAuthorization(options:[.badge,.alert,.sound]){(success, error)inprint(error?.localizedDescription)}returntrue}
Set Actions and Categories :
/// Set User Notifications Action.////// - Parameter id: `String` identifier string value/// - Parameter title: `String` title string value/// - Parameter options: `UNNotificationActionOptions` bevavior to the action as `OptionSet`////// - Returns: `UNNotificationAction`///privatefunc setAction(id:String, title:String, options:UNNotificationActionOptions=[])->UNNotificationAction{letaction=UNNotificationAction(identifier: id, title: title, options: options)return action}/// Set User Notifications Category.////// - Parameter identifier: `String`/// - Parameter action: `[UNNotificationAction]` ask to perform in response to/// a delivered notification/// - Parameter intentIdentifiers: `[String]` array of `String`/// - Parameter options: `[UNNotificationCategoryOptions]` handle notifications,/// associated with this category `OptionSet`////// - Returns: `UNNotificationCategory`///privatefunc setCategory(identifier:String, action:[UNNotificationAction], intentIdentifiers:[String], options:UNNotificationCategoryOptions=[])->UNNotificationCategory{letcategory=UNNotificationCategory(identifier: identifier, actions: action, intentIdentifiers: intentIdentifiers, options: options)return category}
Add request to currentUNUserNotificationCenter
letrequest=UNNotificationRequest(identifier:UNIdentifiers.request, content:content[type.rawValue], trigger:nil)UNUserNotificationCenter.current().add(request){ errorinUNUserNotificationCenter.current().delegate=selfif(error!=nil){ //handle here}}
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!
About
iOS 10~ Experiments - New API Components - New User Notifications with 3DTouch and iPhone 7.
Topics
Resources
Uh oh!
There was an error while loading.Please reload this page.