iOS 10 Notifications (What is new) - Part 1
I must admit that I have a huge crush on the notifications API regardless of the platform (oh yeah I really like the notifications API in Android too) and I have never shut up about it since iOS 7 now in iOS 10 things get even better. There is a new framework altogether to handle notifications both remote and local, the UserNotifications framework. Previously all the notifications API were bundled within the UIApplication class of UIKit and Apple has realized that notifications play a crucial part in the way users interact with the platforms which is evident in all the WWDC sessions and no wonder they plugged a lot of things out. Not just plugging out has happened but there is a lot of new features too.
If you have been using the UILocalNotification class as you have been doing since ages we need to move on to the new UserNotifications framework. Yes here comes the truth UILocalNotification is deprecate since iOS 10.
If you go see the UILocalNotification class you would find this,
@available(iOS, introduced: 4.0, deprecated: 10.0, message: “Use UserNotifications Framework’s UNNotificationRequest”) public class UILocalNotification : NSObject, NSCopying, NSCoding
Even the way we were receiving notifications are changed, we need to move on to the new framework.
@available(iOS, introduced: 4.0, deprecated: 10.0, message: “Use UserNotifications Framework’s -[UNUserNotificationCenterDelegate willPresentNotification:withCompletionHandler:] or -[UNUserNotificationCenterDelegate didReceiveNotificationResponse:withCompletionHandler:]”)
optional public func application(_ application: UIApplication, didReceive notification: UILocalNotification)
After seeing all this it was pretty much clear that the whole of UILocalNotification is going to be flushed away from iOS 10.
So after going through all the sessions and with my experience with notifications in iOS I am pretty sure that I need to make a lot of changes in the existing code and was pretty surprised of the many changes done here.
So coming to the new features that have been introduced, apps can now send notification with enriched content and users can see the content without even opening the app. The most important types of attachments that were talked about are,
Images
Videos
Audio
Last but not least GIFs!
The other new feature worth mentioning is the custom UI that apps can now show during notification presentation yes you can modify the UI format in which notifications are presented in your app. Hmmmmm I guess that is a pretty big topic and lets just cover that in another post, please!!
Ok then I just started tinkering with local notifications using the UNUserNotificationCenter class in the UserNotifications framework and tried to send some notifications with attachments,
So lets get our hands dirty with some code to send a simple notification with the UserNotification APIs,
///Creating a UNMutableNotificationContent object that will specify the notification content details
let content = UNMutableNotificationContent()
///Setting the title
content.title = "The Code Ninja says"
///Setting the body
content.body = "The new notifications api in iOS 10 is just awesome"
///Setting the subtitle property, this is new for iOS 10 alone.
content.subtitle = "Also you can add a subtitle with it"
///Setting the trigger time
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
///Setting the identifier to identify different types of notifications
let requestIdentifier = "TheCodeNinja_Identifier"
///Creating a UNNotificationRequest object with the identifier, content and the trigger object
let request = UNNotificationRequest(identifier: requestIdentifier, content: content,trigger: trigger)
/// last step, Adding the request to UNUserNotificationCenter.current()'s queue so that it presents the notification when the trigger happens.
UNUserNotificationCenter.current().add(request) { (error) in}
I ran the app, BOOM I got a notification with an image like below,
Now let us try a bit more interesting, we’ll try to add an image attachment to the notification, to do that add the below code before sending the notification
/// Creating a image URL from bundle
let imgURL = Bundle.main().urlForResource("notification", withExtension: "jpg")
/// Creating an attachment object
let notificationImageAttachment = try UNNotificationAttachment(identifier: "jpg_identifier", url:imgURL! , options: nil)
content.attachments = [notificationAttachment!]
Hit Run, you will get something like below…
Isn’t that just great, but I was running in the simulator and could not interact much with the attachment but if you have a device with 3D touch you could try out how will you can interact with the notification attachment.
I tried different types attachments like audio, video and GIF’s but could not test all on the simulator and the framework automatically tries to crack the file type for you, if you are trying to do anything apart from common file formats then you can specify the file type like below mentioning the hint key to the API,
let notificationOptions = [UNNotificationAttachmentOptionsTypeHintKey:kUTTypeMP3]
/// Creating an attachment object
let notificationImageAttachment = try UNNotificationAttachment(identifier: "audio_identifier", url:imgURL! , options: notificationOptions)
All the code I have been doing so far is here. See you all for now, will post more soon on my discoveries on the new notifications API.
UPDATE 1
The code has been upgraded to the changes in Xcode 8 beta 2.
Screenshots of the various attachments I tried, so to view notification attachments the user needs to swipe down the notification banner and he can see the notification attachment.
Image Attachment
GIF (the GIF starts to play when the user swipes down)
Audio Attachment (the user is given a play/pause button and a seek bar to control the audio)
Video attachment (the user is given a play/pause button and a seek bar to control the video)
See you soon guys!!
6 notes
·
View notes