Implement android expandable notification with xml content? - android

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

Related

Custom Android notification with RemoteView and actions

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

Android notification Big View - Intent on button not fired

I'm following the Android Dev tutorial to use Big View for notifications. I want an on going notification with some functionality when clicking on the body of the notification and other functionality when clicking the Button.
This is my code:
Intent bodyIntent = new Intent(context, MainActivity.class);
innerIntent.putExtra(NOTIFICATION_BODY, "Click on notification body");
PendingIntent bodyPendingIntent = PendingIntent.getActivity(context, 0, bodyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent buttonIntent = new Intent(context, MainActivity.class);
buttonIntent.setAction(NOTIFICATION_BUTTON);
PendingIntent buttonPendingIntent = PendingIntent.getService(context, 0, buttonIntent, 0);
android.support.v4.app.NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setCategory(Notification.CATEGORY_SERVICE)
.setPriority(Notification.PRIORITY_MAX)
.setVisibility(Notification.VISIBILITY_PRIVATE)
.setContentIntent(notificationPendingIntent)
.setAutoCancel(true)
.setOnlyAlertOnce(true)
.setLights(getResources().getColor(R.color.primary), 50, 10000)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setVibrate(new long[] {0, 50})
.setOngoing(true)
.setStyle(new NotificationCompat.BigTextStyle().bigText("Hello World BIG!"))
.addAction(R.mipmap.ic_image, "Button", buttonPendingIntent);
int mNotificationId = 001;
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
Then in onResume:
Intent intent = getIntent();
Log.e("m", String.valueOf(intent.getIntExtra(NOTIFICATION_RESULT, 0)));
Log.e("m", String.valueOf(intent.getAction()));
Result:
When clicking on the body of the notification the bodyIntent fires and I get the correct log printing.
When clicking on the button: Nothing happens and MainActivity not even starting.
Thanks,
You should call PendingIntent.getActivity() method if you want to start an activity, but you are creating buttonPendingIntent by calling PendingIntent.getService().

Update Notification without Collasping notification drawer

i have a notification manager that launches an activity when clicked.
mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setOngoing(true)
.setOnlyAlertOnce(true)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContent(remoteViews)
.setAutoCancel(false);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, Launch.class).putExtra("type", 0), 0);
mBuilder.setContentIntent(pendingIntent);
the function performed in this can also be be asynchronous depending on the users preference. I want to update the notification to an ongoing progress. something of this nature:
mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setOngoing(true)
.setContentTitle("Sending details")
.setPriority(NotificationCompat.PRIORITY_MAX)
.setProgress(100, 0, true)
.setOnlyAlertOnce(true)
.setAutoCancel(false);
when the notification is selected. This is achieved but what i want to do is update the notification from the former to the later without having to first slide up the navigation drawer.
Try this..
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
you need NOTIFICATION_ID to updated the specific notification.
Please refer this

Android - How to Display Large Text in Notification

I need to implement a code for displaying large text with 200 characters in Notifications. Here i used following code but it shows single line.
Intent notificationIntent;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.icon)
.setContentTitle(context.getString(R.string.app_name))
.setContentText(message)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true);
notificationIntent = new Intent(context, Main.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pintent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pintent);
NotificationManager mNotifyMgr =
(NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(1, mBuilder.build());
You can choose from different styles to use for the notification builder:
In your case I would use the NotificationCompat.BigTextStyle. More details can be found here.
Don't forget to import v4 support when using this.
In my application, notification message is static but can't show all sentense.
I solved with subText properties of notification.
Notification n = new Notification.Builder(this)
.setContentTitle("Job Tracker Application")
.setContentText("GPS is turned off.")
.setSubText("Please turn on in the settings.")
.setSmallIcon(R.drawable.app_icon)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.build();
Another notification sample have show under the blogs.
http://www.objc.io/issue-11/android-notifications.html
https://capdroid.wordpress.com/tag/android-notification/
http://www.tutorialspoint.com/android/android_notifications.htm
You add a style for the the large text.
Notification notification = new NotificationCompat.Builder(this, CHANNEL_SYNC)
.setContentTitle(title)
.setSmallIcon(R.drawable.ic_sync)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setColor(getResources().getColor(R.color.colorPrimaryLight))
.setContentIntent(pendingIntent)
.build();

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