Technical Articles
Apple Push Notifications using SCP Mobile Services and HANA
In this day and age Enterprise Mobility is a real thing! And SAP are doing their bit in helping Enterprises by providing the tools (read SCP and its suite of services) to make mobile Apps that integrate with their SAP backends seamlessly. Once the basic app is developed, there is always a requirement to add bells and whistles to it so that it can be made even more user friendly. This blog will look at one such aspect which is Push Notifications to an iOS device.
In our scenario we have a SAP Patient Engagement system hosted on the SAP Cloud Platform. We have two major players in this whole solution. The main focus is on the Patient who uses a mobile (iOS) app to track their wellbeing. The other player is the Clinician who monitors the Patients’ wellbeing on a Fiori App. The Patient App is built using the SAP SDK for iOS. The Patients are supposed to receive Notifications at regular intervals which serve as reminders to track their physical activity/statistics like temperature, bowel movement or physical state like mood, drowsiness etc.
Here is a list of Components that form part of this solution –
- Backend – SAP Patient Engagement with a HANA DB on SAP Cloud Platform
- Frontend – iOS App used by patients.
- Push Notification Enablers – SAP Cloud Platform Mobile Services and Apple Push Notification Service (APNS)
Before we go into the details let’s look at the architecture diagram (figure1) that describes the scenario
Figure 1
Here is a bit more detail on what happens in each step of the diagram above.
Step 1 – Obtaining Device Token
- User(Patient) opens iOS app and is prompted to allow Notifications. This is a very familiar screen as shown in figure 2.
Figure 2
- If the Patient agrees, the iOS App receives a device token from APNS.
Step 2 – Send Device Token to SAP HANA
- The App sends the phone’s device token to SAP HANA along with the Patient’s internal ID
- This happens by the app calling an xsodata service which is exposed via the SAP Cloud Platform. The service receives the call and creates an entry in DB which registers this device token against the patient’s ID.
Step 3 – Obtain Device Registration id from SAP Cloud Platform Mobile Services
- The xsodata service above also calls an API to register the device in SCP Mobile Services.
- The API provided by SCP mobile services has a similar url/endpoint to this – https://mobile-xyz.hana.ondemand.com/odata/applications/v4/<your namespace>/Connections
- The response of the API call above contains what is called a registration id in SCP Mobile services. This registration id is linked to the device token passed in the request. Figure 3 shows the general Navigation of how to get to the Registration Page in “Mobile Services” on the SAP Cloud Platform. It also highlights a table entry at the bottom of the page which shows this registration id. Figure 4 shows the device token (highlighted) registered against a particular SCP Mobile services registration id.
- This registration id is stored in the HANA DB against the Patient and is used in step 4.
Figure 3
Figure 4
Step 4 – Call notification API
- A batch job in HANA(.xsjob) that runs at regular intervals (in this case daily at 9 am) . Here is a sample of the code in the xsjob file which calls an xsjs called sendNotifications.
{
"description": "Send notifications",
"action": "<packagepath>:sendNotification.xsjs::sendNotifications",
"schedules": [
{
"description": "Send notification at 9.00 AM",
"xscron": "* * * * 9 0 0"
}
]
}
- This xsjs service calls an SCP Mobile services API to send Notifications. The url/endpoint of this service will be something like this – https://mobile-xyz.ap1.hana.ondemand.com/Notification
- The json payload of the request looks something like this
{ "notification": { "alert": "{\"title\": \"Time to check in with your daily plan\",\"subtitle\": \"\",\"body\": \"Press and hold here to view details for your treatment plan\"}", "badge": 1, "sound": "default", "customParameters": { "apns.category": "dailyCheckIn", "apns.mutable-content": 1, "apns.thread-id": "check-in-7-02-2019" } }, "registrations": [ notifSchedule.CONNECTION_ID ] }
The category, mutable-content and thread-id fields of the payload are used by the app to perform different actions on receipt of the notification like showing a section of the app on 3-D Touch or showing an Image etc. In this case, a 3-d touch expand the notification below (figure 5) and shows details about the Patient’s treatment plan.
Figure 5
- The urls of these APIs are available in the APIs tab when you register your application on SAP Cloud Platform Mobile services., the how-to of which is not covered in this blog as it has been covered elsewhere.
Steps 5 and 6 – Send Push notification to iOS App
One the SAP Cloud Platform Mobile services receives the “Notification” API request it forwards the content to the Apple Push Notification Service along with the iOS device token which in turn send it to the iOS App using that token. This completes the whole circle.
Hope you found the blog useful. In case of details of code behind the iOS App sending the device token to HANA or for any part of the process in HANA do let me know in the comments section and I will be happy to provide more code snippets.
Thanks you sir very helpful blog! But sir i have some issue like where i can write the XSJOB file also where you made the object of APN. and sir i just have one backend this backend connected with the SAP odata with hana and my ios app also connected same way . so i just want to know how can i get the messege immidietely when backend user push the data in the hana table.
Hi Himanshu
Instead of starting the Job (xsjob) you could call the xsjs (that sends the notification) itself on successful update of the record in the HANA table.