socket.io-android-chat
socket.io-android-chat copied to clipboard
How can i Replace Service with Work Manager Due to Service is not works in 8.0?
i Have Done some of stuff and working fine in background and forground but when i kill it will not working.
public class ChatServiceWorker extends Worker {
private static final String TAG = "SocketService";
private Socket mSocket;
private Context mContext;
private NotificationManager notificationManager;
private NotificationCompat.Builder notificationBuilder;
private int currentNotificationID = 0;
@NonNull
@Override
public Result doWork() {
mContext = getApplicationContext();
notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
try {
IO.Options opts = new IO.Options();
opts.reconnection = true;
mSocket = IO.socket(Constants.CHAT_SERVER_URL, opts);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
initSocket();
return Result.SUCCESS;
}
private void initSocket() {
mSocket.connect();
mSocket.on(Socket.EVENT_CONNECT_ERROR, onConnectError);
mSocket.on(Socket.EVENT_CONNECT_TIMEOUT, onConnectError);
mSocket.on(Socket.EVENT_CONNECT, onConnect);
mSocket.on(Socket.EVENT_RECONNECT, onConnect);
mSocket.on(Socket.EVENT_DISCONNECT, onConnectError);
//I send the an auth event to the socket.io server
// auth();
mSocket.on("chat message", onReceived);
}
private Emitter.Listener onConnect = new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.d(TAG, "connected " + mSocket.connected());
}
};
private Emitter.Listener onConnectError = new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.d(TAG, "error " + args[0].toString());
}
};
private Emitter.Listener onReceived = new Emitter.Listener() {
@Override
public void call(final Object... args) {
Log.d(TAG, "received rtm");
// I parse the object...
// binder.messageArrived(args[0].toString() + "," + args[1].toString());
Intent i = new Intent(Constants.ACTION_DASHBOARD_CONTENT);
i.putExtra(Constants.RECEIVED_USERNAME, args[0].toString());
i.putExtra(Constants.RECEIVED_MESSAGE, args[1].toString());
LocalBroadcastManager.getInstance(mContext).sendBroadcast(i);
/* Data output = new Data.Builder()
.putString(Constants.RECEIVED_USERNAME, args[0].toString())
.putString(Constants.RECEIVED_MESSAGE, args[1].toString())
.build();
setOutputData(output);*/
setDataForSimpleNotification(args[1].toString());
}
};
/* @Subscribe public void sendMessage(SendMessageEvent event) { mSocket.emit("send", event.getMessage()); }*/
/* @Override public void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy"); mSocket.disconnect(); mSocket.off(Socket.EVENT_CONNECT_ERROR, onConnectError); mSocket.off(Socket.EVENT_CONNECT_TIMEOUT, onConnectError); mSocket.off(Socket.EVENT_CONNECT, onConnect); mSocket.off("chat message", onReceived);
// mBus.getInstance().unregister(this); }*/
/* @Override public IBinder onBind(Intent intent) { return binder; } */ private void setDataForSimpleNotification(String message) { notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(mContext) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("Chat") .setContentText(message); sendNotification(); }
private void sendNotification() {
Intent notificationIntent = new Intent(mContext, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(contentIntent);
Notification notification = notificationBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
currentNotificationID++;
int notificationId = currentNotificationID;
if (notificationId == Integer.MAX_VALUE - 1)
notificationId = 0;
notificationManager.notify(notificationId, notification);
}
public void sendMessage(String variable,String mUsername , String message){
if (!mSocket.connected()) return;
mSocket.emit(variable, mUsername , message);
}
}
calling from Fragment
Constraints myConstraints = new Constraints.Builder() .setRequiredNetworkType(NetworkType.CONNECTED) .build();
OneTimeWorkRequest compressionWork =
new OneTimeWorkRequest.Builder(ChatServiceWorker.class)
.setConstraints(myConstraints)
.build();
WorkManager.getInstance().enqueue(compressionWork);
any help will be appreciated.
Thank you.
It's very long Time for the Answer. May be it will help other. Work Manager don't work as expected in some of chinese os they stop Work Manager as soon as user kill the Application. Solution is grant auto startup permission.
@PrinceVergil, thanks. If a user kills the Application, why should we expect socket to work? If we use Work Manager, can we grant startup permission to an application? Wow, isn't it too harsh? :) Please, say, should we use foreground services?
@CoolMind using foreground or background service totally depends on the requirements.
but work manager have some issue on Chinese os. you can check this so post.