Setting up push notifications for iOS apps can be one of the most tedious tasks for iOS mobile development. In order to understand and setup it, we will first understand the entities involved and the flow during the complete process.
- App: The app for which we need to configure the Push Notifications
- App Server: This will be used to trigger the Push notification to devices
- Apple Push Notification Services (APNS): This is the service provided by Apple to send push notification to registered devices
- Device-token: This is a unique token generated by APNs when the device registers with it, using the device’s certificate. This token is sent to App Server and used to uniquely send a message to the device via APNs
- Certificates: Certificate identifies the communication between our app server and APNs and is used for authentication and security
Below are the main interactions which happen during the setup of the connections
Handshake between APNs and App Server to setup Trust
Device Token Generation
Send the Push Notifications Once the above steps are completed, while sending the Push notification our App Server sends a Push Notification request to APNs, using its certificate and devicetoken for the required device. APNs uses App server’s certificate information (The .p12 file info) to authenticate the App Server and once done uses the device token to uniquely identify the device and send the Push notifications to it.
In order to setup Push notification for an Ionic app for iOS we need 2 things
- Setup the certificates, provisioning profile
Setup certificates: This step requires that you have a Apple Developer account. We need to create a certificate and use to to create a provisioning profile.
- Create a Certificate Signing Request:
- Open Keychain Access tool on Mac
- Open Keychain Access > Certificate Assistant > Request a Certificate From a Certificate Authority
- Enter name and address. These are optional
- Save it to disk. This will generate your certificate singing request file and it will be saved as .certSigningRequest file
- Create an App Id: Apple identifies every app by a unique app id added in dev center. This is by convention named in reverse-DNS format and will be same as app’s bundle id (mentioned in
ios-CFBundleIdentifier). E.g. com.aditya.myapp
- Go to Apple Developer Center
- Open Certificates , Identifiers & Profiles
- Open Identifiers section and click + to add a new app
- Provide an app name and select the checkbox for Push Notifications under App Services
- Provide app prefix and suffix. Prefix can be the default one, which is the Team Id. while suffix can be the app’s bundle id (in reverse-DNS format)
Continue. This will show the app as
- Save the app
- Configure the app to send Push notifications:
- From list of apps, open the newly created app
- Click Edit and select the push notification service which says Configurable
- Enable it by clicking on Create Certificate
- This will take you to this screen. Since we already have the .certSigningRequest, press Continue
- On the next screen locate .certSigningRequest file and once selected, it will give option to download the certificates for the app configured with Push Notification services.
- Download the dev version of the cert. This will be a file named
- Double click it to add it to keychain
- Locate this cert in keychain. It will be present with the name of the app
- Right click to export this certificate as
.p12file and save
- Provide a password when prompted and note it down for later purposes. This is optional, but recommended
.p12generated above to your app server for App Server to interact with APNs for any further communications.
Congrats. You have completed the steps for developing Push Notifications for the development version of your app. You need to repeat above steps for production version of your app
- Create the Provisioning profile: The provisioning profile is used in development to limit the access of the app to certain devices.
It is a XML file which consist information about the AppIDName, ProvisionedDevices, DeveloperCertificates etc.
- Under Certificates , Identifiers & profiles, select Provisioning Profiles
- Select the + in the top-right of the screen to create a new iOS Provisioning Profile.
- Select iOS App Development as profile type, then Continue.
- Select your App ID created in Section 1, then Continue.
- Select the iOS development certificate you created, then Continue.
- You will now be asked which devices will be included in your provisioning profile. Select all the development devices then Continue.
- Provide name for the profile, then hit Generate.
- Download the profile from the next screen and install it by double clicking on it
- This will add the provisioning profile to XCode and you are ready to use the Push Notification on development machine.
You can provide the path to the provisioning profile while generating a manual build using
Add phonegap-push-plugin to the app using:
ionic plugin add phonegap-push-pluginThis will expose a global window object
Register for the Push notification. The
push.on('registration')event callback will provide us the devicetoken id mentioned in Devicetoken generation step. This will be send to our app servers
Now use the push notification payload from
Note: There is an issue with plugin that when the app is killed, and a Push notification is tapped the
push.on('notification') callback is not called. This is a known issue and
I have mentioned a solution here