Images in Notifications
If you need to display an image in a notification, you can use the BaseBitmapDataSubscriber for requesting a bitmap from the ImagePipeline. This is safe to be passed to a notification as the system will parcel it after the NotificationManager#notify method. This page explains a full sample on how to do this.
Step by step
First create an ImageRequest with the URI:
1
ImageRequest imageRequest = ImageRequest.fromUri("http://example.org/user/42/profile.jpg"));
Then create a DataSource and request the decoded image from the ImagePipeline:
1
2
ImagePipeline imagePipeline = Fresco.getImagePipeline();
DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline.fetchDecodedImage(imageRequest, null);
As a DataSource is similar to a Future, we need to add a DataSubscriber to handle the result. The BaseBitmapDataSubscriber abstracts some of the complexity away when dealing with Bitmap:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
dataSource.subscribe(
new BaseBitmapDataSubscriber() {
@Override
protected void onNewResultImpl(Bitmap bitmap) {
displayNotification(bitmap);
}
@Override
protected void onFailureImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
// In general, failing to fetch the image should not keep us from displaying the
// notification. We proceed without the bitmap.
displayNotification(null);
}
},
UiThreadImmediateExecutorService.getInstance());
}
The displayNotification(Bitmap) method then is similar to the ‘normal’ way to do this on Android:
1
2
3
4
5
6
7
8
9
10
11
12
13
private void displayNotification(@Nullable Bitmap bitmap) {
final NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(getContext())
.setSmallIcon(R.drawable.ic_done)
.setLargeIcon(bitmap)
.setContentTitle("Fresco Says Hello")
.setContentText("Notification Text ...");
final NotificationManager notificationManager =
(NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
}
Full Sample
For the full sample see the ImagePipelineNotificationFragment in the showcase app: ImagePipelineNotificationFragment.java
