gpt4 book ai didi

android - FirebaseMessagingService 仅在后台显示通知

转载 作者:行者123 更新时间:2023-11-29 19:05:59 30 4
gpt4 key购买 nike

FCM 消息可以包含datanotification 或两者之一。系统使用notification 生成的通知缺乏功能,所以我删除了notification 并只发送了data。因此,我必须自己创建通知。

创建和显示通知很容易,但我不想在我的应用程序(特别是 MainActivity,但它只有一个 Activity )已经在前台时显示通知。大多数应用在前台时不会显示通知。

如果我的应用程序不在前台,我如何在 onMessageReceived 中知道?

class MessagingService : FirebaseMessagingService()
{
override fun onMessageReceived(remoteMessage: RemoteMessage?)
{
// Check if message contains a data payload.
if (remoteMessage?.data?.isNotEmpty() == true)
{
//Log.d(TAG, "Message data payload: " + remoteMessage.data)

......

if "Only when my app is on the background or not running?"
sendNotification("Got a message.")
}
}

最佳答案

/**
* Method checks if the app is in background or not
*
* @param context Application context
* @return True/False
*/
public static boolean isAppIsInBackground(Context context) {
boolean isInBackground = true;
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
for (String activeProcess : processInfo.pkgList) {
if (activeProcess.equals(context.getPackageName())) {
isInBackground = false;
}
}
}
}
} else {
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
if (componentInfo.getPackageName().equals(context.getPackageName())) {
isInBackground = false;
}
}

return isInBackground;
}

关于android - FirebaseMessagingService 仅在后台显示通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47218387/

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