Custom Android notification with RemoteView and actions - android

I have an issue for display a notification with a RemoteViews and add an action on it with addAction(R.drawable.ic_notification, "More", pendingIntent)
This is the notification with the remoteViews without the addAction method
This is the notification with the addAction
As you can see the system don't display the RemoteViews and display only the action button
This is the instanciation of the remoteViews
RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.notification_small);
notificationLayout.setImageViewResource(R.id.notification_small_iv, R.drawable.logo);
notificationLayout.setTextViewText(R.id.notification_small_title, remoteMessage.getData().get(TITLE));
notificationLayout.setTextViewText(R.id.notification_small_subtitle, remoteMessage.getData().get(CONTENT));
And this is the notification builder where I set the configuration and add Action
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setCustomContentView(notificationLayout)
.setColor(getResources().getColor(R.color.nice_blue))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setLights(Color.BLUE, 1000, 1000)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setPriority(PRIORITY_MAX)
.setDefaults(DEFAULT_ALL)
.addAction( 0,"More", pendingIntent)
.build();
This is how I create the pending Intent but I'm not sure is related
PendingIntent pendingIntent;
Intent intent = new Intent(this, SplashActivity.class);
pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
I'm pretty sure it's possible to have a custom view and a AddAction button because Slack have it on the Android application

I was in the same boat. I wanted a custom view on top, with the options of the action buttons on the bottom. What you're looking for is the NotificationCompat.DecoratedCustomViewStyle.
Here is an example

Related

Implement android expandable notification with xml content?

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

Android push notification expand on click

My target is to show notification in normal mode.
After click on it to be expanded like example.
From here I must be able to click on buttons. Problem is that when I add
.setStyle(new NotificationCompat.BigTextStyle()
.setBigContentTitle(someBigTitle)
.bigText(someString)))
it ovelaps the exsisting small test
.setContentText(someSmallText)
.setSubText(someSmallSubText)
and after click did not expand. I do not want to navigate to another activity on click. What must be my intent? Full code:
Intent intent = new Intent(context, MyActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
android.app.Notification mNotification = buildAndroidNotification(notification.getTitle(), someLogo, notification.getSubject(), pIntent, true);
mNotification.flags |= android.app.Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(2, mNotification);
and builder:
mNotification = new NotificationCompat.Builder(context)
.setContentTitle(smallHeading)
.setSmallIcon(icon)
.setContentIntent(intent)
.setContentText(someSmallText)
.setSubText(someSmallSubText)
.addAction(R.drawable.some_icon, some_str, null)
.addAction(R.drawable.some_icon, some_str2, null)
.setStyle(new NotificationCompat.BigTextStyle()
.setBigContentTitle(someBigTitle)
.bigText(someString))
.setWhen(created)
.build();

Is it possible to add an action to the summary of a bundled notification?

I am using bundled notifications to group notifications by type. This works quite fine from a user's point of view when notifications are maximised, as shown below:
This does not work too well however when the child notifications are collapsed within the summary:
Now, given that the same REGISTER action can be performed on all the notifications in this group, I would like to give the user the possibility to carry out all these by simply tapping on a REGISTER ALL action in the summary notification. Adding actions to the summary however seems to have no effect. The code below - which is what I'm currently using - shows that the action on the summary notification is simply ignored:
final PendingIntent actionIntent = PendingIntent.getService(this, 0, RegisterAllService.getRegisterAllServiceIntent(),PendingIntent.FLAG_UPDATE_CURRENT);
final NotificationCompat.Action action = new NotificationCompat.Action(R.mipmap.ic_launcher, "register all", actionIntent);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_notification)
.setAutoCancel(true)
.addAction(action)
.setGroup("confirmgroup")
.setGroupSummary(true);
NotificationManagerCompat.from(getApplicationContext()).notify(0, builder.build());
for(MyNotification myNotification: myNotifications){
final PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
final PendingIntent actionIntent = PendingIntent.getService(this, 0, RegisterService.getRegisterServiceIntent(),PendingIntent.FLAG_UPDATE_CURRENT);
final NotificationCompat.Action action = new NotificationCompat.Action(R.mipmap.ic_launcher, "register", actionIntent);
NotificationCompat.Builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_notification)
.setContentTitle(myNotification.getTitle())
.setStyle(new NotificationCompat.BigTextStyle().bigText(myNotification.getSubtitle()))
.setTicker(myNotification.getSubtitle())
.setContentText(myNotification.getSubtitle())
.setAutoCancel(true)
.addAction(action)
.setGroup("confirmgroup")
.setGroupSummary(false)
.setContentIntent(pendingIntent)
NotificationManagerCompat.from(getApplicationContext()).notify(myNotification.getId(), builder.build());
}
Is there a way to make this happen?

android push notification not removed when i swipe

android push notification not removed when i swipe
following is my code that i trying to implement push notification
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(icon)
.setContentTitle(title)
.setAutoCancel(true)
.setContentText(message)
.setOngoing(true);
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.putExtra("Your Message", message);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.putExtra("type", type);
notificationIntent.putExtra("id", id);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(intent);
notificationManager.notify(NOTIFICATION_ID, mBuilder.build());
can somebody help me to resolve these issue
The .setOngoing(true); should be false, or just remove this line, if you don't put the .setOngoing(boolean) it will be false, so you'll be able to swipe the notification.
More information setOnGoing
Set whether this is an "ongoing" notification. Ongoing notifications cannot be dismissed by the user, so your application or service must take care of canceling them. They are typically used to indicate a background task that the user is actively engaged with (e.g., playing music) or is pending in some way and therefore occupying the device (e.g., a file download, sync operation, active network connection).
So your mBuilder should be like :
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(icon)
.setContentTitle(title)
.setAutoCancel(true)
.setContentText(message)
.setOngoing(false); //or just remove it
The setOngoing makes the notification not dismissible
.setOngoing(true);

Implement expand and collapse notification android

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

Categories

Resources