(Android)有什么方法可以在收到Firebase云消息时显示AlertDialog(即使应用程序处于后台)?

发布于 2025-01-12 22:09:55 字数 3471 浏览 0 评论 0原文

我想在收到 Firebase 云消息时显示 AlertDialog(Android 应用程序处于前台或后台)。

在前台,应用程序显示AlertDialog,但在后台,应用程序不显示AlertDialog。

MyFirebaseMessageService.kt

class MyFirebaseMessageService : FirebaseMessagingService() {
private val TAG = "fcm_tag"

override fun onMessageReceived(remoteMessage: RemoteMessage) {
    val title = remoteMessage.notification?.title
    val body = remoteMessage.notification?.body

    Log.d(TAG, "fcm title: $title")
    Log.d(TAG, "fcm body: $body")

    val alertDialogIntent = Intent(baseContext, AlertDialogActivity::class.java)
    alertDialogIntent.putExtra("title", title)
    alertDialogIntent.putExtra("body", body)

    try {
        PendingIntent.getActivity(
            baseContext,
            0,
            alertDialogIntent,
            PendingIntent.FLAG_IMMUTABLE
        ).send()
    } catch (e: Exception) {
        Log.d(TAG, "fcm error: $e")
    }
}
}

AlertDialogActivity.kt

class AlertDialogActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    val bund = intent.extras
    val title = bund?.getString("title")
    val body = bund?.getString("body")

    AlertDialog.Builder(this).run {
        setTitle(title)
        setMessage(body)
        setPositiveButton("OK") { _, _ ->
            finish()
        }
        setCancelable(false)
        show()
    }
}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.myproject">
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyProject">

        <service
            android:name=".MyFirebaseMessageService"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

        <activity
            android:name=".AlertDialogActivity"
            android:exported="true"
            android:theme="@style/Theme.AppCompat.DayNight.Dialog">
        </activity>

        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:label="@string/app_name"
            android:theme="@style/Theme.MyProject">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="fcm_default_channel" />
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/ic_launcher_foreground" />
    </application>
</manifest>

I want to show an AlertDialog when the Firebase cloud message received (and either the Android application is foreground or background).

In forground, the app shows the AlertDialog, but in background, the app doesn't show the AlertDialog.

MyFirebaseMessageService.kt

class MyFirebaseMessageService : FirebaseMessagingService() {
private val TAG = "fcm_tag"

override fun onMessageReceived(remoteMessage: RemoteMessage) {
    val title = remoteMessage.notification?.title
    val body = remoteMessage.notification?.body

    Log.d(TAG, "fcm title: $title")
    Log.d(TAG, "fcm body: $body")

    val alertDialogIntent = Intent(baseContext, AlertDialogActivity::class.java)
    alertDialogIntent.putExtra("title", title)
    alertDialogIntent.putExtra("body", body)

    try {
        PendingIntent.getActivity(
            baseContext,
            0,
            alertDialogIntent,
            PendingIntent.FLAG_IMMUTABLE
        ).send()
    } catch (e: Exception) {
        Log.d(TAG, "fcm error: $e")
    }
}
}

AlertDialogActivity.kt

class AlertDialogActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    val bund = intent.extras
    val title = bund?.getString("title")
    val body = bund?.getString("body")

    AlertDialog.Builder(this).run {
        setTitle(title)
        setMessage(body)
        setPositiveButton("OK") { _, _ ->
            finish()
        }
        setCancelable(false)
        show()
    }
}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.myproject">
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyProject">

        <service
            android:name=".MyFirebaseMessageService"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

        <activity
            android:name=".AlertDialogActivity"
            android:exported="true"
            android:theme="@style/Theme.AppCompat.DayNight.Dialog">
        </activity>

        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:label="@string/app_name"
            android:theme="@style/Theme.MyProject">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="fcm_default_channel" />
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/ic_launcher_foreground" />
    </application>
</manifest>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白日梦 2025-01-19 22:09:55
AlertDialog alertDialog = new AlertDialog.Builder(this)
                    .setTitle("Title")
                    .setMessage("Are you sure?")
                    .create();

alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alertDialog.show();

并请求此许可

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
AlertDialog alertDialog = new AlertDialog.Builder(this)
                    .setTitle("Title")
                    .setMessage("Are you sure?")
                    .create();

alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alertDialog.show();

And ask for this permission

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文