How to call the REST Notification API of SMP 3 from ABAP in an Username based scenario
A few months ago we had to implement Push notifications for a SAPUI5 hybrid app. Back then the Kapsel side was well documented already by Daniel Van Leeuwen in: Getting Started with Kapsel – Part 4 — Push (SP09+)
However the Backend side was a bit less documented.
There are two roads to follow to get Push notifications working from the backend. One is by leveraging registration ids and logging those in the backend.
This way is thankfully now well described as well in the following document series: How To… Implement End-to-End Push Notifications with HCPms and SAP Gateway by Kenichi Unnai.
But there is also extended functionality in the Rest Notification API to send notifications to specific user(names) for specific apps as described for the mobile side in the blog post by Daniel van Leeuwen and in the SMP help documents here: Push Notification API Scenarios – REST API Application Development – SAP Library
We followed the latter route as that scenario was the one preferred for our scenario and the documentation and implementation for the first was a bit scattered around and not on the level it is now.
To implement the username based scenario we wrote a bit of ABAP leveraging the HTTP_CLIENT on the Backend to send a HTTP post call to the SMP server in case a specific object was created for that user.
Ofcourse this is the basic scenario if all your servers are reachable for each other. In case you use HCPms you might have to extend it, as your Backend server probably can’t talk to the outside like this.
The basic ABAP code we used looks like this, you still have to change it so the URL is more dynamic.
DATA: SURL TYPE STRING,
HTTP_CLIENT TYPE REF TO IF_HTTP_CLIENT,
RLENGTH TYPE I,
JSONSTRING TYPE STRING.
SURL = ‘http://<server uri>:<server http port>/restnotification/application/<appname>/user/<username>’.
JSONSTRING = ‘{“alert”: “<alert text>”,”badge” : <badge number if used>,”data”: “<data>”,”sound”: “default”}’.
RLENGTH = STRLEN( JSONSTRING ).
CL_HTTP_CLIENT=>CREATE_BY_URL( EXPORTING URL = SURL
IMPORTING CLIENT = HTTP_CLIENT ).
CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
EXPORTING
NAME = ‘Content-Type’
VALUE = ‘application/JSON; charset=utf-8’.
CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
EXPORTING
NAME = ‘Authorization’
VALUE = ‘<authorization of smppush user (Basic …. for example)>’.
CALL METHOD HTTP_CLIENT->REQUEST->SET_HEADER_FIELD
EXPORTING
NAME = ‘~request_method’
VALUE = ‘POST’.
CALL METHOD HTTP_CLIENT->REQUEST->SET_CDATA
EXPORTING
DATA = JSONSTRING
OFFSET = 0
LENGTH = RLENGTH.
HTTP_CLIENT->SEND( ).
HTTP_CLIENT->RECEIVE( ).
HTTP_CLIENT->RESPONSE->GET_STATUS( IMPORTING CODE = RLENGTH ).
JSONSTRING = HTTP_CLIENT->RESPONSE->GET_CDATA( ).
HTTP_CLIENT->CLOSE( ).
Something to consider when using this:
– Username is case sensitive. Either make sure that the user name is always the same case on SMP server as on the call from the Backend
or use SMP3 runtime SP10 where you can make it case insensitive.
Hi Vincent,
I got http_communication_failure when requesting http_client->receive().
Do you have any idea ?
I am afraid I don't have the exact answer, as I am not an ABAP expert.
However when googling for the failure code I found: Http_Communication_Failure in IF_HTTP_CLIENT--&... | SCN
Which might help in researching where and why the failure exactly occurred.
Hi Vinvent,
It was the network problem, I'm able to received notification now.
You provide a very nice blog and a complete solutions.
Thanks for your good work.
Good to hear you resolved the problem. 🙂
And thank you. I appreciated the help I got on SCN and SO in the days I just started with mobile, now is the time I can repay it by helping others.
Hi Choong,
I am also trying to implement solution provided by 'Vincent' for push notification from backend. it is popping for user credentials which user/password should i give in. Should it be the SMP user credentials , if yes what should be the client .
Your help much appreciated. Please do help me with some input.
Thanks
Hello Vincent,
I am also trying to implement your solution for push notification backend. it is popping for user credentials which user/password should i give in. Should it be the SMP user credentials , if yes what should be the client .
You help much appeciated. Please do help me with some input.
Thanks
hello,
I am trying to implement the above solution but in my case its giving 401 Unauthorized error code , but when i try to excute the same url with rest client its working perfectly ok. could anyone help me by giving some valuable inputs.