title: (Android) Is there any way to show AlertDialog when firebase cloud message received (even when app is backgorund)?
body: <p>I want to show an AlertDialog when the Firebase cloud message received (and either the Android application is foreground or background).</p>
  <p>In forground, the app shows the AlertDialog, but in background, the app doesn't show the AlertDialog.</p>
  <p><strong>MyFirebaseMessageService.kt</strong></p>
  <pre><code>class MyFirebaseMessageService : FirebaseMessagingService() {
  private val TAG = &quot;fcm_tag&quot;
  
  override fun onMessageReceived(remoteMessage: RemoteMessage) {
      val title = remoteMessage.notification?.title
      val body = remoteMessage.notification?.body
  
      Log.d(TAG, &quot;fcm title: $title&quot;)
      Log.d(TAG, &quot;fcm body: $body&quot;)
  
      val alertDialogIntent = Intent(baseContext, AlertDialogActivity::class.java)
      alertDialogIntent.putExtra(&quot;title&quot;, title)
      alertDialogIntent.putExtra(&quot;body&quot;, body)
  
      try {
          PendingIntent.getActivity(
              baseContext,
              0,
              alertDialogIntent,
              PendingIntent.FLAG_IMMUTABLE
          ).send()
      } catch (e: Exception) {
          Log.d(TAG, &quot;fcm error: $e&quot;)
      }
  }
  }
  </code></pre>
  <p><strong>AlertDialogActivity.kt</strong></p>
  <pre><code>class AlertDialogActivity : Activity() {
  override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      val bund = intent.extras
      val title = bund?.getString(&quot;title&quot;)
      val body = bund?.getString(&quot;body&quot;)
  
      AlertDialog.Builder(this).run {
          setTitle(title)
          setMessage(body)
          setPositiveButton(&quot;OK&quot;) { _, _ -&gt;
              finish()
          }
          setCancelable(false)
          show()
      }
  }
  }
  </code></pre>
  <p><strong>AndroidManifest.xml</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      package=&quot;com.myproject&quot;&gt;
      &lt;uses-permission android:name=&quot;android.permission.FOREGROUND_SERVICE&quot; /&gt;
  
      &lt;application
          android:allowBackup=&quot;true&quot;
          android:icon=&quot;@mipmap/ic_launcher&quot;
          android:label=&quot;@string/app_name&quot;
          android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
          android:supportsRtl=&quot;true&quot;
          android:theme=&quot;@style/Theme.MyProject&quot;&gt;
  
          &lt;service
              android:name=&quot;.MyFirebaseMessageService&quot;
              android:enabled=&quot;true&quot;
              android:exported=&quot;true&quot;&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name=&quot;com.google.firebase.MESSAGING_EVENT&quot; /&gt;
              &lt;/intent-filter&gt;
          &lt;/service&gt;
  
          &lt;activity
              android:name=&quot;.AlertDialogActivity&quot;
              android:exported=&quot;true&quot;
              android:theme=&quot;@style/Theme.AppCompat.DayNight.Dialog&quot;&gt;
          &lt;/activity&gt;
  
          &lt;activity
              android:name=&quot;.MainActivity&quot;
              android:exported=&quot;true&quot;
              android:label=&quot;@string/app_name&quot;
              android:theme=&quot;@style/Theme.MyProject&quot;&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
                  &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
  
          &lt;meta-data
              android:name=&quot;com.google.firebase.messaging.default_notification_channel_id&quot;
              android:value=&quot;fcm_default_channel&quot; /&gt;
          &lt;meta-data
              android:name=&quot;com.google.firebase.messaging.default_notification_icon&quot;
              android:resource=&quot;@drawable/ic_launcher_foreground&quot; /&gt;
      &lt;/application&gt;
  &lt;/manifest&gt;
  </code></pre>