如何让 FCM 在应用程序在前台时显示通知?

6

如果不是数据消息,让 FCM 处理设备上的通知。

我如何强制 Android 使用 FCM 显示通知,即使应用程序在前台?我不想构建自己的通知。

我向我的应用程序发送两种类型的消息:数据消息和普通通知消息。数据消息在 onMessageReceived() 中处理,无论应用程序处于后台、前台还是被杀死,都可以正常处理。但现在我也通过 Firebase 控制台发送普通通知消息,当应用程序在后台时它们会自动显示,但当应用程序在前台时,会调用 onMessageReceived()。在这里,我需要以某种方式告诉 FCM 显示内容,而无需我自己构建通知。

我尝试了以下方法:

@Override public void onMessageReceived(RemoteMessage remoteMessage) {
        if(remoteMessage.getData() == null || !remoteMessage.getData().containsKey("specificKey")) {
            // notification msg, let FCM handle it
            super.onMessageReceived(remoteMessage); // this is not working - I want FCM to show notification the same as if the app was in background. 
            return;
        } else {
          // data message, I'm handling it on my own and showing a custom notification, working fine and well
        }
}

但这是我通过代码自己处理的,我希望FCM能以某种方式来完成它。我该怎么做呢?


1
我相信这是Firebase的一般行为。当应用程序在前台时,您必须创建通知。 - Ali Ahmed
2个回答

2

当应用程序不在前台时,通知消息由系统处理。这是定义的行为,无法更改。

如果您想在应用程序不在前台时控制显示什么内容,则需要发送数据消息。


正好相反 - 我希望FCM无论应用程序是在后台还是前台都能处理通知消息。我只想处理数据消息。 - c0dehunter
啊,明白了。我其实也不认为这是可能的。你所能做的就是展示一个看起来和系统生成的通知一样的自己的通知。 - Frank van Puffelen

0
在这里,else 部分需要由您处理,以显示自定义通知如下:
class FCMMessagingService: FirebaseMessagingService() {
    var dataMap: Map<String, String>? = null
    var body: String = ""
    var title: String = ""

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

        dataMap = remoteMessage?.data
        Log.d("Data Map", dataMap.toString())

        try {
            val jsonObject = JSONObject(dataMap?.get("data")!!)
            val contentInfo = jsonObject.get("contentInfo") as JSONObject

            val time = contentInfo.getString("time")
            var message = jsonObject.getString("message")
            message = message.replace("<time>", Utils.getTimeFromTimestamp(time.toLong(), true))
            body = message
        } catch (e:JSONException) {
            e.printStackTrace()
        }

        title = dataMap?.get("title")!!
        if(Foreground.get().foreground) {
            sendBroadCast(title , body)
        } else {
            createNotification(title, body)
        }
    }

    private fun createNotification(title: String, body: String) {
        val intent = Intent(this, DashBoardActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)

        val pendingIntent = PendingIntent.getActivity(this, Calendar.getInstance().timeInMillis.toInt(), intent,
                PendingIntent.FLAG_ONE_SHOT)

        val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
        val notification = NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.noti)
                .setContentTitle(title)
                .setContentText(body)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent)
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            notification.color = resources.getColor(R.color.toolBarColor)
        }

        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.notify(Calendar.getInstance().timeInMillis.toInt(), notification.build())
    }

    private fun sendBroadCast(title: String, body: String) {
        val broadCastIntent = Intent(Constant.NOTIFICATION)
        broadCastIntent.putExtra("title", title)
        broadCastIntent.putExtra("body", body)
        LocalBroadcastManager.getInstance(this).sendBroadcast(broadCastIntent)
        // val intent = Intent(this, DashBoardActivity::class.java)
        // intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
    }
}

我相信你只能这样做,因为FCM不会为你处理它。希望这可以帮到你。


1
谢谢你的回答,但我知道如何显示通知。我只是懒得想让 FCM 生成通知,如果它们不是数据消息,则无论应用程序状态(bg、fg、killed)。 - c0dehunter
1
我相信,这不能使用 FCM 完成。只有通过编写代码自己处理通知才能实现。 - Nitin Gurbani

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接