【问题标题】:Firebase notifications not shown on foregroundFirebase 通知未在前台显示
【发布时间】:2020-02-14 07:16:54
【问题描述】:

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

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

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

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

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())

谢谢大家!

【问题讨论】:

    标签: android firebase kotlin firebase-cloud-messaging


    【解决方案1】:

    你必须使用通知渠道:

    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)
    }
    

    【讨论】:

    • 谢谢,但它不起作用:((我正在尝试在此处包含代码...)
    • 你在日志中获取任何异常吗?
    • 没有,我一步步调试,没发现异常。
    • 能否请您也发布通知有效负载?
    • 什么是通知负载?我通过 Firebase 控制台发送通知。
    【解决方案2】:

    我不知道你在哪里创建通知通道(因为 Android O 是必须的)。

    Create and Manage Notification Channels

    【讨论】:

    • Ì 没有。但是我按照您的链接步骤和@Md 添加了这个。 Asaduzzaman 的迹象,但对我不起作用。
    【解决方案3】:

    我认为这个问题与发送通知有关,不接收通知尝试发送这样的通知

    "data": {
      "data_title": "test",
      "data_body" : "test"
     }
    

    【讨论】:

    • 不,它没有。总是收到通知,只是它没有显示在前台。我终于修好了。非常感谢!
    • @msolla 你能解释一下你是如何解决这个问题的
    • @Saurabh 我用对我有用的修复程序编辑了原始问题,请查看 EDIT 2 :)(请原谅响应缓慢)
    猜你喜欢
    • 2019-08-05
    • 1970-01-01
    • 2020-04-26
    • 2022-01-03
    • 2021-07-27
    • 2022-01-06
    • 2019-06-24
    • 2020-08-09
    • 1970-01-01
    相关资源
    最近更新 更多