Android push notification service based on io-session-handler
Building an own Android push messaging service to receive data messages in the background in real-time and alternate for Firebase Cloud Messaging.
Add the below dependency into your module level build.gradle
file
implementation 'com.aslam:scm:+'
Make sure you have set usesCleartextTraffic
to true in AndroidManifest.xml
file
<application
...
android:usesCleartextTraffic="true">
Create a SCMessaging
instance with context, socket server url and device token.
SCMessaging scMessaging = new SCMessaging(this, "http://192.168.8.200:3000", "user_token");
Implement onMessageData
method to listen the server push messages.
scMessaging.setListener(new SCMessaging.Listener() {
@Override
public void onMessageData(String data) {
Log.d("onMessageData", data);
}
});
Finally call the connect method to start the socket connection.
scMessaging.connect();
Override all the methods in SCMessaging.Listener()
to notify the program from more events as below.
scMessaging.setListener(new SCMessaging.Listener() {
@Override
public void onMessageData(String data) {
}
@Override
public void onConnect(String serverURL) {
}
@Override
public void onDisconnect(String serverURL) {
}
@Override
public void onConnectError(Exception ex) {
}
});
This is the constructor for passing more options to the Socket.IO engine, the options variable should be an IO.Options()
object, if it's not included in your project, you can add the library in the build.gradle
file.
IO.Options options = new IO.Options();
options.reconnection = false;
SCMessaging scMessaging = new SCMessaging(this, "http://192.168.8.200:3000", "user_token", options);
implementation ('io.socket:socket.io-client:1.0.0') {
exclude group: 'org.json', module: 'json'
}
Test the sample app that I made for you: SCM-Demo-v1.0.1.apk
Made with ❤️ by Aslam Anver