Search code examples
androidandroid-notificationsuniversal-image-loader

Displaying notification with external image link using ImageLoader


I'm trying to display notification with external image link using ImageLoader but without any luck. Logcat doesn't show any errors and the notifications doesn't appear at all.

This is my code:

void shownot()
{
    mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
    .setLargeIcon(getSingleItemLargeIcon(IMAGE_LINK_URL))
    .setTicker("test test test test test test")
    .setContentTitle("test test test test test test")
    .setAutoCancel(true)
    .setContentText("test test test test test test");

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(1, mBuilder.build());
}



private Bitmap getSingleItemLargeIcon(String imageUri) {
    ImageLoader imageLoader = ImageLoader.getInstance();
    imageLoader.init(config);

    imageLoader.loadImage(imageUri, new ImageLoadingListener() {

    @Override
    public void onLoadingStarted(String arg0, View arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onLoadingFailed(String arg0, View arg1, FailReason arg2) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onLoadingComplete(String arg0, View arg1, Bitmap arg2) {
        bitmap = arg2;
    }

    @Override
    public void onLoadingCancelled(String arg0, View arg1) {
        // TODO Auto-generated method stub

    }
});

return bitmap;
}

Did i miss something?


Solution

  • Finally figured it out. It seems like you need to set setSmallIcon() to in the notification for notification to show:

    void shownot() {
        NotificationManager mNotificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Intent intent = new Intent(this, PhotoIntentActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                intent, 0);
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
                R.drawable.microphone);// download this bitmap from internet
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setLargeIcon(bitmap).setSmallIcon(R.drawable.icon)
                // this is also required
                .setTicker("+1 notification").setContentTitle("Awsome Title")
                .setAutoCancel(true).setContentText("Awsome Text");
    
        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(1, mBuilder.build());
    }