Firebase通知未显示在前台

问题描述 投票:0回答:2

我正在尝试管理通知,但是当应用程序处于前台时,我无法对其进行任何操作。

最小化或完全关闭应用程序时,通知会正确显示,但是如果应用程序处于前台,则看不到它。

我对此代码进行了测试,但是没有任何效果。关闭或最小化应用程序的通知无需修改此代码或完全修改此代码即可:

import android.app.Notification
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Looper
import android.util.Log
import androidx.appcompat.app.AlertDialog
import androidx.core.app.NotificationCompat
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import PACKAGE_NAME.ContenedorPrincipal
import PACKAGE_NAME.R
import PACKAGE_NAME.general.General
import java.text.SimpleDateFormat
import java.util.*
//import java.util.concurrent.atomic.AtomicInteger

class ServicioNotificaciones: FirebaseMessagingService()
{
    //private val c = AtomicInteger(0)

    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        super.onMessageReceived(remoteMessage)

        try{
            val mNotificationManager: NotificationManager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            val mBuilder: NotificationCompat.Builder = NotificationCompat.Builder(applicationContext, "notify_001")
            val ii = Intent(applicationContext, ContenedorPrincipal::class.java)
            val pendingIntent = PendingIntent.getActivity(applicationContext, 0, ii, 0)

            val bigText = NotificationCompat.BigTextStyle()
            bigText.bigText(remoteMessage.notification?.body ?: "")
            bigText.setBigContentTitle(remoteMessage.notification?.title ?: "")
            bigText.setSummaryText(remoteMessage.notification?.body ?: "")

            mBuilder.setContentIntent(pendingIntent)
            mBuilder.setSmallIcon(R.mipmap.ic_launcher_round)
            mBuilder.setContentTitle(remoteMessage.notification?.title ?: "")
            mBuilder.setContentText(remoteMessage.notification?.body ?: "")
            @Suppress("DEPRECATION")
            mBuilder.priority = Notification.PRIORITY_MAX
            mBuilder.setStyle(bigText)

            val buildedNotification = mBuilder.build()

            //mNotificationManager.notify(c.incrementAndGet(), buildedNotification)
            mNotificationManager.notify(Integer.parseInt(SimpleDateFormat("ddHHmmss",  Locale.getDefault()).format(Date())), buildedNotification)

            /*Looper.prepare()
            General.mostrarConfirmacion(
                remoteMessage.notification?.title ?: "",
                remoteMessage.notification?.body ?: "",
                AlertDialog.Builder(this)
            )*/
        }
        catch (ex: Exception){
            Log.wtf("onMessageReceivedEX", ex.message.toString())
        }
    }
}

和清单:

   <service
            android:name="PACKAGE_NAME.servicios.ServicioNotificaciones"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
    </service>
android firebase kotlin notifications foreground
2个回答
0
投票

我看不到您在哪里创建通知通道(因为必须安装Android O,所以需要。)>

Create and Manage Notification Channels


0
投票

您必须使用通知渠道:

© www.soinside.com 2019 - 2024. All rights reserved.