Search code examples
androidnotificationsalarmmanagerpicassointentservice

can't get bitmap of image using Picasso target on intentservice when app is not running


this code is for show bigpicture style notification when alarm manager triggers alarm and the bigpicture image have to download from internet using picasso and intentservice even when the app is not running in foreground or background but working fine when app is running in foreground or background

i got notification with image downloaded from internet when app is in foreground and also in background Check the video for understanding the problem please mute audio because its very noisy

https://www.youtube.com/watch?v=ID3PLuBvaRE&t=35s

class ShowNotificationFromAlarm : IntentService("ShowNotificationFromAlarm") {

    var target = object : Target {
        override fun onPrepareLoad(placeHolderDrawable: Drawable?) {
            Log.d("alarmnoti", "onprepareload")
        }

        override fun onBitmapFailed(errorDrawable: Drawable?) {
            Log.d("alarmnoti", "bitmapfailed")
        }

        override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) {
            Log.d("alarmnoti", "bitmaploaded")
            val builder = Notification.Builder(this@ShowNotificationFromAlarm)
                    .setSmallIcon(R.drawable.ic_share)
                    .setContentTitle([email protected](R.string.app_name))
                    .setContentText("this is text")
                    .setLargeIcon(bitmap)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setPriority(Notification.PRIORITY_HIGH) //must give priority to High, Max which will considered as heads-up notification
                    .setStyle(Notification.BigPictureStyle()
                            .setBigContentTitle("this is big content title")
                            .setSummaryText("this is summary text")
                            .bigPicture(bitmap)
                    )

            //.addAction(action)
            val notification = builder.build()
            NotificationManagerCompat.from(this@ShowNotificationFromAlarm).notify(100, notification)
        }
    }

    override fun onHandleIntent(intent: Intent?) {    
        Log.d(this.packageName + "\t intentservice", "intent started")

        if (intent != null) {
            val uiHandler = Handler(Looper.getMainLooper())
            uiHandler.post(Runnable {
                shownotification([email protected], intent)
            })
        }
    }

    private fun shownotification(context: Context, intent: Intent) {    
        val intent = Intent(context, MainActivity::class.java)
        val pIntent = PendingIntent.getActivity(context, System.currentTimeMillis().toInt(), intent, 0)    
        Picasso.with(context)
                .load("https://cartodb-basemaps-d.global.ssl.fastly.net/dark_nolabels/3/3/4.png")
                //.load(context.filesDir.absolutePath + File.pathSeparator+"logo-2.png")
                .into(target)
    }
}

Solution

  • Since you are using IntentService which is already runs on separate thread.You can directly download image instead of pushing the downloading task into other thread. See the code below.

    public class DownloadClass extends IntentService{
    public DownloadClass(String name) {
        super(name);
    }
    
    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        String url =intent.getStringExtra("url");
        Bitmap bitmap=downloadImageBitmap(url);
        if(bitmap!=null){
            // Use bitmap here
        }
    }
    
    private Bitmap downloadImageBitmap(String sUrl) {
        Bitmap bitmap = null;
        try {
            InputStream inputStream = new URL(sUrl).openStream();   // Download Image from URL
            bitmap = BitmapFactory.decodeStream(inputStream);       // Decode Bitmap
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }
    }