Skip to content

Instantly share code, notes, and snippets.

@h4h13
Last active September 3, 2018 20:10
Show Gist options
  • Save h4h13/c680be1dcc742ad30f14785555fe67eb to your computer and use it in GitHub Desktop.
Save h4h13/c680be1dcc742ad30f14785555fe67eb to your computer and use it in GitHub Desktop.
Android Big Picture notification with picasso
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import com.squareup.picasso.Picasso;
import java.io.IOException;
/**
* Created by BlackFootSanji on 9/22/2016.
*/
public class NotificationHelper {
private final Context context;
public NotificationHelper(Context context) {
this.context = context;
}
public void showNotification(String title, String message, int smallIcon) {
try {
final NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentTitle(title)
.setContentText(message)
.setSmallIcon(smallIcon)
.setContentIntent(
PendingIntent.getActivity(
context,
0,
new Intent(context, MainActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT)
)
//here comes to load image by Picasso
//it should be inside try block
.setLargeIcon(Picasso.with(context).load("URL_TO_LOAD_LARGE_ICON").get())
//BigPicture Style
.setStyle(new NotificationCompat.BigPictureStyle()
//This one is same as large icon but it wont show when its expanded that's why we again setting
.bigLargeIcon(Picasso.with(context).load("URL_TO_LOAD_LARGE_ICON").get())
//This is Big Banner image
.bigPicture(Picasso.with(context).load("URL_TO_LOAD_BANNER_IMAGE").get())
//When Notification expanded title and content text
.setBigContentTitle(title)
.setSummaryText(message)
);
//extra you can add actions
manager.notify(1029, builder.build());
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*****************************************
*
* To update images
*
* ***************************************/
final Notification notification = builder.build();
final int notificationId = new Random().nextInt(200);
manager.notify(notificationId, notification);
final RemoteViews contentView = notification.contentView;
final RemoteViews bigContentView = notification.bigContentView;
final int iconId = android.R.id.icon;
final int bigIconId = context.getResources().getIdentifier("android:id/big_picture", null, null);
// To push notification from background thread
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
Picasso.with(context).load("URL").into(contentView, iconId, notificationId, notification);
Picasso.with(context).load("BANNER_IMAGE_URL").into(bigContentView, bigIconId, notificationId, notification);
}
});
@maelfosso
Copy link

Hi,

Please, how to use it with custom layout ?

@rajanbhavsar001
Copy link

Yes Can you please illustrate how to used this ?

Copy link

ghost commented Aug 4, 2017

your showNotification() method executes a network call on the same thread:
Picasso.with(context).load("URL_TO_LOAD_LARGE_ICON").get()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment