This is the third of a four-part tutorial on setting up a mobile backend as a service on Amazon and setting up iOS and Android client applications to enable push notification. If you want to setup Push Notifications for an iOS app refer to Part Three.
If you have not done so already, please go to Part One to setup your backend LoopBack application server on Amazon.
This article provides information on creating iOS apps that can get push notifications from a LoopBack application. See Creating push notifications for information on creating the corresponding LoopBack server application.
The basic steps to set up push notifications for iOS clients are:
- Provision an application with Apple and configure it to enable push notifications.
- Provide a hook to receive the device token when the application launches and register it with the LoopBack server using the
- Provide code to receive notifications, under three different application modes: foreground, background, and offline.
- Process notifications.
For general information on the Apple push notifications, see Apple iOS Local and Push Notification Programming Guide. For additional useful information, see Delivering iOS Push Notifications with Node.js.
Before you start developing your application make sure you’ve performed all the prerequisite steps outlined in this section.
Configure APN push settings in your server application
If you have not already done so, create your APNS certificates for iOS apps. After following the instructions, you will have APNS certificates on your system. Then edit
config.js in your backend application and look for these lines:
Replace the file names with the names of the files containing your APNS certificates. By default,
readCredentialsFile() looks in the
/credentials sub-directory for your APNS certificates. You can create a credentials directory and copy your pem files into that directory.
|NOTE: If you want to try a sample client application follow steps in “Install and run LoopBack Push Notification app” OR if you want to enable push notifications for your own android application using the LoopBack SDK follow steps in “Add LoopBack iOS SDK as a framework to your own iOS Project”|
Install and run LoopBack Push Notification app
If you want to use the sample iOS client app, download the Push Notification Example iOS app . Then follow these steps to run the app
- Open the apnagent.xcodeproj in Xcode, select targets, under build phases unfold Link Binary with Libraries, and click on ‘+’ to add LoopBack framework(LoopBack iOS sdk).
- Edit Settings.plist and update the RootPath to your instance ip. In my case it’s: http://ec2-54-184-36-164.us-west-2.compute.amazonaws.com:3000/api
- Update the AppId with your own unique application id, when you register the app.
Add LoopBack iOS SDK as a framework
Open your XCode project, select targets, under build phases unfold “Link Binary with Libraries”, and click on “+” to add LoopBack framework.
The LoopBack iOS SDK provides two classes to simplify push notification programming:
- LBInstallation – enables the iOS application to register mobile devices with LoopBack.
- LBPushNotification – provides a set of helper methods to handle common tasks for push notifications.
The following code instantiates the shared
LBRESTAdapter. In most circumstances, you do this only once; putting the reference in a singleton is recommended for the sake of simplicity. However, some applications will need to talk to more than one server; in this case, create as many adapters as you need.
Register the device
Handle received notifications