【问题标题】:FCM onMessageReceived does not fire when app is killed (Android Oreo)应用程序被杀死时 FCM onMessageReceived 不会触发(Android Oreo)
【发布时间】:2020-03-02 18:52:26
【问题描述】:

我正在处理一个需要在发送 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());
    }

}

但是当我发送消息时,它会显示另一个通知(不是我在函数中创建的那个),其中包含我在发送消息时编写的消息正文和标题。(在我的函数中,我没有发送标题和收到消息的正文,我只是放了一些随机文本)。而且我的前台类没有被解雇。

【问题讨论】:

    标签: java android


    【解决方案1】:

    我不确定,但我遇到了几乎相同的问题并要求用户授权

    ACTION_MANAGE_OVERLAY_PERMISSION
    

    解决了我的问题。之后,前台类被解雇了。

    Restrictions on starting activities from the background

    【讨论】:

    • 你说的是android 10+。我在奥利奥工作,所以没有意义。不管怎样,谢谢你的回答。
    • 在奥利奥中确实如此,您不需要此权限。您确定它适用于服务的上下文吗?在这种情况下,您可以考虑查看:developer.android.com/training/notify-user/navigation
    猜你喜欢
    • 1970-01-01
    • 2021-08-23
    • 2020-02-10
    • 1970-01-01
    • 2018-07-10
    • 1970-01-01
    • 2020-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多