繁体   English   中英

即使app在前台,如何让FCM显示通知?

[英]How to let FCM display notification even when app is in foreground?

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

即使app位于前台,我如何强制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以某种方式做到这一点。 我怎样才能做到这一点?

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

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

在此,您需要处理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无论如何都不会为你处理它 我希望这可以帮助你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM