Registering for Notifications on iOS
We are no longer allowing customers to onboard to Notify. We intend to deprecate the Notify product on April 25, 2024. Learn more in our Notify API End of Life Notice. We prepared this Transition Guide to assist in supporting your push notification use cases.
Ready to send your first notification? Of course you're not! That's why you're here. But don't worry, we'll get you registered so you can start sending notifications faster than a collision-free hash table lookup (OK, maybe not that fast).
A Notifications application involves two components:
- A client (iOS) app that registers for and receives notifications
- A server app that creates bindings and sends notifications
The aim of this guide is to show you how these two components work together when you register for notifications. Let's get to it!
Table of Contents
Client: Registering a Device with APNS
Before working with Twilio Notifications, we'll need to register our device with APNS. This registration will happen every app launch, and in our AppDelegate and we can specify the type of notification (sound, alert, or badge) that we'd like to accept. On the first launch, iOS will prompt us to enable push notifications with a popup alert.
Once we've enabled notifications, iOS will send an asynchronous response to the app via the didRegisterForRemoteNotifications function. Make sure you've properly configured your iOS app for notifications or else the registration won't work.
Client + Server: Creating a Binding
Client
Once we've received a successful registration response from the didRegisterForRemote NotificationsWithDeviceToken method, it's time to create a Binding. A “Binding” represents a unique device that can receive a notification. It associates a unique device token (provided by iOS) with an identity and an optional set of tags that you define for your application. When you want to send a notification, Twilio will send your notification only to Bindings that match the parameters you specify (more on that later).
In iOS 13, the format of the string returned when you call the description
method on an NSData
object has changed from previous iOS versions. This means that code that parses the device token string from a call like [deviceToken description]
or similar, where deviceToken
is an NSData
object, will break in iOS 13.
The preferred way of creating a string of hex characters from bytes would be to iterate through each of the bytes and create a new string using a format string. There is an example in the Objective-C snippet for Creating a Binding.
The client app should send a request to the server containing the device token and any additional information the server might need to create the binding (typically Identity and BindingType). We'll use the NSURLSession networking API that is part of the iOS standard library to make the request. You could also use another library, such as AFNetworking or Alamofire.
The last step on the client side is storing the Endpoint identifier generated by Twilio and included in the response. Storing the Endpoint and reusing it in subsequent requests will allow us to avoid creating duplicated Bindings when the device token changes. Here we use the KeychainAccess helper class from our quickstart app but you can use your own way of accessing the keychain.
Server
In our server app, we’ll receive the POST request. It’ll contain the following four parameters.
name | description |
---|---|
Identity | The Identity to which this Binding belongs. Identity is defined by your application and can have multiple endpoints. |
BindingType | The type of the Binding determining the transport technology to use. We will use apn here. |
Address | The device token obtained from iOS. |
Endpoint | The identifier of the device to which this registration belongs. This is generated the first time you create a Binding. Then you need to store it in the keychain and provide it in your subsequent registrations to avoid duplicating Bindings even if the device token changes. |
Notify uses Identity as a unique identifier of a user. You should not use directly identifying information (aka personally identifiable information or PII) like a person's name, home address, email or phone number, etc., as Identity because the systems that will process this attribute assume it is not directly identifying information.
We'll use index.js in our server app and these four parameters to send an API call to Twilio, using the next generation Twilio Node Helper Library. This will help us create a new Binding. We implement this logic in the /register endpoint, but this is not required. You can come up with your own API, perhaps integrate it into a login or a session initialization flow.
Now that we've registered the client app with APNS and created a Binding, it's time to send some notifications! Check out our Sending Notifications guide for more information.
Need some help?
We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.