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: 
former_member190010
Contributor
This is the second part of the 2 blog posts regarding the usage of native mobile notifications in the standard sap fiori client:

As you have seen in the previous blog, it is possible to receive native mobile push notifications in your Standard Fiori client using SCP Mobile Services. Please refer to this blog for more information.

In this article I am going to finish the circuit, by showing you how to send the Notifications messages from the S/4HANA System. The way we are going to do this, is basically by calling the rest api of SCPms to send the notifications to the different devices. The S/4HANA system should be able to reach the SCPms url and port. This blog was really helpful to implement this scenario.

It is important to emphasize that it is not possible to use the cloud connector to call SCPms as it is stated in the sap note 2525899.

Landscape


The best way to start explaining how to send push notifications from an ECC or S/4HANA system, is by showing which is the landscape of the solution.

The landscape of the image is just a high level picture. It's very likely that your landscape defer from the one shown below, for example by adding firewalls and proxies between the S/4HANA system and the SCP platform.



Configure connection between FES and SCPms


 

1) Import Root CA Certificate from SCPms to SAP Gateway system

Go to the SCPms and get the url from the APIs section.


 

Paste this url into the browser, and download the ssl certificate.

 



 


 

Then, we need to import this certificate into the SAP System.

Go to transaction code STRUST. Select “SSL client SSL Client (Anonymous)”.


 


Once imported, click on "Add to Certificate List". (If you see the button disabled, please check your screen is in edit mode).


Remember to save the imported Root Certificate file by pressing the Save icon.


 

2) Configure HTTP Destination targetting SCPms

Now that we have already imported the SSL certificate from SCPms, we are ready to configure the HTTP connection in the Netweaver system.

Go to t-code SM59, and create a new entry with Connection Type = G (HTTP connection).

Type the Target Host for your SCPms instance server name with the port number 443. Remember that we can get this information from the API section as we have seen in the first step of this blog.

Note: Depending on the definition of your landscape, you may have to configure a proxy in the SM59 connection, change the host to a proxy system and configure additional routing rules in your reverse proxy system. Please check it with your system administrator.


 

Navigate to the tab "Logon & Security", and enter your SCPms account credential. (S-User and Password. Remember that the S-User has to have the permissions to send notifications from SCPms as seen in the previous article).


 

Finally, scroll down and select "SSL = Active", "SSL Certificate = ANONYM SSL CLIENT (Anonymous)". This ensures the connection will use the Root Certificate you had imported.



ABAP Code


Now we are going to review the abap code needed to send notifications from the ECC or S/4HANA. Have in mind that this is just a snippet of code to explain the functionality. If you want to implement it in a real project, I would recommend to modularize it in a Function Module or Class and call it in the different places where you would like a notification to be sent to the user's mobile device.

Previously to code any action, we need to define the global object that will hold the definition of the http client, a constant that will hold the name of the destination created in our previous steps, and a constant that will hold the query that we are going to add to the host and port defined in the destination.

In this case, I am sending notification to all the devices registered against an application defined in the SCPms console, but there are also another scenarios as sending notification to certain users or device that you could implement by modifying the way the api is called. Check this link for more information (section 1.5.1.2.9 Native Push NotLficDtLon for a Back End).

I have used the application ID that we have used in the previous article, but you should use your own application ID.
DATA: go_http_client TYPE REF TO if_http_client.

CONSTANTS: c_rfc_scpms TYPE rfcdest VALUE '<RFC destination name>', " RFC Destination TYPE G
c_query TYPE string VALUE '/restnotification/application/com.sap.test'.

The first thing we have to do is to instance the http client. For this we use the constant that holds the rfc type g name.
****Create the HTTP CLient
CALL METHOD cl_http_client=>create_by_destination
EXPORTING
destination = c_rfc_scpms
IMPORTING
client = go_http_client
EXCEPTIONS
argument_not_found = 1
destination_not_found = 2
destination_no_authority = 3
plugin_not_active = 4
internal_error = 5
OTHERS = 6.

 

The second step is where we set the url of the request, and to set the content-type of the request.
DATA: l_query TYPE string.

l_query = c_query.
****Set Request Path
cl_http_utility=>set_request_uri(
EXPORTING
request = go_http_client->request " HTTP Framework (iHTTP) HTTP Request
uri = l_query " URI String (in the Form of /path?query-string)
).

****content type
CALL METHOD go_http_client->request->set_content_type
EXPORTING
content_type = 'application/json'.

 

Finally, the third step is where we define that the http client will be a rest client.

We also define here the content of the body of the request and we send the request using the HTTP method selected. In this case, it is a POST method.

After sending the request, we receive the response and analyze the results.

If everything goes well, we are going to receive the http status code "201" as in the rest client.

In case there was an error, as bad request or something similat, the http status code will be different.
  DATA: lo_rest_client TYPE REF TO cl_rest_http_client.
DATA: lo_request TYPE REF TO if_rest_entity.
DATA: lv_body TYPE string.

****Create REST Client object
CREATE OBJECT lo_rest_client
EXPORTING
io_http_client = go_http_client.
TRY.
CONCATENATE '{"alert":"Notifcation coming from SAP S4HANA", "badge":1, "data": "{\"title\":\"Notification Title\",\"details\":{}}", "priority": "high" }' INTO lv_body.

" Set Payload or body ( JSON or XML)
lo_request = lo_rest_client->if_rest_client~create_request_entity( ).
lo_request->set_content_type( iv_media_type = if_rest_media_type=>gc_appl_json ).
lo_request->set_string_data( lv_body ).

" POST - Send Request
lo_rest_client->if_rest_resource~post( lo_request ).

" Get Response
DATA(lo_response) = lo_rest_client->if_rest_client~get_response_entity( ).
DATA(lv_http_status) = lo_response->get_header_field( '~status_code' ).

****HTTP Request Failed --> If Response status code different to 201
IF lv_http_status NE 201.
DATA(lv_reason) = lo_response->get_header_field( '~status_reason' ).
WRITE: lv_reason.
RETURN.
ENDIF.

****Read the response data in JSON.
DATA(lv_json_data) = lo_response->get_string_data( ).
"Refresh the SLACK response to clear HTTP memory of previous calls
IF go_http_client IS BOUND.
go_http_client->refresh_response( ).
"Close HTTP session (Exception HTTP_NO_MEMORY)
go_http_client->close( ).
ENDIF.

****Collect into the exception table.
CATCH cx_rest_client_exception INTO DATA(lo_rest_client_exception).
WRITE: 'Error: Rest Client exception'.
ENDTRY.

You can download the code from my github account.

Once we execute the report, we are going to receive the notifications as you can see in the images below (remember that the fiori client app should be closed in order to receive the notifications).


 



Conclusion


I would like to finish this post, mentioning another relevant use case apart from the workitems approval for mobile notification.

As you have seen in different articles, it was recently introduced in S/4HANA the functionality of situation handling. For those who are not aware of what situation handling is, it is a framework that automatically detectstracks, and informs about issues in your business that require the attention of specified users. Whenever there is a new event that would need the attention of a business user, the S/4HANA system sends a notification to the S/4HANA notification center of the user that needs to review the event. As you would be imagine right now, sounds pretty natural the usage of the notifications received in the mobile device to process the events generated by the situation handling framework.

I personally think this is a great example of increasing user experience by using S/4HANA and SCP solutions together, and you could make it by doing some little tweaks to the code shared in this blog.

Finally, if you are interested on situation handling I recommend you to read the following articles: Link1 and Link2.

 

Best regards,

Emanuel