I have a notification of battery level. I want to make different icon in status bar and notifications center. In status bar: number (2 digits). In body: my app icon.
How can I do that? The app icon is also in the status bar, how can I change that?
I'm not sure what your code looks like right now but I'd do something like this:
Notification.Builder nb = new Notification.Builder(context)
.setContentTitle("title")
.setContentText("content")
.setAutoCancel(true)
.setLargeIcon(R.drawable.large_icon)
.setSmallIcon(R.drawable.small_icon)
.setTicker("ticker text");
NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(100, nb.build());
Note the setLargeIcon() and setSmallIcon(). The icon you want to show in the 'ticker' should be set in setSmallIcon() and for what you have called the 'notification centre', you should use setLargeIcon(). That should work.
Related
I'm seeing some apps showing background color in whole notification.
Myntra Notification with BackgroundColor
First Cry Notification with BackgroundColor
I tried solutions from these links but nothing worked.
Changing Notification RemoteViews Background Color
https://cazimirroman.medium.com/android-how-to-set-the-background-color-for-a-notification-in-a-foreground-service-eaa505e2b82d
Tried using DecoratedMediaCustomViewStyle ->
val mediaSession = MediaSessionCompat(context,"tag")
mediaSession.setFlags(0)
mediaSession.setPlaybackState(PlaybackStateCompat.Builder()
.setState(PlaybackStateCompat.STATE_NONE,0,0f)
.build())
val builder = NotificationCompat.Builder(context, "channelId1")
.setSmallIcon(R.drawable.ic_notification_small)
.setContentTitle("The Title")
.setPriority(NotificationCompat.PRIORITY_MAX)
.setWhen(System.currentTimeMillis())
.setOngoing(true)
.setAutoCancel(true)
.setColor(ContextCompat.getColor(context, R.color.pink))
.setColorized(true)
.setStyle(androidx.media.app.NotificationCompat.DecoratedMediaCustomViewStyle().setMediaSession(mediaSession.sessionToken))
In this case notification is coming black always (suppose to be pink) and also doesn't show all the information.
Notification using MediaStyle
You should create two custom layouts for your notifications, one for a collapsed notification and one for an expanded notification, then in your activity:
RemoteView collapsedRV = new RemoteViews (getPackageName(), R.layout.collapsed_notification)
RemoteView expandedRV = new RemoteViews (getPackageName(), R.layout.expanded_notification)
then in your builder you need to set the content view like this:
.setCustomContentView(collapsedRV)
.setCutomBigContentView(expandedRV)
.build()
notificationManager.notify(1,builder)
I would like to make the notification look a different color completely, I am using local notifications so I would like to be able to do this with NotificationCompat.Builder if possible.
Here's an example of what I would like the notification to look like:
Using notification builder methods
NotificationCompat.Builder(context, channelId)
.setColor(ContextCompat.getColor(this, R.color.primaryColor))
.setColorized(true)
.....
.build()
I use below code to display notifications in my Android apps. I can display it but both "small-Icon" and "large-Icon" are incorrect.They are not even launcher-icon.! I don't know Android where does they get from !!!!
My device is xiaomi-mi5s, is it related to my device? although other apps like Gmail show correct notifications.
I tested notifications in API 15,17,26 emulators and everything
NotificationCompat.Builder builder = new android.support.v4.app.NotificationCompat.Builder(context, PRIMARY_CHANNEL)
.setSmallIcon(R.drawable.ic_stat_name)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
.setContentIntent(getPendingIntent(context))
.setContentTitle("This is dummy title")
.setContentText("This is dummy body text")
.setSound(getDefaultSoundUri())
.setCategory("My_Category")
.setDeleteIntent(getOnDismissedPendingIntent(context));
NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= 26) {
NotificationChannel chan1 = new NotificationChannel(PRIMARY_CHANNEL,
PRIMARY_CHANNEL, NotificationManager.IMPORTANCE_DEFAULT);
chan1.setLightColor(Color.BLUE);
chan1.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
manager.createNotificationChannel(chan1);
}
manager.notify(12121, builder.build());
First of all, Large icon and Small icon can be different.
setSmallIcon(): sets an icon that should be displayed at status bar when notification was received. If setLargeIcon() was not called in the builder, large icon will use small icon drawable. Icon should have size of 24dp.
setLargeIcon(): sets an icon that should be displayed at notification in notification list, that can be opened by user by vertical dragging of status bar.
!!! Since API 21, Android requires you to use only single-colour icons with transparency for small icons (e.g. filled contour of Android with only white colour and transparent background).
As a Large icon, you can use icon of any colours, there are no restrictions.
On Android 6 I'm trying to display a notification with the following combination of properties:
text title and content
expandable image content (see here and here)
don't show a notification icon in the status bar (don't want to clutter it up)
don't flash any LEDs (don't want to trouble user)
show on lockscreen
show on lockscreen in expanded (or at least expandable) format (showing the bigContentView)
I can achieve 3 and 4 by setPriority(Notification.PRIORITY_MIN) but then the notification doesn't seem to show on the lock screen at all (fail on 5).
As for 6, when the notification does shown on the lockscreen e.g. using PRIORITY_MAX (passing on 5 but failing on 3 and 4), it is not expanded or even expandable (failing on 6).
I'm using the following to set up the notification:
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle(titleText)
.setContentText(contentText)
.setSmallIcon(R.drawable.small_icon)
.setOngoing(true)
.setPriority(Notification.PRIORITY_DEFAULT)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.build();
// add the image content (via a remoteViews)...
notification.bigContentView = remoteViews;
notificationManager.notify(tag, id, notification);
.setContentView(RemoteViews views)
RemoteViews
- A class that describes a view hierarchy that can be displayed in another process. The hierarchy is inflated from a layout resource file, and this class provides some basic operations for modifying the content of the inflated hierarchy.
Edit:
Call .build() later on.
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle(titleText)
.setContentText(contentText)
.setSmallIcon(R.drawable.small_icon)
.setOngoing(true)
.setPriority(Notification.PRIORITY_DEFAULT)
.setVisibility(Notification.VISIBILITY_PUBLIC);
// add the image content (via a remoteViews)...
notification.bigContentView = remoteViews;
notificationManager.notify(tag, id, notification.build());
I am creating an application, I am able to display notification properly, but small icon is not getting displayed as I have mentioned it in the drawable folder, The icon is getting masked with white color. Can any one help me, how can I get the icon to display properly.
Below is my notification code:
nb = new NotificationCompat.Builder(context)
.setContentTitle(contentTitle)
.setContentText(contentText)
.setSmallIcon(icon)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.micon_notification))
.setWhen(when)
.setContentIntent(contentIntent)
.setAutoCancel(true)
.setOnlyAlertOnce(true)
.setTicker(tickerText)
.setColor(Color.RED);
The icon mentioned in drawable is as shown below:
[1]: http://i.stack.imgur.com/ggYCY.png
That complete red color present inside the image is getting vanished and icon is getting displayed with complete white color. All suggestions are welcome.
This is the code Android uses to display notification icons:
if (entry.targetSdk >= Build.VERSION_CODES.LOLLIPOP) {
entry.icon.setColorFilter(mContext.getResources().getColor(android.R.color.white));
} else {
entry.icon.setColorFilter(null);
}
For that you've to make icon like Silhouette and make some section Transparent wherever you wants to add your Colors.
You can add your color using
.setColor(your_color_resource_here)
NOTE : setColor is only available in Lollipop so, you've to check OSVersion
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Notification notification = new Notification.Builder(context)
...
} else {
// Lollipop specific setColor method goes here.
Notification notification = new Notification.Builder(context)
...
notification.setColor(your_color)
...
}
Look at the documentation: http://developer.android.com/design/style/iconography.html
there are words:
"Notification icons must be entirely white. Also, the system may scale
down and/or darken the icons."
I hope it helps!