【问题标题】:How to let FCM display notification even when app is in foreground?即使应用程序在前台,如何让 FCM 显示通知?
【发布时间】:2018-12-03 12:09:09
【问题描述】:

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

如何强制 Android 使用 FCM 显示通知,即使应用程序处于前台也是如此?我不想建立自己的通知。

我正在向我的应用程序发送两种类型的消息:数据消息和普通通知消息。数据消息在onMessageReceived() 中处理,无论应用程序是在后台还是前台还是被杀死,这是OK。但现在我也通过 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 以某种方式做到这一点。我该怎么做?

【问题讨论】:

  • 我相信这是 Firebase 的一般行为。当应用程序处于前台时,您必须创建通知

标签: android firebase-cloud-messaging android-notifications


【解决方案1】:

当应用不在前台时,通知消息由系统处理。这是根据定义,没有办法改变行为。

如果您想控制应用不在前台时显示的内容,则必须发送数据消息。

【讨论】:

  • 反之亦然 - 无论应用程序是后台还是前台,我都希望 FCM 处理通知消息。我只想处理数据消息。
  • 啊,明白了。我其实也不认为这是可能的。您所能做的就是显示您自己的通知,该通知看起来与系统生成的通知相同。
【解决方案2】:

在此,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 无论如何都不会为您处理它。希望对您有所帮助。

【讨论】:

  • 感谢您的回答,但我知道如何显示通知。我只是懒惰,希望 FCM 生成通知,如果它们不是数据消息,无论应用程序状态如何(bg,fg,killed)。
  • 我相信,使用 FCM 无法做到这一点。只有当您准备好通过一些代码自己处理通知时,才能做到这一点。
猜你喜欢
  • 2022-01-06
  • 1970-01-01
  • 1970-01-01
  • 2018-11-08
  • 1970-01-01
  • 2022-01-01
  • 1970-01-01
  • 2019-02-25
  • 2022-12-22
相关资源
最近更新 更多