I want to show Big size image in notification bar for that I am using this code
I image come from the server.so inside AsyncTask convert that in bitmap and show in activity image view its working fine but when load the same bitmap in notification using notification Builder is show error
Bitmap img = BitmapFactory.decodeByteArray(result, 0, result.length);
imgView.setImageBitmap(img);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(ctx)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setContentTitle("hahahha").setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(img))
.setAutoCancel(true);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
01-19 12:28:32.556 1737-1737/? W/ProgressBarDelegate: Unknown Drawable subclass, src=android.graphics.drawable.ScaleDrawable@3c75db5
01-19 12:28:32.590 1737-1737/? E/StatusBar: couldn't inflate view for notification com.synergy.fcmdemo/0x0
android.widget.RemoteViews$ActionException: view: android.widget.FrameLayout doesn't have method: setEmphasizedMode(boolean)
android.widget.RemoteViews.getMethod(RemoteViews.java:851)
android.widget.RemoteViews.-wrap5(RemoteViews.java)
android.widget.RemoteViews$ReflectionAction.apply(RemoteViews.java:1411)
android.widget.RemoteViews.performApply(RemoteViews.java:3425)
android.widget.RemoteViews.apply(RemoteViews.java:3160)
com.android.systemui.statusbar.BaseStatusBar.inflateViews(BaseStatusBar.java:615)
com.android.systemui.statusbar.BaseStatusBar.addNotificationViews(BaseStatusBar.java:1036)
at com.android.systemui.statusbar.BaseStatusBar.addNotificationViews(BaseStatusBar.java:1015)
at com.android.systemui.statusbar.phone.PhoneStatusBar.addNotification(PhoneStatusBar.java:2088)
at com.android.systemui.statusbar.CommandQueue$H.handleMessage(CommandQueue.java:472)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6205)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
01-19 12:28:32.591 1737-1737/? E/StatusBar: Couldn't expand RemoteViews for: StatusBarNotification(pkg=com.synergy.fcmdemo user=UserHandle{0} id=0 tag=null key=0|com.synergy.fcmdemo|0|null|10137: Notification(pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x10 color=0x00000000 vis=PRIVATE))
01-19 12:28:32.594 1737-1737/? D/StatusBar: onNotificationRemoved: Key: 0|com.synergy.fcmdemo|0|null|10137
You get the image from api response and convert into Bitmap
, but in your notification builder added image from drawable, just change this
Bitmap img = BitmapFactory.decodeByteArray(result, 0, result.length);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(ctx)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(img)
.setContentTitle("hahahha").setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(img))
.setAutoCancel(true);
Now for big picture you have to set style
Notification notif = new Notification.Builder(context)
.setContentTitle("hahahha")
.setContentText("content")
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(bitmap)
.setStyle(new Notification.BigPictureStyle()
.bigPicture(img)
.setBigContentTitle("hahahha"))
.build();
Try his, working for me
Get image from server using Thread
new Thread(new Runnable() {
@Override
public void run() {
try {
showBigNotification();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
Sending notification
private void showBigNotification() {
Bitmap bitmap = getBitmapFromURL(**Your image url**);
Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle();
bigPictureStyle.setBigContentTitle("title");
bigPictureStyle.setSummaryText("message");
bigPictureStyle.bigPicture(bitmap);
Intent notificationIntent = new Intent(mContext, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(mContext,
101, notificationIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManager nm = (NotificationManager) mContext
.getSystemService(Context.NOTIFICATION_SERVICE);
Resources res = mContext.getResources();
Notification.Builder builder = new Notification.Builder(mContext);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher))
.setTicker(res.getString(R.string.app_name))
.setStyle(bigPictureStyle)
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle("Title")
.setContentText("Message");
Notification n = builder.build();
if (nm != null) {
nm.notify(1001, n);
}
}
Getting image from server
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();
return BitmapFactory.decodeStream(input);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
User contributions licensed under CC BY-SA 3.0