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();
Related
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().
i want to create a notification that user can't close it by drag to left or right.
user must click o that to close notification
my code is :
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com"));
PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
Notification n = new Notification.Builder(getApplicationContext())
.setContentTitle("title")
.setContentText("text")
.setSmallIcon(R.drawable.ic_tt)
.setLargeIcon( BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.ic_tt))
.setContentIntent(pIntent)
.setDefaults(Notification.DEFAULT_SOUND)
.setOngoing(true).build();
n.flags |= Notification.FLAG_ONGOING_EVENT;
NotificationManager notificationManager =(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, n);
but it dosnot work
any idea ?
You need to use both functions to retain the notification,
nBuilder.setOngoing(true);
nBuilder.setAutoCancel(true);
It can be cancelled only when user taps on it.
Find setAutoCancel in this Managing Notifications.
Hope this helped!
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 found many answer to this, but none help :(
I have this code:
private static void generateNotification(Context context, String message) {
int icon = R.drawable.icon;
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int notifyID = 96;
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(context)
.setContentTitle("MyApp")
.setContentText(message)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setSmallIcon(icon);
Notification notification = mNotifyBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(notifyID, notification);
}
But if i click on notification nothing happen and it is still there. In documentation is, that i need to use:
.setAutoCancel(true)
Some one have similar problem and somebody tells him to use:
notification.flags |= Notification.FLAG_AUTO_CANCEL;
I use both, but no result :(
Thank you very much for answers. :)
I think that if you don't use an Intent with your notification, the only way to dismiss its to swipe it.
Otherwise you can use an Intent to open your activity and that will actually clear the notification:
Intent showIntent = new Intent(this, YourActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, showIntent, 0);
And to add it to the notification:
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(context)
.setContentTitle("MyApp")
.setContentText(message)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setContentIntent(contentIntent)
.setSmallIcon(icon);
Hope this helps!
The user can dismiss all notification or if you set your notification to auto-cancel it is also removed once the user selects it.
You can also call the cancel() for a specific notification ID on the NotificationManager. The cancelAll() method call removes all of the notifications you previously issued.
Example:
mNotificationManager.cancel(notifyId);
just use this line inside button onclick event-
mNotificationManager.cancel(notifyId);//id is that u used for notification
just add
Intent showIntent = new Intent();
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, showIntent, 0);
myNotification = new Notification.Builder(getApplicationContext())
.setContentTitle("Title")
.setContentText("Text")
.setTicker("notificatio")
.setContentIntent(contentIntent)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher)
.build();
add empty pending intent then on click on notification, notification will remove. it works for me.
I generally use:
mNotificationManager.cancelAll();
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 :)