Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
mgrasshoff
Advisor
Advisor


Hi there,

In my last blog I promised to write a new one about how to use “mobile services”. I first thought about writing about our Offline OData innovation, but Midhun was much faster than me. So I take the topic of push notifications.

Notifications are adding real time capabilities to mobile applications. They help to speed up things. Typically it is triggered by a business event occurring in a back-end, so one of the most important parts for enabling real-time in mobile scenarios is to carefully think about the implementation of triggering events from the backend. Which business events are important, who are the recipients, what information should be sent (basically none, it should be a poke/pull implantation). If you have a clear understanding of those questions and hopefully some answers you can think about implementing push notifications.

Since all those questions are business related and I can’t answer them here, I'll focus to the technical implementation, which is always the same.

So for this we mock the business event just by using a rest client. That’s the easy part and I will later explain how to do that. The middle layer is obviously SAP HANA Cloud Platform mobile services and for this blog I choose Android to demonstrate the receiver part and I’ll use the Hybrid SDK (Cordova plugins provided by SMP SDK). Basically I followed this great guide (Getting Started with Kapsel - Part 4 -- Push) by daniel.vanleeuwen, which covers most of the client related steps. I strongly encourage you to do the same if you did not yet. So stop reading here and create a hybrid, Android, mobile app, which is push enabled. Come back to this blog if you are ready.

 

Hi and welcome back.

I think you have recognised that Daniel used the SMP Admin UI to set the server sided configuration of Push. Instead of doing it for SMP, I will now show you how to do the same in the Admin Cockpit of “mobile services”.

 

Open the Admin Cockpit, and select the “Applications” tile.



As you can see I have already created an Application Configuration that points to a public OData service. The AppID (com.sap.hcpms.push.demo) can be named as you like. Here on this screen you can see a PushURL. This URL is the “old” API that supports SAP Gateway subscription based notifications. This kind of notifications is suitable for scenarios where your backend keeps track of the registered devices. Today we will concentrate on the enhanced and more flexible Push API. So right now, just ignore this URL.

At this point you need to either create a new Application Configuration or “Configure” an existing one.

Navigate to the “Push” tab (first click “Configure).

As you can see, you con configure various native push services here. HANA Cloud Platform mobile services supports native Android (GCM), iOS notifications (APNS), Blackberry, Windows Phone and Windows Desktop (Metrrrr…, sorry Windows Store Apps).

Since we want to use Android today, we need to provide an API Key and a Sender ID. In order to get those you need to follow the instructions here:

http://developer.android.com/google/gcm/gs.html

Your Sender ID is your Project Number – a simple numerical value like 123456789123.

The API key is a bit longer, it is also described how to get this under the link above.

We know put the values, generated by Google into the Admin Cockpit and store the values. That’s it. It’s done, really. This is how it should look like:



Save your changes.

 

One more thing:

Not everybody is allowed to send notification to the server. Even the Administrator can't do this. You need a user with a special role in order to issue notifications. Typically a technical user that is known to the backend is granted the so called "Notification User" role.

In order to do this you need to navigate to the HCP account page.

Select "Services".

 



Now select the little "Configure Roles" icon on the far right hand side.

Now select the "Notification User" in the upper part of the screen.

Click on "Assign..." and put your user ID into the dialog.

Then confirm the dialog.

The screen should look like the following:


 



You need to repeat these steps for any user you want to grant access to the notification API.

 

We can now start our client application and register against HANA Cloud Platform mobile services.

Once we are registered we need to register with Google to get a “registration_id”, this is handled by the push plugin in the client (it uses the native push libraries by Google to do so). This registration id needs to be sent to the mobile services in order to make HCPms aware of the particular device (we knew the device before, but not in the context of GCM). This is also done by the push plugin, so you don’t have to care about it.

In summary, “mobile services” knows how to act like a GCM server and it knows all about the clients. So you could say it behaves like a message broker.

Now we can mock the back-end event using POSTMAN (Google Chrome plugin) or any other REST client (I like the RESTClient from Wiztools as well – more control for cookies, curl for the hackers among you).

This is a valid back-end call:

 

POST /restnotification/application/com.sap.hcpms.push.demo/ HTTP/1.1
Host: hcpms-d055161trial.hanatrial.ondemand.com
Content-Type: application/json
Authorization: Basic bm90bXlwYXNzd29yZA==
Cache-Control: no-cache

{"alert":"New Vacation Approval Request", "data":"It works!!" }


 

 

Notice that we use /restnotifications instead of /Notification. The new REST API provides a lot more options – try it out.

 

tl;dr


1. Follow:Getting Started with Kapsel - Part 4 -- Push

2. Follow: Getting Started on Android | Android Developers

3. Create Application config in HCPms cockpit and provide Sender ID and API Key

4. Test you application using a rest client.

 

Have Fun,

Martin

9 Comments