,FCM服务也会自动启动并发送通知。
删除FCM令牌
public class FirstService extends Service {
private static String TAG = "Service";
Boolean isLoggedIn;
SharedPreferences sharedPreferences;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Log.d(TAG, "FirstService started");
sharedPreferences = getSharedPreferences(getPackageName(), MODE_PRIVATE);
isLoggedIn = sharedPreferences.getBoolean("isLoggedIn", false);
if(isLoggedIn){
Intent intent1 = new Intent(this,MyFirebaseMessagingService.class);
startService(intent1);
}
else
{
stopService(new Intent(this, MyFirebaseMessagingService.class));
stopService(new Intent(this, MyFirebaseInstanceIDService.class));
}
}
@Override
public void onDestroy() {
super.onDestroy();
sharedPreferences = getSharedPreferences(getPackageName(), MODE_PRIVATE);
isLoggedIn = sharedPreferences.getBoolean("isLoggedIn", false);
if(isLoggedIn){
Intent intent = new Intent(this,MyFirebaseMessagingService.class);
startService(intent);
}
else
{
stopService(new Intent(this, MyFirebaseMessagingService.class));
stopService(new Intent(this, MyFirebaseInstanceIDService.class));
}
Log.d(TAG, "FirstService destroyed");
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
sharedPreferences = getSharedPreferences(getPackageName(), MODE_PRIVATE);
isLoggedIn = sharedPreferences.getBoolean("isLoggedIn", false);
if(isLoggedIn){
Intent intent = new Intent(this,MyFirebaseMessagingService.class);
startService(intent);
}
else
{
stopService(new Intent(this, MyFirebaseMessagingService.class));
stopService(new Intent(this, MyFirebaseInstanceIDService.class));
}
Log.d(TAG, "FirstService tastremove");
}
}
如何在自定义服务中停止FCM服务?首次服务开始,但是在后台,我的服务不起作用,即使我没有登录我的应用程序?
firebasemessagingservice在接收消息中没有积极的角色。它仅在收到消息后处理。
即使您停止了它,Android也会在新消息到达时再次开始。
如果您希望您的应用程序停止接收消息,则可以停止发送,或者可以使用FirebaseInstanceId.getInstance().deleteId()