简体   繁体   中英

FCM onMessageReceived does not fire when app is killed (Android Oreo)

I am working at an app that needs to open a foreground service when a FCM message is sent. My code is: FirebaseService.class:

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

import androidx.core.app.NotificationCompat;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import java.io.IOException;

public class FirebaseService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d("From",remoteMessage.getFrom());
        Intent intt = new Intent(this,foreground.class);
        startActivity(intt);
    }

    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        String channelId = "TT";
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.drawable.ic_launcher_foreground)
                        .setContentTitle("TR")
                        .setContentText(messageBody)
                        .setAutoCancel(true);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId,
                    "Channel human readable title",
                    NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }
        Log.d("TEST","received");
        Server server = new Server();
        try {
            if(server.connect())
                Log.d("TEST","Connected");
            else
                Log.d("TEST","NOT Connected");

        } catch (Exception e) {
            e.printStackTrace();
        }
        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }

}

But when I send a meesage it shows me another notification (not the one I create in my function) with the message body and title that I wrote when the meesage was sent.(in my function I do not sent the title and body from the received message and I just put some random text). And my foreground class is not fired.

I am not sure, but I had nearly the same problem and asking the user to grant

ACTION_MANAGE_OVERLAY_PERMISSION

solved the problem in my case. After that, the foreground class got fired.

Restrictions on starting activities from the background

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM