0

I am using the following code with the notification where the images are from the drawable. But, I want to use the image URL to use the images that I have in my Firebase Storage. How can I do that. I tried adding the URL and that gives me an error.

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

This is the complete code.

    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())
    }
}
Codist
  • 737
  • 8
  • 23

1 Answers1

3

enter image description hereYou can try this:

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()
    }
}
rNkL
  • 376
  • 3
  • 11
  • Seems it will work. I wanted the code for `Kotlin`. When I copy and paste the code, Android Studio doesn't convert it. Can you give code for `Kotlin? – Codist Aug 07 '21 at 09:09
  • I already have `Glide` but I do not know how to use it with notification. What goes into `.into()`? The link and the code you have provided is for `JAVA` and I am not able to covert it into `Kotlin` – Codist Aug 07 '21 at 09:50
  • I have made changes and converted the code in kotlin. you can check it now – rNkL Aug 07 '21 at 10:09
  • 1
    It's awesome. It works fine. Anybody who wants to implement this in Kotlin can use it. – Codist Aug 07 '21 at 11:16
  • Is it possible to send a notification to a specific user or a group of users with this? – Codist Aug 09 '21 at 13:57
  • 1
    yes it is possible to send a notification to the specific user, for that you have to pass the token of that user from Firebase. – rNkL Aug 09 '21 at 13:58
  • I want to develop something like this, when a user places an order, another specific user (the seller) should get an intimation and show a notification symbol/badge in the Order Received tab. Is it usually done using this notification method or something else? Thanks for your time. – Codist Aug 09 '21 at 14:21
  • for that you have to write code from backend side to send notification at some action done from user side. – rNkL Aug 10 '21 at 06:10
  • I just noticed that I receive the notification only on the device from which I am sending the notification. I do not receive it on other devices. Do you think I messed up, could you help me fix it? I have been trying to find what went wrong but I couldn't find anything. Recently I created a new Firebase project and the app is connected to it but there is no issue with the Firestore database, would that have any impact on this? – Codist Aug 19 '21 at 07:40
  • You want to send the notification to the particular user right? – rNkL Aug 19 '21 at 09:58
  • That is another requirement and I shall post that as a separate question, regarding sending to a specific user or specific group. But now what I am asking is sending notifications to everyone. – Codist Aug 19 '21 at 10:16
  • The issue is, I tried sending notifications from two different devices and I receive the notification only on the device from which I send the notification. I do not receive it on the other device. Do you think I messed up the code, if so, could you help me find where since I couldn't find it? Thanks. – Codist Aug 19 '21 at 10:41
  • Are you using fcm to send notifications? – rNkL Aug 19 '21 at 10:54
  • this code is only for showing notification with dynamic image url – rNkL Aug 19 '21 at 11:03
  • OK, let me do some research on how to send notifications using fcm with dynamic image URL. Thanks – Codist Aug 19 '21 at 11:17
  • 1
    This site can helps you :: https://firebase.google.com/docs/cloud-messaging/android/send-image – rNkL Aug 19 '21 at 11:23
  • Can you check the question I posted. https://stackoverflow.com/q/68598466/13935956 – Codist Aug 19 '21 at 14:11
  • Can you help me implement the same with the FCM, I have already created the code for sending notifications using fcm, only that I am not able to implement this with my new code. Here is my new question https://stackoverflow.com/q/68903345/13935956 – Codist Aug 24 '21 at 07:24