android - Firebase 通知未在前台显示

标签 android firebase kotlin firebase-cloud-messaging

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

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

我有此代码的测试变体,但没有任何效果。应用程序关闭或最小化的通知适用于此代码的任何修改,或根本没有此代码:

(根据评论指示编辑,结果相同)

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 uniqueID = Integer.parseInt(SimpleDateFormat("ddHHmmss",  Locale.getDefault()).format(Date()))

            val mNotificationManager: NotificationManager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val notificationChannel = NotificationChannel(
                    "notificationChannelID",
                    "notificationChannelName",
                    NotificationManager.IMPORTANCE_HIGH)

                mNotificationManager.createNotificationChannel(notificationChannel)
            }


            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(uniqueID, 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>

编辑2:
我最终使它与此代码一起使用:
                val intent2 = Intent(this, MainActivity::class.java)
            intent2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
            val pendingintent2 = PendingIntent.getActivity(this, 0, intent2, PendingIntent.FLAG_ONE_SHOT)
            val channelId = "Default"
            val builder = NotificationCompat.Builder(this, channelId)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(remoteMessage.notification?.title)
                .setContentText(remoteMessage.notification?.body).setAutoCancel(true)
                .setContentIntent(pendingintent2)
            val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val channel = NotificationChannel(
                    channelId,
                    "Default channel",
                    NotificationManager.IMPORTANCE_DEFAULT
                )
                manager.createNotificationChannel(channel)
            }
            manager.notify(uniqueID, builder.build())

谢谢大家!

最佳答案

您必须使用通知 channel :

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    val notificationChannel = NotificationChannel(
        "notify_001",
        getString(R.string.notification_channel_name),
        NotificationManager.IMPORTANCE_HIGH)

    mNotificationManager.createNotificationChannel(notificationChannel)
}

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

相关文章:

node.js - 使用 Angular 和 Firebase 获取 uid 并迭代对象数组

android - 如何使用 Flutter 将整数存储到 Firestore

javascript - 如何在 FireBase 和 Angular 中根据用户隐藏元素

android - 可空接收器类型 bundle 的不安全使用? android 应用程序将在编译时出现警告但立即崩溃

java - 传递给另一个 Activity

android - 捕获我自己的 Android 应用程序发送和接收的数据包的最佳方法是什么?

android - Facebook API 异常 "This link could not be posted."

Android 布局 - 如何实现固定/卡住的标题和列

java - Spring Boot-如何通过实现BeforeAllCallback的自定义扩展类设置或覆盖application.yml中定义的属性?

postgresql - Kotliquery 不会关闭 postgresql 连接