A simple Notification Service Extension class to add media attachments to iOS 10 rich push notifications
Switch branches/tags
Nothing to show
Clone or download
Peter Wilkniss
Peter Wilkniss update to remote pods
Latest commit 5bdbfc1 Sep 20, 2017
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
CTNotificationService.xcodeproj start Feb 28, 2017
CTNotificationService README Mar 1, 2017
Example update to remote pods Sep 19, 2017
ExampleSwift update to remote pods Sep 19, 2017
images README Mar 1, 2017
.gitignore prepare 0.1.1 Sep 19, 2017
CHANGELOG.md prepare 0.1.1 Sep 19, 2017
CTNotificationService.podspec prepare 0.1.1 Sep 19, 2017
LICENSE.txt start Feb 28, 2017
README.md tweak Aug 30, 2017

README.md

CTNotificationService

Version License Platform

A simple Notification Service Extension class to add media attachments to iOS 10 rich push notifications

Starting with iOS 10 you can add media attachments (image, gif, video, audio) to iOS push notifications. This library provides a simple drop-in class to accomplish that.

Rich push notifications are enabled in iOS 10 via a Notification Service Extension, a separate and distinct binary embedded in your app bundle.

Configure your app for Push and add a Notification Service Extension target

Enable push notifications in your main app.

Create a Notification Service Extension in your project. To do that in your Xcode project, select File -> New -> Target and choose the Notification Service Extension template.

notification service extension

Install CTNotificationService in your Notification Service Extension via CocoaPods

Your Podfile should look something like this:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'

use_frameworks!

target 'YOUR_NOTIFICATION_SERVICE_TARGET_NAME' do  
    pod 'CTNotificationService'  
end     

Then run pod install.

See example Podfile here.

Configure your Notification Service Extension to use the CTNotificationServiceExtension class

By default CTNotificatonServiceExtension will look for the push payload key ct_mediaUrl with a value representing the url to your media file and the key ct_mediaType with a value of the type of media (image, video, audio or gif).

If you are happy with the default key names, you can simply insert CTNotificationServiceExtension in place of your extension class name as the value for the NSExtension -> NSExtensionPrincipalClass entry in your Notfication Service Extension target Info.plist. See example here.

Alternatively, you can leave the NSExtensionPrincipalClass entry unchanged and instead have your NotificationService class extend the CTNotificationServiceExtension class. You can then also override the defaults to your chosen key names if you wish. See Swift example here and Objective-C example here. In that case, only override didReceive request: contentHandler: as shown in the example.

If you plan on downloading non-SSL urls please be sure to enable App Transport Security Settings -> Allow Arbitrary Loads -> true in your plist. See plist example here.

Configure your APNS payload

Then, when sending notifications via APNS:

  • include the mutable-content flag in your payload aps entry (this key must be present in the aps payload or the system will not call your app extension)
  • add the ct_mediaUrl and ct_mediaType key-values (or your custom key-values) to the payload, outside of the aps entry.
{
    "aps": {
        "alert": {
      		"body": "test message",
      		"title": "test title",
   	  	},
        "mutable-content": 1,
   	},
    "ct_mediaType": "gif",
    "ct_mediaUrl": "https://www.wired.com/images_blogs/design/2013/09/davey1_1.gif",
	...
}

See an example Swift project here.

See an example Objective-C project here.