mostashaar
mostashaar

Reputation: 83

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?

Upvotes: 1

Views: 1030

Answers (2)

Illegal Argument
Illegal Argument

Reputation: 10338

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());
}

Upvotes: 1

pjco
pjco

Reputation: 3836

Because notifications in Android are RemoteViews, you can't update the content directly.

Instead you have 2 choices:

1) Download the image before calling mNotificationManager.notify(); as said in Illegal Argument's comment to your question.

2) Save the id number and call mNotificationManager.notify(id, notification) again with a new notification when the image has downloaded. This will make it appear as though there is a "new" notification (the ticker text will scroll by again, etc).

Upvotes: 0

Related Questions