Search code examples
androidservicefirebase-cloud-messaging

Can't download image to show in firebase push notification service


I am trying to show an image in my push notification but it fails at first push sent in my Xiaomi and it is never shown in a Samsung device. I tried many solutions:

  1. I tried using different classes to download images ( URL, COIL, etc )
  2. Using workmanager
  3. Creating a service after workmanager to download the image in a separate thread
  4. I thought the problem was the low wifi connection

But none of these worked :(
How can I show an image in my push notification?


Solution

  • After days of trying to fix this I could get a main error. In my attempt when used a service, I got an error that said "Could not download the file, Try using the main thread", so I launched the download in a different thread that targeted Main and it worked.

    First I use Thread class, like this:

    val hilo = Thread(){
     try{
    
     }catch{
    
     }
    }
    hilo.start()
    

    But this code looked so Java, although it worked perfect but another concern was how the thread was shut down ?
    So I wasn't satisfied with this solution so after searching I found this article: https://www.kodeco.com/20123726-android-services-getting-started when in a service ( as in FirebaseMessagingCloud class) the author used CoroutineScope to run a thread. So, I implemented it and it worked perfect as Thread but it can be cancelled in the Destroy method of the Service and futhermore, this was fully kotlin style to handle threads in a Service or in another context.