According to the code below, I create a notification which is expandable.
private void bannerNotif() {
Notification foregroundNote;
RemoteViews bigView = new RemoteViews(getApplicationContext()
.getPackageName(), R.layout.banner_notif);
Bitmap icon = BitmapFactory.decodeResource(getResources(),
R.drawable.featuredimagehandler);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
this);
foregroundNote = mNotifyBuilder.setContentTitle("Title")
.setContentText("Description")
.setSmallIcon(R.drawable.app_icon).setLargeIcon(icon).build();
foregroundNote.bigContentView = bigView;
NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyManager.notify(2, foregroundNote);
}
According to this document
A notification's big view appears only when the notification is expanded, which happens when the notification is at the top of the notification drawer, or when the user expands the notification with a gesture.
the problem is when this notification is placed on top, it is not expanded.
I had this problem and the issue was that the layout I created for the notification had a height of "MATCH_PARENT" so it was too big to fit. Setting the height to a reasonable value solved the issue and the notification expanded.
I also had something similar with remoteviews and notifications. Mine ended up not expanding because my notification_id was not unique throughout my app :)
Related
What I need
What I want to achieve is something like
where it should be possible to programmatically set the number displayed within the icon.
I.e. I want a notification action icon with a programmatically adjustable text.
Is there a simple way to achieve this? It does not seem to be possible to add a TextView to an android drawable and set the text when I'm building my notification. What other ways are there?
Code
The rough code for building my notification is (although I think it's not necessary for answering the question):
private void buildNotification() {
createNotificationChannel();
Intent playbackActionIntent = new Intent(this, MediaPlayerService.class);
playbackActionIntent.setAction(ACTION_BACKWARD);
PendingIntent backwardAction = PendingIntent.getService(this, 3, playbackActionIntent, 0);
String audioTitle = "testTitle";
String albumTitle = "testAlbumTitle";
// Create a new Notification
mNotificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
// Hide the timestamp
.setShowWhen(false)
// Set the Notification style
.setStyle(new android.support.v4.media.app.NotificationCompat.MediaStyle()
// Attach our MediaSession token
.setMediaSession(mediaSession.getSessionToken())
// Show our playback controls in the compat view
.setShowActionsInCompactView(0, 1, 2))
// Set the Notification color
.setColor(getResources().getColor(R.color.colorAccent))
.setSmallIcon(R.drawable.ic_notification_new)
// Set Notification content information
.setContentText(albumTitle)
.setContentTitle(audioTitle)
// Set the visibility for the lock screen
.setVisibility(VISIBILITY_PUBLIC)
// Add playback actions
.addAction(R.drawable.ic_media_backward, "backward", backwardAction)
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
notificationManager.notify(NOTIFICATION_ID, mNotificationBuilder.build());
}
}
So for R.drawable.ic_media_backward I want a drawable with an adjustable text as in the linked image.
Any help is appreciated! So far I could not find anything that would solve my problem.
I'm trying to display a notification with expanded layout on Lollipop, but i don't want the smallIcon to show up on the bottom right corner of the largeIcon as per the example below. notice that this only happens for the extended layout (i tried the solution proposed here, but it doesn't seem to work).
here is the code:
NotificationCompat.Builder notification = new NotificationCompat.Builder(context);
notification.setContentTitle(reader.getString(Constants.NOTIFICATION_CONFIG_KEY_TITLE));
notification.setContentText(reader.getString(Constants.NOTIFICATION_CONFIG_KEY_TEXT)) ;
notification.setAutoCancel(true) ;
notification.setSmallIcon(Constants.NOTIFICATION_CONFIG_ICON_ID);
notification.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), Constants.NOTIFICATION_CONFIG_ICON_ID));
Notification n = notification.build() ;
the smallIcon's visibility could probably be changed once the notification is built but i don't have a clear idea how. perhaps, someone could help with that.
Thanks.
You can achieve that by building both layouts (the short and the expand) by yourself.
First make 2 XML files and custom your layouts, then attach them to the notification. The root view of those layouts should be LinearLayout:
RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.notification_layout); // the small layout
RemoteViews notificationLayoutExpanded = new RemoteViews(getPackageName(), R.layout.notification_layout_large); // the expand layout
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, getString(R.string.notification_channel_id))
.setSmallIcon(R.drawable.logo)
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(notificationLayout)
.setCustomBigContentView(notificationLayoutExpanded);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(0, builder.build());
If you want to set a text or an image to those layouts dynamically (not in the XML), you do it like that:
RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.notification_layout);
String text = "the text to display";
Bitmap picture = BitmapFactory.decodeResource(getResources(), R.drawable.notification_default_img);;
notificationLayout.setTextViewText(R.id.small_notification_text, text);
notificationLayout.setImageViewBitmap(R.id.small_notification_img, picture);
I am creating an application with a notification at different times. First notification display fine in the notification bar with an icon, title and text. But when the second notification comes the title and text of the first notification gets cleared and the second one displays. For the third notification again the second one's title and text gets cleared and so on.
The code i am using is
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
long mNotificationId = System.currentTimeMillis();
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setSound(uri);
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
bigTextStyle.setBigContentTitle("Time to Sleep!!!");
bigTextStyle.bigText("A good laugh and a long sleep are the best cures in the doctor's book.");
mBuilder.setStyle(bigTextStyle);
mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify((int)mNotificationId, mBuilder.build());
Any help would be appreciated. Thanks in advance.
I think you are only setting text for BigTextStyle
Also set Values for small or default settings
for eg
// Small - default style
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(context.getApplicationInfo().icon)
.setWhen(System.currentTimeMillis())
.setContentTitle(extras.getString("title"))
.setTicker(extras.getString("title"))
.setContentIntent(contentIntent);
Just check the mNotificationId value if its same
If same you can use the following to generate the random number
Random random = new Random();
int m = random.nextInt(9999 - 1000) + 1000;
and then use this m
mNotifyMgr.notify(m, mBuilder.build());
Try using
int notification_id = (int) date.getTime();
to ensure that every time a unique id is generated. Notifications with unique ids do not get over written and you can see multiple notifications in the notification panel. Hope this helps.
Im trying to make my custom notification similar to this one
I found a bunch of similar questions here on SO and examples on the internet but no one helped me.
Here is my code:
RemoteViews bigView = new RemoteViews(mContext.getApplicationContext().getPackageName(), R.layout.notification_playback);
Intent i = new Intent(MainActivity.ACTION_PLAYER, null, mContext, MainActivity.class);
i.putExtra("CURRENT_TRACK", t);
i.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pi = PendingIntent.getActivity(mContext, 0, i, 0);
Notification n = new NotificationCompat.Builder(mContext)
.setContentTitle(t.getName())
.setContentText(t.getArtistName())
.setContentIntent(pi)
.setSmallIcon(R.drawable.ic_notification)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_launcher))
.setOngoing(true)
.setContent(bigView)
.setWhen(System.currentTimeMillis())
.build();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
n.bigContentView = bigView;
NotificationManager notifyMgr = (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notifyMgr.notify(0, n);
It appears in default size and become a size I expect only after long tap on it.
Is it possible to make it a big size immediately without long tap ? And how can I do it if so ?
Thanks!
So, what i figured out.
Notification appears expanded if it is it the to of notifications. Otherwise it appears wit regular height an user can drag it down to expand.
In order to make it appear in the top of notifications you can use .setPriority(Notification.PRIORITY_MAX) in Builder.
I am trying to create a notification with custom RemoteView.But when displaying notification,bottom of the RemoteView is getting cropped.Only half of the remote view is visible in the notification.Could any one tell me about increasing the height of notification in android?
First of all, Notification height is limited to 256dp on Android. Views will be clipped at the bottom that exceed this height. Second, to create a custom Big Notification layout, use RemoteViews like this:
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.ic_notification_icon)
RemoteViews content = new RemoteViews(this.getPackageName(), R.layout.content);
Notification notif = builder.build();
notif.bigContentView = content;
notificationManager.notify(NOTIFICATION_ID, notif);
Set the style of the notification intent to bigstyle,
Notification noti = new Notification.Builder(this).setStyle(new Notification.BigTextStyle().bigText(longText))