簡體   English   中英

當應用程序被殺死時 FCM onMessageReceived 不會觸發 (Android Oreo)

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

我正在使用需要在發送 FCM 消息時打開前台服務的應用程序。 我的代碼是: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());
    }

}

但是,當我發送消息時,它會向我顯示另一個通知(不是我在函數中創建的那個),其中包含我在發送消息時編寫的消息正文和標題。(在我的函數中,我沒有從收到消息,我只是放了一些隨機文本)。 我的前台班沒有被解雇。

我不確定,但我遇到了幾乎相同的問題並要求用戶授予

ACTION_MANAGE_OVERLAY_PERMISSION

解決了我的問題。 在那之后,前台類被解雇了。

從后台啟動活動的限制

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM