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)
Related
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 am trying to set color to my notification action btns with next code:
.setColor(Color.parseColor("#ff7900"))
NotificationCompat.Builder(service, CHANEL_ID)
.setColor(Color.parseColor("#ff7900"))
.setContentIntent(piClick)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setSmallIcon(R.drawable.ic_notification)
.setShowWhen(false)
here is result:
but the real color is different - "#d14d00".
any ideas why it is happen and how it can be fixed?
you can try with this
builder.setColor(ContextCompat.getColor(context, R.color.yourColor));
or try with this
builder.setColor(context.getResources().getColor(R.color.yourColor));
I am working on an Android app. This last makes use of a notification with a custom view that is displayed on the lock screen. Unfortunately, I am not able to get the ripple and elevation effect when I tap on it like other notifications. Also, a single touch trigger the intent I have configured whereas other notifications require double tap.
I have put a minimal project example on Github:
https://github.com/lpellegr/android-notification-custom-example
The app example offers two buttons to publish notifications: one that uses a custom view and suffer from the issues mentioned above and another notification that uses the default system view with the expected behaviour.
Any idea about how to get the ripple and elevation effect but also the double tap behaviour (by keeping the custom view) is welcome.
PS: I am targeting API 19+ and I want to use a custom view layout for the notification, along with setOnClickPendingIntent since only this listener allows to open an activity whatever the security mode of the device is.
Remove setOnClickPendingIntent from the method publishNotificationWithCustomView and add setContentIntent to the notification builder:
private void publishNotificationWithCustomView() {
String title = "Notification Custom View";
String content = "No ripple effect, no elevation, single tap trigger";
Context context = getApplicationContext();
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
.setWhen(System.currentTimeMillis())
.setDefaults(DEFAULT_ALL)
.setSmallIcon(R.mipmap.ic_launcher)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setOnlyAlertOnce(true)
.setAutoCancel(false)
.setColor(ContextCompat.getColor(context, R.color.colorAccent))
.setContentTitle(title)
.setContentText(content)
.setOngoing(true)
.setCategory(NotificationCompat.CATEGORY_ALARM)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentIntent(createLockscreenNotificationPendingIntent(context));
int notificationLayoutResId = R.layout.lock_screen_notification;
// using folder layout-vX is having issue with LG devices
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
notificationLayoutResId = R.layout.lock_screen_notification_android_n;
}
RemoteViews remoteView = new RemoteViews(
context.getPackageName(), notificationLayoutResId);
remoteView.setTextViewText(R.id.title, title);
remoteView.setTextViewText(R.id.text, content);
builder.setCustomContentView(remoteView);
Notification notification = builder.build();
publishNotification(context, notification, 7);
}
Then remove android:clickable="true" from lock_screen_notification.xml and lock_screen_notification_android_n.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="64dp">
....
i am trying to show a notification with more than 3 actions. Unfortunately, the forth action and so on are not showing (probably because there not enough space). Also, the action items does not have the same width.
Does anyone know how can i display more than 3 actions?
This is my code:
final NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle(message.getData().get(DATA_TITLE));
inboxStyle.addLine(message.getData().get(DATA_BODY));
final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notification)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setContentTitle(message.getData().get(DATA_TITLE))
.setContentText(message.getData().get(DATA_BODY))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setStyle(inboxStyle)
.setContentIntent(defaultIntent)
.setPriority(Notification.PRIORITY_MAX);
addActions(notificationBuilder, message);
private void addActions(final NotificationCompat.Builder builder, final RemoteMessage message) {
if (containsAction(message, EventActionType.OpenMessage)) {
builder.addAction(R.drawable.ic_email, "open", getActionIntent(message, MyActivity.class));
}
if (containsAction(message, EventActionType.Details)) {
builder.addAction(R.drawable.ic_notification_account, "details", getActionIntent(message, MyActivity.class));
}
if (containsAction(message, EventActionType.Transfer)) {
builder.addAction(R.drawable.ic_access_time, "transfer", getActionIntent(message, MyActivity.class));
}
This link can help you.
According to standard docs of android we can't have more than three actions for a notification.
If you want to have more than three actions , you can use remoteViews. Every notification will have a default view which is provided by android os, but we can customise it. To do that we need to create a layout will be used as a our notification view and use as many as buttons as you want in that layout, but height of the layout is limited as notification height is limited by android.Make sure all your buttons will fit in notification.Create a remoteViews with this layout.
After that when you are creating notification attach it to notification using setCustomContentView. For each button in the view you can have different pendingintent i.e on clicking on different buttons different pending intents can be executed. see RemoteView.setOnClickPendingIntent(view,pendingIntent).
Happy coding ;-)
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());