如何以一种风格创建长文本和图片的推送通知

问题描述 投票:0回答:1

如何创建推送通知 在一种通知样式中包含长文本和图片?

我使用

BigTextStyle
显示推送通知, 因为我需要显示大文本,但是如何添加图像?

或者我需要使用一些自定义通知?

一些例子:

enter image description here

android notifications styles push
1个回答
0
投票

我希望这对您有帮助。如果您在这方面发现困难,请告诉我

private fun showBigNotification(
    bitmap: Bitmap,
    mBuilder: NotificationCompat.Builder,
    icon: Int,
    title: String,
    message: String,
    resultPendingIntent: PendingIntent,
    alarmSound: Uri
) {


    val CHANNEL_ID = "123456789"
    val name: CharSequence = mContext.getString(R.string.app_name)
    val importance = NotificationManager.IMPORTANCE_HIGH
    val inboxStyle = NotificationCompat.InboxStyle()
    val notificationManager =
        mContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    val audioAttributes = AudioAttributes.Builder()
        .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
        .setUsage(AudioAttributes.USAGE_ALARM)
        .build()
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        @SuppressLint("WrongConstant") val mChannel =
            NotificationChannel(CHANNEL_ID, name, importance)
        mChannel.setSound(alarmSound, audioAttributes)
        mChannel.enableLights(true)
        mChannel.enableVibration(true)
        notificationManager.createNotificationChannel(mChannel)
    }


    val bigPictureStyle = NotificationCompat.BigPictureStyle()
    bigPictureStyle.setBigContentTitle(title)
    bigPictureStyle.setSummaryText(Html.fromHtml(message).toString())
    bigPictureStyle.bigPicture(getResizedBitmap(bitmap, bitmap.width))
    val notification: Notification
    val notificationBuilder = NotificationCompat.Builder(
        mContext, CHANNEL_ID
    )/*
    notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
        */
        .setAutoCancel(true)
        .setContentTitle(title)
        .setContentIntent(resultPendingIntent)
        .setSound(alarmSound)
        .setStyle(bigPictureStyle)
        .setSmallIcon(R.drawable.ic_stat_name)
        .setLargeIcon(BitmapFactory.decodeResource(mContext.resources, icon))
        .setContentText(message)

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        notificationBuilder.setSmallIcon(R.drawable.ic_stat_name)
        notificationBuilder.color = mContext.resources.getColor(R.color.colorPrimary)
    } else {
        notificationBuilder.setSmallIcon(icon)
    }
    val id = Random(System.currentTimeMillis()).nextInt(1000)
    notificationManager.notify(id, notificationBuilder.build())

}
© www.soinside.com 2019 - 2024. All rights reserved.