gpt4 book ai didi

java - 当应用程序被终止时,FCM onMessageReceived 不会触发(Android Oreo)

转载 作者:行者123 更新时间:2023-12-01 18:09:20 25 4
gpt4 key购买 nike

我正在开发一个应用程序,该应用程序需要在发送 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

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

Restrictions on starting activities from the background

关于java - 当应用程序被终止时,FCM onMessageReceived 不会触发(Android Oreo),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60494937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com