how to remove time counter from notification? - android

How to remove 'now' and the drop-down arrow from notification layout in my app. I am using a single layout for notification and yet it is expanding. I kept the height to 64dp and 50dp but no change in the size
// Get the layouts to use in the custom notification
RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.notification_small);
//RemoteViews notificationLayoutExpanded = new RemoteViews(getPackageName(), R.layout.notification_large);
notificationLayout.setTextViewText(R.id.otpText,otp);
// Apply the layouts to the notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
builder.setSmallIcon(R.drawable.ic_launcher_foreground)
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(notificationLayout)
.setPriority(PRIORITY_MAX)
.setDefaults(DEFAULT_SOUND)
.setColor(getResources().getColor(R.color.colorAccent))
.setDefaults(DEFAULT_VIBRATE)
.setContentIntent(pendingIntent);

just use
builder.setSmallIcon(R.drawable.ic_launcher_foreground)
...
.setWhen(0)
...

According to the Documentation you can use:
setWhen(long when)
setShowWhen(boolean show);
By default setShowWhen is true, so you can set it to setShowWhen(false) to remove the timestamp.

Related

How to set or change notification buttons position

Can not find any information about buttons positioning. I have discovered that different devices show buttons in different (left\center\right)
I can provide some code from my Xamarin project but Kotlin\Java code should be almost the same
var notificationBuilder = new NotificationCompat.Builder(Application.Context, ChannelId)
.SetSmallIcon(ApplicationContext.ApplicationInfo.Icon)
.SetContentTitle("title")
.SetContentText("message")
.SetAutoCancel(true)
.SetStyle(new NotificationCompat.BigTextStyle().BigText(message))
.SetColor(Resource.Color.orange)
.SetContentIntent(contentIntent);
var buttonIntent = new Intent(ApplicationContext, typeof(NotificationReceiver));
buttonIntent.PutExtra(NotificationConstants.SelectedActionKey, selectedAction);
var pendingIntent = PendingIntent.GetBroadcast(ApplicationContext, requestCode, buttonIntent, PendingIntentFlags.UpdateCurrent);
notificationBuilder.AddAction(0, "cool button", pendingIntent),
You won't be able to pick how the default actions are displayed. But you can instead custom view.
RemoteViews remoteViews = new RemoteViews("com.companyname.NotificationChannelsDemo", Resource.Layout.layout1);
var builder = new NotificationCompat.Builder(this, CHANNEL_ID2)
.SetCustomContentView(remoteViews).
.........
com.companyname.NotificationChannelsDemo is your package name.
Resource.Layout.layout1 is the layout of your custom view. You could set the button position in this layout.

Updated notification with custom layout changes to default layout android

I have a custom layout notification which populates some data (distance data).
I have to update the distance in the existing notification.
When I update it , my custom layout changes to default android notification layout.
New notification creation code.
remoteViews = notificationCustomUI(poi_name, formatDistanceText(poi_type, distance), left_action, right_action, pendingLeftIntent, pendingRightIntent);
notificationBuilder = new NotificationCompat.Builder(this); // creating new builder for new notification.
notification = notificationBuilder.setSmallIcon(R.mipmap.launcher)
.setContentText(formatDistanceText(poi_type, distance))
.setContentTitle(poi_name)
.setSmallIcon(getNotificationIcon())
.setLargeIcon(largeIcon)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingLeftIntent)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDeleteIntent(getDeleteIntent(notificationId, poi_id, poi_type))
.build();
notification.bigContentView = remoteViews;
notificationManager.notify(notificationId, notification);
Updating exiting notification code
remoteViews.setTextViewText(R.id.tv_distance, formatDistanceText(poi_type, distance));
notificationManager.notify(existingNotificationId, notificationBuilder.build());

Android remove smallIcon overlay from expanded notification layout

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);

Android's Expanded Notifications - Both Standard ContentView and custom BigContentView after expansion

I've made a custom RemoteView in JellyBean, as described here, and set it as the bigContentView of a notification.
notification.bigContentView = customNotifView;
I'm trying to have the custom layout placed below the standard contentView after the expansion; something like this.
The problem is that the custom layout overrides the standard contentView after the expansion.
Is there a way to do that?
I solved by creating a custom layout for the contentView called layout_base_content_notification.xml, which is exactly the same (handmade) layout Android provides for notifications.
RemoteViews collapsedViews = new RemoteViews(c.getPackageName(), R.layout.layout_base_content_notification);
Notification noti = builder.build();
noti.contentView = collapsedViews;
Then I've included it in a customLayout, called layout_big_content_notification.xml:
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/layout_base_content_notification" />
And added it as a bigContentView:
RemoteViews expandedViews = new RemoteViews(c.getPackageName(), R.layout.layout_big_content_notification);
noti.bigContentView = expandedViews;
Now, after the expansion, the bigContentView replaces the contentView, but their header is the same.
Please let me know if there is a better solution.
Much simple solution would look like this
RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.big_notificaiton_layout);
fillTextViews(profile, contentView);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NotificationChannelHelper.Channel.Channel.getId())
.setContentIntent(intent)
.setContentTitle(context.getResources().getString(R.string.profile_name_is_active, profile.getTitle()))
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setCustomBigContentView(contentView)
.setOngoing(true)
.setSmallIcon(R.drawable.icon);
You have to set NotificationCompat.DecoratedCustomViewStyle().
You need to create your own RemoteViews, then indicate that you want the expanded content to inherit your custom RemoteViews.
RemoteViews expandedView = new RemoteViews(YOUR CONTEXT.getPackageName(), YOUR CUSTOM LAYOUT);
Notification notification = mBuilder.build();
notification.bigContentView = expandedView;
or check this link.

Android notification button in normal view

I want to add a pause button and if possible a remove button in the notifications.
Like this:
How can I do this?
This is my Code for the notification:
notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
context = getApplicationContext();
builder = new NotificationCompat.Builder(context)
.setContentTitle("Go!")
.setContentText("Timer set to " + waitingtime/1000 + " seconds")
.setTicker("Started!")
.setWhen(System.currentTimeMillis())
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(false)
.setOngoing(true)
.setSmallIcon(R.drawable.notlogosmall);
You will need to add a custom view, it is like how you show views on a home screen widget:
Look at this method on the Notification Builder
http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setContent(android.widget.RemoteViews)
Pass it your RemoteView with the two buttons you want in the layout.
small example:
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.custom_notification);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContent(remoteViews);

Categories

Resources