Hi All,
My Domain is ABAP, After certain days of research on android and Phonegap, Finally we developed an application with enabling push notification.
Our business requirement is, we have a vehicle approval process, If sales consultant is giving discount more than certain amount, manager has to approve for the discount. For this we are done with ABAP code and created services in Gateway.
Note: You should have minimum knowledge on Android development in Eclipse.
Now the following are the steps to enable push notification.
1. Create a Google API Project and get Sender Id, API Key as shown in below link
Getting Started | Android Developers
2.Create an application in SMP for gateway service URL and in PUSH tab in Android section enter Sender Id, API Key
which you got in step 1.
as shown in https://websmp101.sap-ag.de/~sapidb/011000358700000038522014E.pdf
We done with application in SMP.
Now we need to create an application, with following
a. Register with SMP
b. Enable Push Notification
c. GCM Listener
d. And out application.
1. create Android Application project in eclipse.
In AndroidManifest.xml add following
(Change the package name as your application project name)
<!-- GCM connects to Internet Services. -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- Creates a custom permission so only this app can receive its messages. -->
<permission
android:name="com.packagename.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.packagename.permission.C2D_MESSAGE" />
<!-- This app has permission to register and receive data message. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- Network State Permissions to detect Internet status -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- Permission to vibrate -->
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- receiver is for gcm -->
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.packagename" />
</intent-filter>
</receiver>
<!-- intent service for gcm -->
<service android:name="GCMIntentService" />
</application>
2. Add following libraries to your project, these libraries you can find in MobileSDK3\KapselSDK\plugins\ folders
AfariaSLL-7.00.6169.0
ClientHubSLL-3.0.1
Common-3.0.0
commons-codec-1.3
Connectivity-3.0.0
cordova-2.9.0
CoreServices-3.0.0
DataVaultLib-2.3.3.13
gcm
gson-2.2.4
maflogger-1.2.1
maflogoncore-1.2.1
maflogonui-1.2.1
mafsettingscreen-1.2.1
mafuicomponents-1.2.1
Parser-3.0.0
perflib-1.1.5
Request-3.0.0
sap-e2etrace-2.3.3.13
sqlcipher-android-2.1.1
3. In MainActivity.java add below line to load our HTML .
super.loadUrl("file:///android_asset/www/index.html");
For more details about Phonegap development go through below link.
Now our aim is to call java code from JavaScript
4. create push.js file and following code, after include this file in your HTML, javascript section
var PushNotif= function(){};
cordova.addConstructor(function() {
cordova.addPlugin("pushnotif", new PushNotif());
});
PushNotif.prototype.send = function (message){
cordova.exec(function(){ },
function(){ },
'PushNotif',
'sendPush',
[message]);
}
function sendFeedback(){
window.PushNotif.prototype.send("My message body");}
5.Add the plugin in res/xml/config.xml <plugin name="PushNotif" value="com.packagename.PushNotif"/>
6. To call java code from JavaScript, use
window.PushNotif.prototype.send(body);
if you want to send any data to java, you can fill in body variable.
7. Create a java file in your package folder (src) with name PushNotif.java
"Here we are writing about, register the android device with SMP and enabling push notification.
Please find the attached sample file "PushNotif.java " for code.
8. Please add the GCMIntentService.java to your project and add GCMIntentService.txt code.
Note, here you need to change the GCM_SENDER_ID .
and add ConnectivityConstants.java to your project
Now we are done with our application creation. Now install the apk file in android device.
If everything is successful, you should see a new registration in SMP registrations.
By using PostMan Plugin POST Method, you should able to send the data(enjoy) to your android device.
http://xxxxxx:8080/Notification/y_VehicleApproval?data=enjoy
Now our aim is How to call POST method call using ABAP
1. create a HTTP Connections to External Server in SM59 , Give the URL of SMP and SMP credentials
2. find the place where we need to send notification to device( means on which status, Like If discount is more than 10k)
3. write below code to send notification
CALL METHOD cl_http_client=>create_by_destination
EXPORTING
destination = ' 'HTTP Connections to External Server
IMPORTING
client = client
EXCEPTIONS
destination_not_found = 1
internal_error = 2
argument_not_found = 3
destination_no_authority = 4
plugin_not_active = 5
OTHERS = 6.
* set http method GET
CALL METHOD client->request->set_method(
if_http_request=>co_request_method_post ).
* set protocol version
client->request->set_version(
if_http_request=>co_protocol_version_1_0 ).
* set request uri (/<path>[?<querystring>])
concatenate '/Notification/' connid into l_url. // in connid give the registration id, which got registered in SMP
cl_http_utility=>set_request_uri( request = client->request
uri = l_url ).
CALL METHOD client->request->set_header_field
EXPORTING
name = 'X-SMP-GCM-DATA'
value = data.
* * Send
DATA:timeout TYPE i.
timeout = 100000000.
CALL METHOD client->send
EXPORTING
timeout = timeout
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 4.
IF sy-subrc <> 0.
DATA:errortext TYPE string,
subrc TYPE i.
CALL METHOD client->get_last_error
IMPORTING
code = subrc
message = errortext.
WRITE: / 'communication_error( send )',
/ 'code: ', subrc, 'message: ', errortext.
EXIT.
ENDIF.
* receive
CALL METHOD client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 4.
IF sy-subrc <> 0.
CALL METHOD client->get_last_error
IMPORTING
code = subrc
message = errortext.
WRITE: 'communication_error( receive )',
'code: ', subrc, 'message: ', errortext to message.
EXIT.
ELSE.
WRITE:'Notification Sent' to message.
ENDIF.
* * close
CALL METHOD client->close
EXCEPTIONS
http_invalid_state = 1
OTHERS = 2.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
10 | |
9 | |
5 | |
4 | |
4 | |
3 | |
3 | |
3 | |
3 | |
3 |