Android - Use External Profile Image in Notification Bar Like Facebook

Android - use external profile image in notification bar like Facebook

This statement will use a method to convert a URL (naturally, one that points to an image) into a Bitmap.

Bitmap bitmap = getBitmapFromURL("https://graph.facebook.com/YOUR_USER_ID/picture?type=large");

Note: Since you mention a Facebook profile, I have included an URL that gets your the large size profile picture of a Facebook User. You can however, change this to any URL that points to an image that you need to display in the Notification.

And the method that will get the image from the URL you specified in the statement above:

public Bitmap getBitmapFromURL(String strURL) {
try {
URL url = new URL(strURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

Now pass the bitmap instance created above to the Notification.Builder instance. I call it builder in this example code. It is used in this line: builder.setLargeIcon(bitmap);. I am assuming you know how to display the actual Notification and it's configurations. So I will skip that part and add just the builder.

// CONSTRUCT THE NOTIFICATION DETAILS
builder.setAutoCancel(true);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentTitle("Some Title");
builder.setContentText("Some Content Text");
builder.setLargeIcon(bitmap);
builder.setContentIntent(pendingIntent);

Oh, almost forgot, if you haven't already done so, you will need this permission setup in the Manifest:

<uses-permission android:name="android.permission.INTERNET" />

Load image from url in notification Android

Changed my code as below and its working now :

private class sendNotification extends AsyncTask<String, Void, Bitmap> {

Context ctx;
String message;

public sendNotification(Context context) {
super();
this.ctx = context;
}

@Override
protected Bitmap doInBackground(String... params) {

InputStream in;
message = params[0] + params[1];
try {

URL url = new URL(params[2]);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.connect();
in = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(in);
return myBitmap;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Bitmap result) {

super.onPostExecute(result);
try {
NotificationManager notificationManager = (NotificationManager) ctx
.getSystemService(Context.NOTIFICATION_SERVICE);

Intent intent = new Intent(ctx, NotificationsActivity.class);
intent.putExtra("isFromBadge", false);

Notification notification = new Notification.Builder(ctx)
.setContentTitle(
ctx.getResources().getString(R.string.app_name))
.setContentText(message)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(result).build();

// hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;

notificationManager.notify(1, notification);

} catch (Exception e) {
e.printStackTrace();
}
}
}

How to set a profile picture if it exists using Glide, otherwise use a placeholder?

The issue here is that you're attempting to pass something which is not a String (@drawable.placeholder) to your binding adapter. If you replace it with a String, you will see that the error resolves.

My advice would be instead of doing the logic outside of the binding adapter, why not do it inside? Something like this:

    @BindingAdapter("android:profileImage")
fun setProfileImage(imageView: ImageView, profileImage: String?) {
if (profileImage != null) {
Glide.with(imageView.context).load(profileImage)
.placeholder(R.drawable.placeholder)
.into(imageView)
} else {
imageView.setImageDrawable(R.drawable.placeholder)
}
}

Now, your XML declaration can simply be:

android:profileImage="@{user.profileImage}"

Additionally, Glide has a .error() call you can put onto the builder. I am not certain if it works with null or what triggers it, but you could give it a shot. It would look something like this, then:

        @BindingAdapter("android:profileImage")
fun setProfileImage(imageView: ImageView, profileImage: String?) {
Glide.with(imageView.context).load(profileImage)
.placeholder(R.drawable.placeholder)
.error(R.drawable.placeholder)
.into(imageView)
}


Related Topics



Leave a reply



Submit