[英]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.