intro to notifications in iOS 10 Paola Mata iOS Developer at BuzzFeed Co-founder, NYC Tech Latinas
overview
user notifications visual alerts - banner, lock screen, notification center sounds/vibration app icon badges By user notification, I am also referring to the sounds and badges as well.
types of notifications local - sent by apps on device remote - push server-side application component Apple Developer membership For remote - server side application component (I used firebase) Apple membership required to support
why user notifications? increase user engagement perform activities in the background (silent) more powerful than ever! To get your users attention when they’re not using your app and redirect them to your app Background tasks provide a better user experience BuzzFeed News example: new alerts and background tasks, downloading cache html for offline use.
notifications setup So now, we’re psyched about notifications, right? How do we get them into our applications.
steps registration create content triggers schedule Ranging in order of complexity Content for remote notifications is provided as JSON dictionary
registration obtain user authorization for both local & remote specify authorization options (alert, badge, sound) configurable in settings remote notifications also require token registration* *Will explain in more detail in a bit
request authorization Completion handler if user accepted, register for remote
remote notification registration certificate signing request development APNs create App ID (explicit) configure App ID for push notifications register device(s) create provisioning profile configure project (Xcode) register for push notifications Steps 1 - 5 happen in your apple developer account Xcode 8 can generate provisioning profiles for you, but don’t rely on Xcode too heavily. https://www.appcoda.com/push-notification-ios/
token registration obtained from APNS UIApplication registration sent to your server-side application included with notification payload Earlier mentioned token registration required for remote notifications Much of this is handled by the app delegate, which conforms to UIApplicaionDelegate
content title subtitle body optional media attachments
create content
triggers time interval calendar location push (remote) Strong Lifts - timer after each set, notifications every 90, 3 or 5 mins Headspace - daily calendar reminders to meditate Home Depot in-store notifications BuzzFeed News! - push news alerts
create a trigger Other trigger classes with respective initializers: UNLocationNotificationTrigger UNCalendarNotificationTrigger
schedule local notifications create request with identifier, content, and trigger add request on UNUserNotificationCenter
create and add request request Identifier will be important later, when we go into updating/replacing notifications.
scheduling remote notifications server side application sends notification payload to APNs notification payload forwarded to device
push notification payload JSON dictionary Other APNS include: content-available and category (custom actions)
what’s new in iOS 10?
rich notifications Local image add the image as an attachment by providing identifier and url for image We can also add gifs and video
3D touch custom actions Added at time of registration: custom - response handed by delegate method dismiss - requires opt-in via category options* Default notification action opens up the app Custom actions available since iOS 8 Use 3D Touch in iOS 10
adding custom actions UNNotitification actions require identifier Dismiss action need to be added via category options
supporting custom actions For remote, the identifier would be sent with the notification payload
handling custom actions
notification management remove delivered notifications and/or pending notification requests handle updates to existing and pending notifications create a new notification add request using request identifier of notification to update
service extension Service Extensions are a non-User Interface extension (available in iOS 10) that run in the background with the main purpose of augmenting or replacing the visible content of a remote notification before it is presented to the user.
service extension, cont’d potential uses - end to end encryption, add media attachments short execution time -> fallback method -> shows original notification to launch extension, notification payload should include “mutable-content” key Service Extensions are meant to run quickly and are only given a short amount of time to execute by the system. In the event that the Service Extension fails to complete its task in the allotted amount of time, a fallback method will be called. If the fallback fails, the original Notification Content will be displayed to the user.
notification content extension The Notification Content Extension allows the developer to add their own views to the Notification UI and draw out any content they want. It can also handle custom actions.
notification content extension, cont’d multiple content extensions can be used includes: view controller, storyboard, Info.plist creating custom UI default notification content can be hidden interactive UI elements not supported can update UI on custom action can access built-in text input action Info.plist - extension configuration button/text fields not supported Try notifications for messages and calendar notifications for examples Multiple extensions - respond to category identifier
resources Apple Documentation on Notifications https://developer.apple.com/notifications/ WWDC 2016 Sessions 707 & 708 on User Notifications https://developer.apple.com/videos/play/wwdc2016/707/ https://developer.apple.com/videos/play/wwdc2016/708/ try! Swift NYC talk by Ellen Shapiro (great sample code) https://realm.io/news/tryswift-ellen-shapiro-pushing-envelope-ios-10-notifications/
Thank you!
www.paolamata.com www.madebylatinas.com Twitter: @PaolaNotPaolo