使用后台服务的自定义视图显示通知

时间:2019-07-24 07:53:11

标签: android kotlin firebase-cloud-messaging

我的应用程序应显示带有RTL文本的通知。某些设备显示不正确-LTR。我决定使用自定义视图来通知我,以便可以轻松对齐任何RTL文本。如果应用程序处于打开状态,则显示带有自定义视图的通知。如果最小化或关闭它,则使用标准的推送通知映射。对于通知,我使用了Firebase服务。无论哪种方式,都会执行onMessageReceived重写方法。 enter image description here

val notificationLayout = RemoteViews(packageName, R.layout.notification)
notificationLayout.setTextViewText(R.id.notification_title, pushTitle)
notificationLayout.setTextViewText(R.id.notification_body, pushMessage)
notificationLayout.setOnClickPendingIntent(R.id.notification_root, pendingIntent)

val builder = NotificationCompat.Builder(context, channelId)
        .setSmallIcon(R.drawable.ic_icon)
        .setStyle(NotificationCompat.DecoratedCustomViewStyle())
        .setCustomContentView(notificationLayout)

if (bitmap != null) {
    val notificationLayoutExpanded = RemoteViews(packageName, R.layout.notification_expanded)

    notificationLayoutExpanded.setTextViewText(R.id.notification_title_ex, pushTitle)
    notificationLayoutExpanded.setTextViewText(R.id.notification_body_ex, pushMessage)
    notificationLayoutExpanded.setBitmap(R.id.img, "setImageBitmap", bitmap)
    notificationLayoutExpanded.setOnClickPendingIntent(R.id.notification_root_ex, pendingIntent)

    builder.setCustomBigContententer code hereView(notificationLayoutExpanded)
}

notificationManager.notify(Random().nextInt(), builder.build())

0 个答案:

没有答案