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 :)
Related
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();
I am trying to modify existing notifications in android.
What I have in my app
When a notification is already in system tray and another notification appears, the second one overwrites the first notification content.
What I am looking for
If second Notification arrives then instead of overwriting the first I need to change title to show 2 New Messages and go on incrementing as notifications arrive.
Code Implemented
Bitmap icon = BitmapFactory.decodeResource(ctx.getResources(),
R.drawable.icon);
Intent launchActivity = new Intent(ctx, CordovaApp.class);
launchActivity.putExtra("heading",newsHeader);
launchActivity.putExtra("content",newsText);
PendingIntent pi = PendingIntent.getActivity(ctx,0, launchActivity, PendingIntent.FLAG_NO_CREATE);
ParseAnalytics.trackAppOpened(launchActivity);
if(pi==null){
Log.d(TAG, "Pending Intenet is null.");
}else{
Log.d(TAG, "Pending Intenet is not null.");
}
Notification noti = new NotificationCompat.Builder(ctx)
.setContentTitle(newsHeader)
.setContentText(newsText)
.setSmallIcon(R.drawable.icon)
.setLargeIcon(icon)
.setContentIntent(pi)
.setAutoCancel(true)
.build();
NotificationManager nm = (NotificationManager)ctx.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(0, noti);
Update
I implemented the solution mentioned below by #yogendra and now I am getting two separate notifications. Instead of getting stacked. Below is updated code
Notification noti = new NotificationCompat.Builder(ctx)
.setContentTitle(newsHeader)
.setContentText(newsText)
.setSmallIcon(R.drawable.icon)
.setGroup(GROUP_KEY_EMAILS)
.setLargeIcon(icon)
.setContentIntent(pi)
.setLights(Color.parseColor("green"), 5000, 5000)
.setAutoCancel(true)
.setPriority(2)
.setTicker("Notification from App")
.setGroupSummary(true)
.build();
NotificationManager nm = (NotificationManager)ctx.getSystemService(Context.NOTIFICATION_SERVICE);
int timeSeconds = (int)System.currentTimeMillis()%Integer.MAX_VALUE;
Log.i(TAG,"Timing function called "+timeSeconds);
nm.notify(timeSeconds, noti);
See your code
nm.notify(0, noti);
where
notify(int id, Notification notification)
Here 0 is the ID of notification which is to be managed with respect to each notification. If you want to show a different notification your notification id should be unique each time. If you try to post a notification with the same notification id your previously displayed notification will be replaced with the latest notification.
Solution
Now you need to display a custom notification with a custom layout and update the counter each time .
Source code to create a Custom Notification.
create global variable in your class :
private int count = 0;
private ArrayList<String> notificationList = new ArrayList<String>();
private String GROUP_KEY_EMAILS = "email";
/call method createNotification when you need to create notification and pass message what you need to show on message./
private void createNotification(String notificationMassage) {
notificationList.add(notificationMassage);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
// Create builder
Builder summaryNotification = new NotificationCompat.Builder(this)
.setContentTitle(notificationList.size()+" new messages")
.setSmallIcon(R.drawable.settings)
.setLargeIcon(largeIcon)
.setGroup(GROUP_KEY_EMAILS)
.setGroupSummary(true)
.setAutoCancel(true);
// Create style
InboxStyle nStyle = new NotificationCompat.InboxStyle();
nStyle.setBigContentTitle(notificationList.size()+" new messages");
nStyle.setSummaryText("Summery Text...<you can set as blank>");
for (String Str : notificationList) {
nStyle.addLine(Str);
}
summaryNotification.setStyle(nStyle);
mNotificationManager.notify(0, summaryNotification.build());
count++;
}
/**
please clear notification array list after tap on notification.
*/
For more detail please refer below link:
https://developer.android.com/training/wearables/notifications/stacks.html#AddGroup
https://developer.android.com/training/wearables/notifications/stacks.html#AddGroup
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();
I followed this Sample Code
In Big Text Notifications section, he said that need expand to see Big text notification form, as image im below :
I wonder that we can not set Expanded Notification as default in Big Text Notifications?
People who know it is can or not,
If can,
Please tell me how to do it,
Thanks,
The documentation states:
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.
So my answer is no, you can't expand it by default.
There is however a trick to push the notification to the top of the list where it would be expanded. Simply set the Priority to Notification.PRIORITY_MAX and chances are that your app's notification will make it to the top.
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
notification = new NotificationCompat.Builder(context)
Here is an example:
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);
notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText("Your Long Text here"))
simply setStyle of your notification builder.
No need of doing any kind of modification in the any file for showing of multiple lines of Notification while using Push Notification V5, remove the field "style", from the object you are sending. Automatically the Multiple lines of notification will be seen. For more information, upvote the answer, ask your query. I will help you out.
For reference, visit this question
From this notification code you have got images, and big text or more.
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBuilder.setSmallIcon(R.drawable.small_logo);
mBuilder.setColor(Color.parseColor("#D74F4F"));
} else {
mBuilder.setSmallIcon(icon);
}
mBuilder.setTicker(title).setWhen(when);
mBuilder.setAutoCancel(true);
mBuilder.setContentTitle(title);
mBuilder.setContentIntent(intent);
mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
mBuilder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), icon));
mBuilder.setContentText(msg);
mBuilder.setPriority(Notification.PRIORITY_MAX);
if (Utils.validateString(banner_path)) {
mBuilder.setStyle(notiStyle);
} else {
mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(msg));
}
Notification noti = mBuilder.build();
notificationManager.notify(0, noti);
I used below code for displaying notification in notification Bar.
it worked fine.
But i need to display notification icon dynamically that will come from web service.
How i can do?
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification note = new Notification(R.drawable.image,"status message", System.currentTimeMillis());
Intent in = new Intent(Notify.this,CommentU.class);
PendingIntent pi = PendingIntent.getActivity(Notify.this, 0, in, 0);
note.setLatestEventInfo(Notify.this,"NotificationTitle", "You have a new Commentio", pi);
note.number = ++count;
note.vibrate = new long[] { 500l, 200l, 200l, 500l };
note.flags |= Notification.FLAG_AUTO_CANCEL;
nm.notify(NOTIFY_ME_ID, note);
Thanks in Advance.
I have one suggestion for you: you have to download that image which you want to show and then you can set that as bitmap: check below code. I have created one BITMAP.
its look link :
for this you have to add android-support-v4.jar
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification").setLargeIcon(BITMAP)
.setContentText("Hello World!");
Intent resultIntent = new Intent(this, test.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(test.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFY_ME_ID, mBuilder.build());
for more detail chekc this link.
Removing notifications
Notifications remain visible until one of the following happens:
The user dismisses the notification either individually or by using "Clear All" (if the notification can be cleared).
The user clicks the notification, and you called setAutoCancel() when you created the notification.
You call cancel() for a specific notification ID. This method also deletes ongoing notifications.
You call cancelAll(), which removes all of the notifications you previously issued.
Edited: just replace this.
mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification").setLargeIcon(BITMAP)
.setAutoCancel(true)
.setContentText("Hello World!");
just add some icons in your resource folder and then
int myIcon = R.drawable.image;
your logic goes here ....
and change value of icon according to your logic like myIcon = R.drawable.somenewimage;
and then finally set your notification
Notification note = new Notification(myIcon,"status message", System.currentTimeMillis());