我有一个Android应用程序,用于发送和接收FCM消息(Google Firebase消息)。
它的工作非常完美,因为当我的应用程序不在forground或不使用它时我可以收到消息,因为我有一个服务在其上运行。
不幸的是,当我没有在某些智能手机上运行应用程序或其背景时,它不起作用,如OPPO,VIVO或更可能在ColorOS上运行。
可能是什么问题?我们尝试过,但WhatsApp也是如此
以下是我的服务代码:
package com.msb.launcher.services;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import com.msb.auth.utils.Keys;
import com.msb.auth.utils.LogUtils;
public class MessagingService extends Service {
public static final String EXTRA_AUTH_TOKEN = "EXTRA_AUTH_TOKEN";
public static final String EXTRA_FIREBASE_ID_TOKEN = "EXTRA_FIREBASE_ID_TOKEN";
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
LogUtils.i("Service running.");
sendEmptyMessageDelayed(100, 500);
}
};
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
LogUtils.i("onStartCommand");
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onTaskRemoved(Intent rootIntent) {
onDestroy();
super.onTaskRemoved(rootIntent);
try {
handler.removeCallbacksAndMessages(null);
sendBroadcast(new Intent(Keys.ACTION_RESTART_SERVICE));
} catch (Exception ex) {
ex.printStackTrace();
}
}
@Override
public void onDestroy() {
LogUtils.i("Service Destroy");
handler.removeCallbacksAndMessages(null);
sendBroadcast(new Intent(Keys.ACTION_RESTART_SERVICE));
super.onDestroy();
}
@Override
public void onCreate() {
super.onCreate();
LogUtils.i("Messaging Service Started");
handler.sendEmptyMessage(100);
}
}