CoacoaheadsSKG
Dimitri James tsiflitzis
iOS 10 push notifications have new functionality. We can view photos, videos or gifs within the notification.
Tonight we'll go through some code snippets, examples and good practices so you could use them in your own apps.
Server Side App
Opaque APNS
Devices
{
aps: {
alert: {
“body”: “CocoaheadsSKG #10”,
“title”: “Hello World!”
}
mutable-content: 1
category: "rich-apns"
}
data: {
attachment-url: "https://i.imgur.com/t4WGJQx.jpg"
}
}The mutable-content fields tells iOS that the notification has content that can be changed by a service extension before delivery, and the attachment-url field contains the URL for the content to be downloaded.
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
// actions based on whether notifications were authorized or not
}
application.registerForRemoteNotifications()func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
print(deviceTokenString)
/* send the device token to your server */
}func application(_ application: UIApplication,
didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("i am not available in simulator \(error)")
}UNUserNotificationCenter.current().getNotificationSettings(){ (setttings) in
switch setttings.soundSetting{
case .enabled:
print("enabled sound setting")
case .disabled:
print("setting has been disabled")
case .notSupported:
print("something vital went wrong here")
}
}In the following code, we are walking through the apns's payload for the dictionary named data that contains the key attachment-url. If it exists, we download the image from the URL which is the key's value.
The URLSession downloads the media to temporary storage and appends a .tmp file extension, which needs to be removed so that the application can infer the file type and display it. This is done by moving the temporary file to the device’s File Manager.
override func didReceive(_ request: UNNotificationRequest,
withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
self.bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
/* Get the custom data from the notification payload */
if let notificationData = request.content.userInfo["data"] as? [String: String] {
/* Grab the attachment */
if let urlString = notificationData["attachment-url"], let fileUrl = URL(string: urlString) {
/* Download the attachment */
URLSession.shared.downloadTask(with: fileUrl) { (location, response, error) in
if let location = location {
/* Move temporary file to remove .tmp extension */
let tmpDirectory = NSTemporaryDirectory()
let tmpFile = "file://".appending(tmpDirectory).appending(fileUrl.lastPathComponent)
let tmpUrl = URL(string: tmpFile)!
try! FileManager.default.moveItem(at: location, to: tmpUrl)
/* Add the attachment to the notification content */
if let attachment = try? UNNotificationAttachment(identifier: "", url: tmpUrl) {
self.bestAttemptContent?.attachments = [attachment]
}
}
/* Serve the notification content */
self.contentHandler!(self.bestAttemptContent!)
}.resume()
}
}
Ευχαριστούμε