In my android application, i want to set Notification icons dynamically which will be loaded from URL. For that, i have used setLargeIcon
property of Notificati
Since I couldn't find any working solution for Picasso I'm posting my complete and working(July 2020) example using Picasso below.
It is sending the notification immediately and then updates it when the image for setLargeIcon() has been loaded. Normally this is very quick and the user should only see the updated version of the notification in most cases.
private void sendNotification(String message, String title, final String photoUrl) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
final NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.wbib_transp_512)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setContentIntent(pendingIntent);
final NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
final Handler uiHandler = new Handler(Looper.getMainLooper());
uiHandler.post(new Runnable() {
@Override
public void run() {
Picasso.get()
.load(photoUrl)
.resize(200, 200)
.into(new Target() {
@Override
public void onBitmapLoaded(final Bitmap bitmap, final Picasso.LoadedFrom from) {
notificationBuilder.setLargeIcon(bitmap);
notificationManager.notify(0, notificationBuilder.build());
}
@Override
public void onBitmapFailed(Exception e, final Drawable errorDrawable) {
// Do nothing?
}
@Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
// Do nothing?
}
});
}
});
}