当应用未运行时,FCM显示默认消息而非自定义消息 [英] FCM shows default message than custom message when app is not running
问题描述
问题:我的FCM代码针对不同情况显示了不同的消息:
Problem: My FCM code shows different messages to different cases:
案例1:当应用运行时,它会显示自定义通知正文
Case 1: When app is running it shows the custom notification body
案例2:当应用在后台运行时,它会显示自定义通知正文
Case 2: When the app is running in the background it shows custom notification body
情况3:当应用未运行时,它显示从FCM接收到的默认消息比自定义消息
Case 3: When the app is not running it shows the default message received from FCM than the custom message
代码:
/* The class extends FirebaseMessagingService() */
override fun onMessageReceived(remoteMessage: RemoteMessage) {
try {
/* Some other codes */
val pendingIntent = PendingIntent.getActivity(this, 0, Intent(), PendingIntent.FLAG_ONE_SHOT)
val builder = NotificationCompat.Builder(this, "" + R.string.notification_channel_id)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentTitle(remoteMessage.notification!!.title)
.setContentText(getString(R.string.custom_message_body))
.setAutoCancel(true)
.setContentIntent(pendingIntent)
val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel("" + R.string.notification_channel_id, "Alert channel", NotificationManager.IMPORTANCE_DEFAULT)
manager.createNotificationChannel(channel)
}
manager.notify(0, builder.build())
/* super.onMessageReceived(remoteMessage) was REMOVED to prevent default functions */
} catch (e: Exception) {
e.printStackTrace()
}
}
推荐答案
这是预期的. FCM对于应用程序状态(前台和后台/已终止)具有不同的行为. 您应该根据用例,通过从服务器发送的有效负载来处理此问题.
This is expected. FCM has different behaviours for app status (foreground and background / killed). You should handle this by the payload you sent from server, according to your use case.
从服务器发送的味精必须以通知"或数据"格式从仪表板或服务器端api发送. 注意:在Firebase dashobard中,您只能发送通知"正文,而不能发送数据.在这种情况下,FCM将直接显示通知,而不会回调您的应用.
The msg sent from server has to be sent in either "notification" or "data" format, from dashboard or server side api. Note: From firebase dashobard you can only send "notification" body and not data. In such cases, FCM will directly display the notif without giving a callback to your app.
服务器端 以下是示例格式:
通知类型格式 注意:默认情况下,Android系统将在通知托盘中显示通知,而您无需显示它.
Notification Type Format Note : Android System will by default display the notification in the notification tray and you don't need to display it.
{
"to": "your_token_id",
"notification" : {
"title" : "FCM Notification title!",
"body" : "FCM Notification subtext!",
"content_available" : true,
"priority" : "high"
}
}
数据格式(用于在应用程序中接收前台和后台的回调) 注意:您必须自行处理回调并显示notif.
Data Format (For receiving callback in app, in foreground and background) Note : You have to handle callback and display notif on your own.
{
"to": "your_token_id",
"data" : {
"title" : "FCM Notification Title ",
"subtext" : "FCM Notification Sub Title",
"type" : "999",
"priority" : "high"
}
}
Android客户端 要处理您的Android接收器中收到的有效负载,请在此处
Android Client To handle the payload received in your Android receiver, checl the official guide here
/* The class extends FirebaseMessagingService() */
override fun onMessageReceived(remoteMessage: RemoteMessage) {
Log.d(TAG, "From: ${remoteMessage.from}")
// Check if message contains a data payload.
remoteMessage.data.isNotEmpty().let {
Log.d(TAG, "Message data payload: " + remoteMessage.data)
if (/* Check if data needs to be processed by long running job */ true) {
// For long-running tasks (10 seconds or more) use WorkManager.
scheduleJob()
} else {
// Handle message within 10 seconds
handleNow()
}
}
// Check if message contains a notification payload.
remoteMessage.notification?.let {
Log.d(TAG, "Message Notification Body: ${it.body}")
}
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
}
在此处
这篇关于当应用未运行时,FCM显示默认消息而非自定义消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!