-
-
Save flasher297/5e6b1d863a0d822431e65c8bb8a18082 to your computer and use it in GitHub Desktop.
Android Big Picture notification with picasso
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/***************************************** | |
* | |
* 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); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment