简体   繁体   English

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

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

Let FCM handle notifications on device, if it's not a data message. 如果FCM不是数据消息,让FCM处理设备上的通知。

How can I force Android to show notification using FCM, even when app is in foreground? 即使app位于前台,我如何强制Android使用FCM显示通知? I don't want to build my own notification. 我不想建立自己的通知。

I am sending two types of messages to my app: data message and normal notification message. 我正在向我的应用发送两种类型的消息:数据消息和正常通知消息。 Data messages are handled in onMessageReceived() , no matter if the App is in background or foreground or killed which is OK . 数据消息在onMessageReceived()中处理,无论应用程序是在后台还是前台或被杀,都可以 But now I am also sending normal notification messages through Firebase Console, and they are automatically displayed when app is in background but when app is in foreground, onMessageReceived() is called. 但是现在我也通过Firebase控制台发送正常的通知消息,当应用程序处于后台时它们会自动显示,但当应用程序位于前台时,会调用onMessageReceived() Here, I would need to somehow tell FCM to show the contents without me having to build the notification. 在这里,我需要以某种方式告诉FCM显示内容,而无需我构建通知。

I tried with: 我尝试过:

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

But this is my own handling via code and I want FCM to do that somehow. 但这是我自己通过代码处理的问题,我希望FCM以某种方式做到这一点。 How can I do that? 我怎样才能做到这一点?

When the app is not in the foreground, notification messages are handled by the system. 当应用程序不在前台时,系统会处理通知消息。 This is by definition and there is no way to change the behavior. 这是根据定义,没有办法改变行为。

If you want to control what is displayed while the app is not in the foreground, you will have to send a data message. 如果要控制应用程序不在前台时显示的内容,则必须发送数据消息。

In this, the else part needs to be handled by you to show a custom notification as follows: 在此,您需要处理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)
    }
}

I believe you will have to do it this way only as FCM won't handle it anyhow for you . 我相信你必须这样做,因为FCM无论如何都不会为你处理它 I hope, this helps you. 我希望这可以帮助你。

暂无
暂无

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

相关问题 当应用程序在前台而不是后台时,FCM通知,通知单击 - FCM notification, notification click when app is in foreground not background FCM:当应用程序处于前台状态时处理其应用程序的不同数据通知 - FCM : Handling different data notification of an android app when app is in foreground 在前台在后台收到FCM通知时应用崩溃 - App Crashes when receiving FCM Notification on Foreground works on Background 应用程序在前台时无法处理 FCM 通知 - Can't handdle FCM notification when app in foreground 当我的应用程序前台服务被终止时,未收到 FCM 通知 - FCM Notification is not received when my app foreground service is killed 我正在使用 FCM。当应用程序处于前台而不是状态栏时,如何在警报对话框或自定义 Toast 中显示通知标题? - I am using FCM.how can i display notification tittle in alert dialogue or custom Toast when app is in foreground rather than on status bar? 当应用程序在前台使用 Firebase 时不显示通知 - Notification not display when app in foreground using Firebase Android FCM:当应用程序在后台时 FCM 显示(通知)消息登录 - Android FCM: FCM display(notification) message loggin when app is in background 无论应用是后台还是前台,均未收到FCM通知 - FCM notification not received whether the app in background or foreground FCM:我已使用FCM接收有关我的应用程序的通知。 当我的应用程序处于前台时,它将所有通知分组,但不将其组合到后台时 - FCM: I have used FCM to receive notification for my app. When my app is in foreground it groups all notifications but not when app is in background
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM