首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >app在前台,如何让FCM显示通知?

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

Stack Overflow用户
提问于 2018-12-03 20:09:10
回答 2查看 4.1K关注 0票数 5

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

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

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

我尝试过:

代码语言:javascript
复制
@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能以某种方式做到这一点。我该怎么做呢?

EN

回答 2

Stack Overflow用户

发布于 2018-12-03 20:24:12

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

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

票数 2
EN

Stack Overflow用户

发布于 2018-12-03 20:30:38

在这种情况下,else部分需要由您处理以显示自定义通知,如下所示:

代码语言:javascript
复制
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无论如何都不会为你处理它,。我希望,这对你有帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53593580

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档