问题:我的 FCM 代码针对不同的情况显示不同的消息:
案例 1:当应用程序运行时,它会显示自定义通知正文
案例 2:当应用程序在后台运行时,它会显示自定义通知正文
案例 3:当应用程序未运行时,它显示从 FCM 收到的默认消息而不是自定义消息
代码:
/* 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()
}
}

