Contact Us

Click To Download Full Source Code

  • iOS: 10
  • Language: Swift
  • Xcode Version: 8
  • Mac OS: 10.12

Step 1: Create Simple Xcode Project

For this demo, first create a Xcode project by selecting ‘Single View Application’ type.

Step 2: Enable Remote Notifications

  • Now open your application target. Choose capabilities and ensure that ‘Push Notifications’ is enabled. For that open this screen first.
    notifications
  • Now we need to select ‘Remote notifications’ under background modes. For that open this screen first.
    notifications

Step 3: Registering your applications for remote notifications

  • Now open AppDelegate.swift. For that open this screen first
    notifications
  • Import ‘UserNotifications’ and write below code in ‘didFinishLaunchingWithOptions’ method
  • Now you are done with registering remote notifications.
    notifications

Step 4: Add Notification Service Extension

  • Now we need to add ‘Notification Service Extension’ in application. For that open this screen first
    notifications
  • Select ‘Notification Service Extension’ in your application and click on ‘Next’ button
    notifications
  • Enter name of the extension. Here we have entered ‘Service Extension’. Then click on ‘Finish’ button.
    notifications
  • Once you click on ‘Finish’ button, user will be able to see below screen.
    notifications
  • In this screen, Xcode is asking you to active scheme for this new extension. So you can use this scheme for building and debugging. Schemes can be chosen in the toolbar or Product menu. Please activate scheme for ‘Service Extension’.
  • Now you are done with adding notification service extension.

Step 5: Understanding what files you are getting after adding Notification Service Extension

In this extension, you will be able to find that there are total two files:

  • Info.plist
  • NotificationService.swift

Info.plist:

  • In plist file you have to mention what type of operation you want to perform by using Notification Service Extension. notifications

NotificationService.swift
In swift file you have to implement all operations which you like to handle as per supported type for Notification Service Extension.

  • You will see a couple of delegate method – one for receiving a notification request and one for handling the expiration of the service extension.
  • Now focussing on this ‘didReceiveRequestWithContentHandler’ method.
    notifications
  • Write below code in ‘didReceiveRequestWithContentHandler’ method.
    notifications

  • It may seem like a lot is happening here, but it’s really straightforward. Initially we are exploring the notification’s payload for the dictionary named ‘data’ that contains the key ‘attachment-url’. If this key exists, we download the media at the URL specified in the key’s value.
  • The URLSession downloads the media to temporary storage and appends a .tmp file extension which we need to remove so that the application can infer the file type and display it. So to do this, we need to move the file to the application’s local File Manager and that’s it – our media is ready to be attached to the notification content, and served to the user.
  • Now focussing on this ‘serviceExtensionTimeWillExpire’ method.
  • This Called just before the extension will be terminated by the system
  • Use this as an opportunity to deliver your ‘best attempt’ at modified content; otherwise the original push payload will be used.
  • Now you are done with notification service rich notification.

Step 6: Registering device token for remote notifications

  • Now open AppDelegate.swift. For that open this screen first notifications
  • Create Data extension for getting the data to hexString.
    notifications

  • Write code in ‘didRegisterForRemoteNotificationsWithDeviceToken’ method for getting device token
    notifications

  • Use device token for sending push notification.

Step 7: Run on your iOS Device

  • Now connect your device with your system.
  • Select ’NotificationDemo’ from the scheme and run. Once it is run on your device then stop it.
    notifications

Step 8: How to test

  • We mentioned that only certain notifications were intercepted by the Service Extension. So, how does iOS know which notifications to intercept? Well, you just add a mutable-content tag to your payload and set it to 1.
  • The final thing you need to do is to add the media attachment URL to the payload in its own dictionary that we’ve named ‘data’, but you can name it anything. Simply add a key-value pair to the dictionary and as long as the key you’ve used in your Service Extension is the same as the key here, you’re done!
  • The following is a Payload example,

You can now send images, videos, audio, and GIFs in your iOS 10 Push Notifications! Simply change the URL to point to another media file and see the results for yourself.

  1. Text only
    notifications
  2. Image attachment
    notifications
  3. GIF attachment
    notifications
    notifications

Click To Download Full Source Code

Sandeep Joshi

Sandeep Joshi | iOS Developer

I’m Sandeep Joshi - an iOS developer at Yudiz Solutions Pvt. Ltd. - a leading iPhone App Development company. I am ambitious and intensely interested in what I do. You'll quickly find that I do not merely develop software - I develop highly maintainable, elegant code that will save you enormous sums in the long run.

Leave a Reply

Top
Yudiz Solutions Pvt. Ltd.
Career
Are you looking for a company where you are appreciated for your work?
Get a Free Quote
Tell Us Your Project Requirements

Career
Are you looking for a company where you are appreciated for your work?
Get a Free Quote
Tell Us Your Project Requirements