如何使用图像 URL 作为通知图像而不是来自可绘制对象的图像?

How to use an image URL as the image for notifications instead of images from the drawable?

我在图像来自可绘制对象的通知中使用以下代码。但是,我想使用图像 URL 来使用我在 Firebase 存储中的图像。我怎样才能做到这一点。我尝试添加 URL,但出现错误。

val bitmap = BitmapFactory.decodeResource(applicationContext.resources,R.drawable.product_image)
    val bitmapLargeIcon = BitmapFactory.decodeResource(applicationContext.resources,R.drawable.comp_logo)

这是完整的代码。

    private fun sendNotification(title: String, message: String) {

    val intent: Intent = Intent(this, SplashActivity::class.java).apply {
        flags=Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
    }
    val pendingIntent: PendingIntent = PendingIntent.getActivity(this, 0,intent, 0)

    val bitmap = BitmapFactory.decodeResource(applicationContext.resources,R.drawable.product_image)
    val bitmapLargeIcon = BitmapFactory.decodeResource(applicationContext.resources,R.drawable.comp_logo)

    val builder = NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.product_image)
        .setContentTitle(title)
        .setContentText(message)
        .setLargeIcon(bitmapLargeIcon)
        .setStyle(NotificationCompat.BigPictureStyle().bigPicture(bitmap))
        .setContentIntent(pendingIntent)
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)


    with(NotificationManagerCompat.from(this)) {
        notify(notificationId, builder.build())
    }
}

你可以试试这个:

package com.tutorialsbuzz.notificationimgload

import android.app.NotificationChannel
import android.app.NotificationManager
import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.os.Build
import android.os.Bundle
import android.view.View
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import java.util.concurrent.atomic.AtomicInteger

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val notifyBtn = findViewById<Button>(R.id.notifyBtn)
        notifyBtn.setOnClickListener { v: View? ->
            createNotification(
                "Hey Hello!!",
                "How are you?",
                resources.getString(R.string.notification_channel)
            )
        }
    }

    private fun createNotification(
        title: String, content: String,
        channedId: String
    ) {
        val notificationBuilder = NotificationCompat.Builder(applicationContext, channedId)
            .setSmallIcon(R.drawable.ic_notifications_active)
            .setAutoCancel(true)
            .setLights(Color.BLUE, 500, 500)
            .setVibrate(longArrayOf(500, 500, 500))
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setContentTitle(title)
            .setContentText(content)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)


        // Since android Oreo notification channel is needed.
        val notificationManager = NotificationManagerCompat.from(this@MainActivity)

        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(
                channedId,
                channedId,
                NotificationManager.IMPORTANCE_HIGH
            )
            channel.lockscreenVisibility = NotificationCompat.VISIBILITY_PUBLIC
            notificationManager.createNotificationChannel(channel)
        }
        val imageUrl =
            "https://i.pinimg.com/originals/1b/7e/4e/1b7e4eac8558ad54d6fe94ed4e14cb84.jpg"
        Glide.with(applicationContext)
            .asBitmap()
            .load(imageUrl)
            .into(object : CustomTarget<Bitmap>() {
                override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                    notificationBuilder.setLargeIcon(resource) //largeIcon
                    notificationBuilder.setStyle(
                        NotificationCompat.BigPictureStyle().bigPicture(resource)
                    ) //Big Picture
                    val notification = notificationBuilder.build()
                    notificationManager.notify(NotificationID.iD, notification)
                }

                override fun onLoadCleared(placeholder: Drawable?) {}
                override fun onLoadFailed(errorDrawable: Drawable?) {
                    super.onLoadFailed(errorDrawable)
                }
            })
    }

    internal object NotificationID {
        private val c = AtomicInteger(100)
        val iD: Int
            get() = c.incrementAndGet()
    }
}