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);
Related
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.
I'm trying to implement custom notification with an action buttons
But when I try to implement this, the actions buttons and the app name doesn't appear.
My implementation:
RemoteViews remoteViews = new
RemoteViews(getPackageName(),R.layout.emotions_notification);
Intent receiver = new Intent();
receiver.setAction("Action");
PendingIntent pendingIntentYes = PendingIntent.getBroadcast(this, 12345, receiver, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "feelBetter.emotions")
.setContent(remoteViews)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_status_bar)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setColor(Color.parseColor("#322176"))
.setLights(Color.parseColor("#322176"), 1000, 1000)
.addAction(R.drawable.common_google_signin_btn_icon_dark_normal, "Action", pendingIntentYes)
.setSound(uri)
.setAutoCancel(true);
notificationManager = NotificationManagerCompat.from(this);
// notificationId is a unique int for each notification that you must define
notificationManager.notify(id, mBuilder.build());
How to implement this?
try this
// 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);
// Apply the layouts to the notification
Notification customNotification = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(notificationLayout)
.setCustomBigContentView(notificationLayoutExpanded)
.build();
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());
In my Android application I want to create a expendable notification. For this I have to set a Style for the notification like this:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(android.R.drawable.ic_menu_info_details);
builder.setContentTitle("My notification");
builder.setContentText("Hello World!");
builder.setStyle(...);
Android gives me some styles I can use with this method but I like to create my own, so I can load a layout in the notification. So how can I do this? If I create a subclass of Style there are only these two methods:
public Notification build();
public void setBuilder(Builder builder);
So how can I load my own layout in the notification? builder.setContent is insufficient for me because then I only have 64dp for the layout.
Found a solution for this problem. It's really easy. Just use this code here:
String packageName = this.getPackageName();
RemoteViews bigView = new RemoteViews(packageName, R.layout.test);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(android.R.drawable.ic_menu_info_details);
builder.setContentTitle("My notification");
builder.setContentText("Hello World!");
Notification noti = builder.build();
noti.bigContentView = bigView;
NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mgr.notify(1234, noti);
I need to implement expand and collapse notification in status bar for android 4.0 and above version. I have search on google for this but didn't getting any code solution for implementation does anybody have I idea how to implement this
Thank You in advance
An expandable Notification is a special case of a Notification Big View. If the Big View is not at the top of the notification drawer, it it shown 'closed' and is expandable by swipe. Quote from Android Developers:
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. Expanded notifications are available starting with Android 4.1.
The Big View Notification can be created as follows:
Notification notification = new Notification.BigTextStyle(builder)
.bigText(myText).build();
or
Notification notification = new Notification.BigPictureStyle(builder)
.bigPicture(
BitmapFactory.decodeResource(getResources(),
R.drawable.my_picture)).build();
Here is a tutorial.
Notification noti = new Notification.Builder()
... // The same notification properties as the others
.setStyle(new Notification.BigPictureStyle().bigPicture(mBitmap))
.build();
You change
.setStyle(new NotificationCompat.BigTextStyle().bigText(th_alert))
along with the announcement OK !!!
notification = new NotificationCompat.Builder(context)
Here is an example:
Expandable Notifications and Notifications Groups
You can set Code
Intent intent = new Intent(context, ReserveStatusActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
intent = new Intent(String.valueOf(PushActivity.class));
intent.putExtra("message", MESSAGE);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(PushActivity.class);
stackBuilder.addNextIntent(intent);
// PendingIntent pendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
// android.support.v4.app.NotificationCompat.BigTextStyle bigStyle = new NotificationCompat.BigTextStyle();
// bigStyle.bigText((CharSequence) context);
notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(th_title)
.setContentText(th_alert)
.setAutoCancel(true)
// .setStyle(new Notification.BigTextStyle().bigText(th_alert) ตัวเก่า
// .setStyle(new NotificationCompat.BigTextStyle().bigText(th_title))
.setStyle(new NotificationCompat.BigTextStyle().bigText(th_alert))
.setContentIntent(pendingIntent)
.setNumber(++numMessages)
.build();
notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationManager.notify(1000, notification);
or
private void sendNotification(RemoteMessage.Notification notification, Map<String, String> data) {
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.logo);
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
// .setContentTitle(notification.getTitle())
.setContentTitle(getResources().getText(R.string.app_name))
.setContentText(notification.getBody())
.setAutoCancel(true)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(pendingIntent)
.setStyle(new NotificationCompat.BigTextStyle().bigText(notification.getBody()))
.setContentInfo(notification.getTitle())
.setLargeIcon(icon)
.setColor(Color.RED)
.setSmallIcon(R.drawable.logo);
try {
String picture_url = data.get("picture_url");
if (picture_url != null && !"".equals(picture_url)) {
URL url = new URL(picture_url);
Bitmap bigPicture = BitmapFactory.decodeStream(url.openConnection().getInputStream());
notificationBuilder.setStyle(
new NotificationCompat.BigPictureStyle().bigPicture(bigPicture).setSummaryText(notification.getBody())
);
}
} catch (IOException e) {
e.printStackTrace();
}
notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
notificationBuilder.setLights(Color.YELLOW, 1000, 300);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
27/07/2021 :
You should can read detail this : Expandable Notifications and Notifications Groups
I Couldn't able to set new instance of new NotificationCompat.BigTextStyle() in .setStyle() method of Notification. So I have used the below one, new instance of new Notification.BigTextStyle() in .setStyle().
Notification builder =new Notification.Builder(this)
.setSmallIcon(Notification_icons[icon])
.setContentTitle(title)
.setContentText(description)
.setChannelId(channelID_Default)
.setOngoing(true)
.setStyle(new Notification.BigTextStyle()
.bigText(description))
.build();
There is a method for this function you can show a small icon when notification collapsed and show a large one when a notification expanded.
val bitmap = BitmapFactory.decodeResource(resources, R.drawable.notification)
var notification = NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.new_post)
.setContentTitle(imageTitle)
.setContentText(imageDescription)
.setLargeIcon(bitmap)
.setStyle(NotificationCompat.BigPictureStyle()
.bigPicture(bitmap)
.bigLargeIcon(null))
.build()
(2021) Stumbled upon this question when I was having issues with expanding my notification which had a larger text.
Solved by referring to the Android docs on Expandable Notifications: Create an Expandable Notification
Nimisha V's answer shows how to expand a large image on a notification. The below code is used for expanding large text on a notification.
var notification = NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.icon)
.setContentTitle(notification_title)
.setContentText(notification_message)
.setStyle(NotificationCompat.BigTextStyle()
.bigText(notification_message))
.build()
We can't create expandable notification in below android 4.1 versions.
But Instead of this we can do that we can stacked the notifications and then we can set a pending intent to our pretty custom activity which shows all notification in list.
User will happy to see this :)