嗨,自定义FCM通知已停止在Android上显示。他们以前也工作过,但在我更新了所有gradle库之后,通知就不再显示在设计上了。
代码使用断点执行,不会显示任何错误,但不会显示通知。
我对如何调查此事一无所知。
任何指针都会非常有用。
这是一个代码,应该显示具有两种布局的自定义通知,用于大小通知。
private fun showCustomPushNotification(message: RemoteMessage, chargerId: String) {
val notificationLayout = RemoteViews(
packageName,
R.layout.pgin_requires_approval_notification_small
)
val notificationLayoutExpanded = RemoteViews(
packageName,
R.layout.pgin_requires_approval_notification_large
)
val title = message.data[MSG_TITLE]
val subTitle = message.data[MSG_SUB_TITLE]
notificationLayout.setTextViewText(R.id.tvTitle, title)
notificationLayout.setTextViewText(R.id.tvSubTitle, subTitle)
notificationLayoutExpanded.setTextViewText(R.id.tvTitle, title)
notificationLayoutExpanded.setTextViewText(R.id.tvSubTitle, subTitle)
val intent = Intent(this, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
val pendingIntent: PendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)
val customNotification = NotificationCompat.Builder(
this,
CarInfoProcessingService.APPROVE_EACH_PGIN_NOTIFICATION_CHANNEL_ID
)
.setSmallIcon(R.mipmap.ic_launcher)
.setStyle(NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(notificationLayout)
.setCustomBigContentView(notificationLayoutExpanded)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_MAX)
.build()
val approveIntent = Intent(this, CustomNotificationListener::class.java)
approveIntent.putExtra("onClickListener", "approve")
approveIntent.putExtra("chargerId", chargerId)
val pendingApproveIntent = PendingIntent.getBroadcast(
this,
0,
approveIntent,
PendingIntent.FLAG_IMMUTABLE
)
notificationLayoutExpanded.setOnClickPendingIntent(R.id.btnApprove, pendingApproveIntent)
val denyIntent = Intent(this, CustomNotificationListener::class.java)
denyIntent.putExtra("onClickListener", "deny")
denyIntent.putExtra("chargerId", chargerId)
val pendingDenyIntent = PendingIntent.getBroadcast(
this,
1,
denyIntent,
PendingIntent.FLAG_IMMUTABLE
)
notificationLayoutExpanded.setOnClickPendingIntent(R.id.btnDeny, pendingDenyIntent)
val notificationManager =
getSystemService(NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(CustomNotificationListener.WHAT_NOTIFICATION_ID, customNotification)
}
我能想到的唯一变化是逐步更新库,但由于它没有显示任何错误,我一无所知。
感谢您提前提供的帮助。R
你说在更新之前它是工作的。因此,在更新后,我认为您需要检查代码的版本:
定义您的待定意图,如下所示:
val pendingIntent: PendingIntent? = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
PendingIntent.getActivity(applicationContext, 0, intent, PendingIntent.FLAG_IMMUTABLE)
} else {
PendingIntent.getActivity(applicationContext, 0, intent, PendingIntent.FLAG_ONE_SHOT)
}
你可能想为你的通知设置优先级,Build.VERSION_CODES.O及以上版本会忽略这一点,但较旧的设备会使用这个:
customNotification.priority = NotificationCompat.PRIORITY_MAX
对于Build.VERSION_CODES.O及以上版本,需要通知通道:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
customNotification.setChannelId(Constants.NOTIFICATION_CHANNEL)
val channel =
NotificationChannel(
Constants.NOTIFICATION_CHANNEL,
Constants.NOTIFICATION_NAME,
NotificationManager.IMPORTANCE_HIGH
)
notificationManager.createNotificationChannel(channel)
}
对于我使用的constns来说,它们只是字符串:
const val NOTIFICATION_NAME = "YourApp"
const val NOTIFICATION_CHANNEL = "YourApp_channel_01"
只需更改您的代码,它就会工作。如果你做不到,请告诉我。