Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to use Picasso in notification (icon)

I'm looking for an easy way to use Picasso to load a noticiation icon (which is a URL on a remote webpage). In a previous version of the app I'm working on this code seemed to work:

        Bitmap speakerPic = null;
        try {
            speakerPic = new AsyncTask<Void, Void, Bitmap>() {
                @Override
                protected Bitmap doInBackground(Void... params) {
                    try {
                        return Picasso.with(c).load(session.getSpeaker().getPhotoUrl()).get();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return null;
                }
            }.execute().get(1500, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }

        if (speakerPic != null) {
            builder.setLargeIcon(speakerPic);
        } else {
            builder.setLargeIcon(BitmapFactory.decodeResource(c.getResources(), R.drawable.ic_launcher));
        }

But now I get an TimeOutException every time (and I fallback to a default icon in my res folder). I have to use this AsyncTask because Picasso (/network) may not happen on the UI thread. (although I'm blocking the UI thread for 1.5sec here..).

I know Picasso can handle remoteviews, but I don't want to use a custom view for my notificiation. Also I couldn't find a way to get the RemoteView for the NoticifationIcon.

Is there a way to set the icon of my notification simply using Picasso?

like image 462
PieterAelse Avatar asked Nov 12 '14 13:11

PieterAelse


2 Answers

I'll answer the question myself because I've found a decent way, using Picasso and RemoteViews. Tested and working with Picasso 2.5.2 :

// Default stuff; making and showing notification
final Context context = getApplicationContext();
final NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
final Notification notification = new NotificationCompat.Builder(context)
        .setSmallIcon(R.mipmap.ic_launcher) // Needed for the notification to work/show!!
        .setContentTitle("Title of notification")
        .setContentText("This is the description of the notification")
        // Uncomment if you want to load a big picture
        //.setStyle(new NotificationCompat.BigPictureStyle())
        .build();
final int notifId = 1337;
notificationManager.notify(notifId, notification);

// Get RemoteView and id's needed
final RemoteViews contentView = notification.contentView;
final int iconId = android.R.id.icon;

// Uncomment for BigPictureStyle, Requires API 16!
//final RemoteViews bigContentView = notification.bigContentView;
//final int bigIconId = getResources().getIdentifier("android:id/big_picture", null, null);

// Use Picasso with RemoteViews to load image into a notification
Picasso.with(getApplicationContext()).load("http://i.stack.imgur.com/CE5lz.png").into(contentView, iconId, notifId, notification);

// Uncomment for BigPictureStyle
//Picasso.with(getApplicationContext()).load("http://i.stack.imgur.com/CE5lz.png").into(bigContentView, iconId, notifId, notification);
//Picasso.with(getApplicationContext()).load("http://i.stack.imgur.com/CE5lz.png").into(bigContentView, bigIconId, notifId, notification);
like image 181
PieterAelse Avatar answered Sep 22 '22 10:09

PieterAelse


I suggest a simplest way to integrate a remote picture inside your notification as large icon with Picasso.

// your notification builder
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(getString(R.string.app_name))
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

String picture = "http://i.stack.imgur.com/CE5lz.png"; 
Bitmap bmp = Picasso.with(getApplicationContext()).load(picture).get();

notificationBuilder.setLargeIcon(bmp);

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
like image 35
tryp Avatar answered Sep 24 '22 10:09

tryp