Good day. I have a very specific issue considering the Socket IO library for android and it's service.
Important to mention that my device is huawei p8 lite which i am testing on.
Here it goes :
• I have a socket Io library which is being initialized inside the service i have created
• I set the listener to the socket io for new messages
• Everything works like a char if the application is not killed within the process trey by the user.
• The purpose of service was to keep the socket IO connection alive even if the application is killed so the user will be notified about new messages.
• As soon as the application is being killed,the Socket IO connection has been disconnected
No matter what i try,and i try all of possible ways : Isolating the process of Service,giving another process for the service,Starting it sticky,starting it not sticky recreating as soon as the service destroys and etc.
The only thing which has worked is the startForeground() method,but i do not want to use it as it will go under the design principles and plus i do not want to show any notification about the running service.
My question is the next : Can anyone help me out and tell me how can i keep the Socket IO connection alive even if the application is killed?
Here is the code of the service.
package com.github.nkzawa.socketio.androidchat;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;
import java.net.URISyntaxException;
import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
public class SocketService extends Service {
private Socket mSocket;
public static final String TAG = SocketService.class.getSimpleName();
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw null;
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "on created", Toast.LENGTH_SHORT).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "start command", Toast.LENGTH_SHORT).show();
try {
mSocket = IO.socket(Constants.CHAT_SERVER_URL);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
mSocket.on("newMessageReceived", onNewMessage);
mSocket.connect();
return START_STICKY;
}
private Emitter.Listener onNewMessage = new Emitter.Listener() {
@Override
public void call(Object... args) {
String message = args[0].toString();
Log.d(TAG, "call: new message ");
sendGeneralNotification(getApplicationContext(), "1", "new message", message, null);
}
};
private void sendGeneralNotification(Context context, String uniqueId,
String title, String contentText,
@Nullable Class<?> resultClass) {
NotificationManager notificationManagerCompat = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
android.support.v7.app.NotificationCompat.Builder builder = new android.support.v7.app.NotificationCompat.Builder(context);
builder.setAutoCancel(true);
builder.setContentTitle(title);
builder.setContentText(contentText);
builder.setGroup("faskfjasfa");
builder.setDefaults(android.app.Notification.DEFAULT_ALL);
builder.setStyle(new NotificationCompat.BigTextStyle()
.setSummaryText(title)
.setBigContentTitle(title)
.bigText(contentText)
);
Intent requestsViewIntent = new Intent(context, MainActivity.class);
requestsViewIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
requestsViewIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent requestsViewPending = PendingIntent.getActivity(context, Integer.valueOf(uniqueId), requestsViewIntent, 0);
builder.setContentIntent(requestsViewPending);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setShowWhen(true);
android.app.Notification notification = builder.build();
notificationManagerCompat.notify(Integer.valueOf(uniqueId), notification);
}
private Notification getNotification() {
Notification notification;
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setColor(getResources()
.getColor(R.color.material_deep_teal_500))
.setAutoCancel(true);
notification = builder.build();
notification.flags = Notification.FLAG_FOREGROUND_SERVICE | Notification.FLAG_AUTO_CANCEL;
return notification;
}
}
And NO,i do not want to use neither firebase nor any 3rd party made ones as i am using one now and i suffer from delays,from non-received notifications and etc,and i will make my own small one ways better. Thanks everyone for you'r time and patience.
Implementing Socket.io in the background is not the best of ideas. Socket.io has an active ping to your server. Using it for a short period is okay, but in the background it is a NO.
The best way to solve your problem is to combine Socket.io and FCM/GCM topics to emit a broadcast. On the users device, detect if there is an active Socket.io connection, if one exits ignore the FCM else execute the FCM.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With