Notification bigText does not wrap on pre-Lollipop - android

I am trying to create notifications for my application and I am struggled with different behaviour on two Android versions - I am testing on 5.0.2 and 4.1.2. For displaying long text I'm using NotificationCompat.BigTextStyle like so:
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
bigTextStyle.bigText("trying to wrap\na very long text");
On Android 5, there is wrapped text properly displayed. On version 4, there is not - see provided screenshots. How can I achieve wrapping text on pre-lollipop versions? I tried NotificationCompat.InboxStyle so far, but that looks differently and I do not want that.

This is how I achieved this:
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
// set icon, title, etc.
.
.
.
// set Message
String longText = "trying to wrap a very long text message";
builder.setContentText(longText);
if (Build.VERSION.SDK_INT >= 16) // Jelly Bean
builder.setStyle(new NotificationCompat.BigTextStyle().bigText(longText));
.
.
.
but this is to wrap a long message, your code looks like you're trying to insert a line break and control how/where the wrapping happens, so this answer might not be helpful.

Related

android 7.0 Notification icon appearing white square

I am using the below snippet to generate notifications in my android app.
private void sendNotification(String contentText, String message) {
Intent resultIntent = new Intent(this, MainActivity.class);
resultIntent.putExtra("clear","clear");
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent piResult = PendingIntent.getActivity(this, 0, resultIntent,0);
NotificationCompat.Builder builder=new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setColor(ContextCompat.getColor(getApplicationContext(),R.color.red))
.setContentTitle("title")
.setContentText(message)
.setAutoCancel(true)
.setLargeIcon(BitmapFactory.decodeResource(getResources()
,R.drawable.notification))
.setContentIntent(piResult);
NotificationCompat.InboxStyle notification = new NotificationCompat.InboxStyle(builder);
int i;
for(i=0; i<messageList.size();i++){
notification.addLine(messageList.get(i));
}
notification.setBigContentTitle("title");
notification.setSummaryText(contentText);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID,notification.build());
}
It works in android 5 and 6 but for android nougat it is not working
Following the docs from the Android API:
Status bar icons are composed simply of white pixels on a transparent backdrop, with alpha blending used for smooth edges and internal texture where appropriate.
Your whole image but the transparent parts is going to be converted to white (being originally white or having colors).
One solution is to create a silhouette icon with color, by this way you can use the same image in all Android APIs. One example could be this icon:
In lower versions of Android you would see the black face, in the last versions you will see the same face but with white color. That's because of the transparent parts (it seems SO removes the transparency, you can get the original from here) of the image.
From android version lollpop onwards, they have made the changes for the notifications. When you are specifing the small icon, it should be of specific size as mentioned in this link.
The important thing is the image should be transparent and contains only white color.
You can check this question to get the answer
According to this blog here
It says that
You’ll note that the icons are not present in the new notifications; instead more room is provided for the labels themselves in the constrained space of the notification shade. However, the notification action icons are still required and continue to be used on older versions of Android and on devices such as Android Wear.
If you’ve been building your notification with NotificationCompat.Builder and the standard styles available to you there, you’ll get the new look and feel by default with no code changes required.

Weird white expansion in android notification setContent

Trying to implement a custom notification, but I am getting this awful expansion to the notification when i try to forcefully expand it (In my case was not expecting it). This only happens when i switch off screen and then try to interact with the notification other times it acts normal and does not expand when i forcefully try too.
Also tried to cover up the white expansion by setting notification color like below :
.setColor(ContextCompat.getColor(context, R.color.notification_background_light))
did not make much difference.
Testing environment
Device : LG H815
OS : Android 6.0
SNAPHOT :
here
CODE :
builder = new NotificationCompat.Builder(context);
builder.setSmallIcon(R.mipmap.ic_launcher)
.setContent(remoteViews)
.setContentIntent(pendingIntent)
.setColor(ContextCompat.getColor(context, R.color.notification_background_light))
.setOngoing(true);
notificationManager.notify(notification_id,builder.build());
i don't know if its an error from my side.
Thanks in advance

multiline notification in android different behaviour in different device

I am trying to display notification text in multiline.
I am using this code to do so.
NotificationManager mNotificationManager;
String msg = "Do you want to share Fantasy Cricket on Twitter/FB/Google+?";
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.app_icon)
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg).setAutoCancel(true);
mBuilder.setContentTitle("Change team");
mNotificationManager.notify(notId, 123, mBuilder.build());
It's displaying differently in two devices.
First device
Second device
In first device it just displays fine text in multiline but in second, it's displaying in one line only removing trailing space.
Why it's having different behavior in both. Am I missing something here? Both device are having kitkat if that makes any difference.
UPDATE
As per #leo's answer it will display in two line when there is only single notification. But I checked TOI app it's always displaying in two lines.
TOI's screenshot
Docs say only the top notification is expanded by default. So maybe the other app you are comparing to is using a completely custom notification layout (especially since I don't see a title there)?
In case someone falls into it like me, so according to LINK
Users can drag down on a notification in the drawer to reveal the expanded view, which shows additional content and action buttons, if provided. This happens often on Samsung devices though...

How to create Custom Notification in android

I need to create a custom notification instead of the default notification in android.
Current notification have a icon,heading and message like below image
I want it customise like this
how can i achieve this
Notification views
Normal View - A notification in normal view appears in an area that’s up to 64 dp tall. Even if you create a notification with a big view style, it will appear in normal view until it’s expanded.
Content title
Large icon
Content text
Content info
Small icon
Notification time
Normal View Like
Big View - 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 were first introduced in Android 4.1 JellyBean [API 16]. Expandable notifications were designed to support rich notification style objects called Notification.Style.
Big View Like
Go to this link expandable-notifications-android
More information on official docs
I have create notification usingby following http://developer.android.com/wear/notifications/creating.html
Add code for create notification.
// Specify the 'big view' content to display the long
// event description that may not fit the normal content text.
BigTextStyle bigStyle = new NotificationCompat.BigTextStyle();
bigStyle.bigText(eventDescription);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_event)
.setLargeIcon(BitmapFractory.decodeResource(
getResources(), R.drawable.notif_background))
.setContentTitle(eventTitle)
.setContentText(eventLocation)
.setContentIntent(viewPendingIntent)
.addAction(R.drawable.ic_map,
getString(R.string.map), mapPendingIntent)
.setStyle(bigStyle);
That's called Expanded layouts which is available right from Jelly Bean version.
There are 2 views of Notifications:
Normal View
Big View
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 were first introduced in Android 4.1 JellyBean [API 16].
In your case, you just want to display big picture in notification, give Notification.BigPictureStyle a try:
Bitmap remote_picture = null;
// Create the style object with BigPictureStyle subclass.
NotificationCompat.BigPictureStyle notiStyle = new
NotificationCompat.BigPictureStyle();
notiStyle.setBigContentTitle("Big Picture Expanded");
notiStyle.setSummaryText("Nice big picture.");
try {
remote_picture = BitmapFactory.decodeStream(
(InputStream) new URL(sample_url).getContent());
} catch (IOException e) {
e.printStackTrace();
}
// Add the big picture to the style.
notiStyle.bigPicture(remote_picture);
you can use NotificationCompat that need remoteview . below i set my custom layout named
activity_downlaodactivity
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.activity_downlaodactivity);
android.support.v4.app.NotificationCompat.Builder mBuilder =
new android.support.v4.app.NotificationCompat.Builder(this)
.setContent(contentView)
manager.notify(0, mBuilder.build());

How to display timestamp in Android Notifications for Honeycomb+ devices?

I'd like to show a timestamp in an Android Notification, just like the Android Design Guidelines suggest. (see the first snapshot, "12:03PM" is what I want!).
I tried many different ways, I thought setWhen would do it, but it only seems to affect the ordering in the Notification tray. Any idea how to achieve that?
See my code below:
Notification.Builder builder = new Notification.Builder(context);
builder.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_notify)
.setLargeIcon(largeIcon)
.setTicker(text)
.setNumber(unreadCount)
.setWhen((new Date()).getTime())
.setAutoCancel(true)
.setContentTitle(title)
.setContentText(text)
.setDefaults(Notification.DEFAULT_VIBRATE);
notificationManager.notify(NOTIFICATION_ID, builder.getNotification());
I don't want to use a custom layout for the notification.
I think I found the answer myself, when seems to be displayed only on Android 4.0.3 and later.
My wife's Nexus S (4.0.3) shows it, and I just upgraded to 4.0.4 on my Galaxy Nexus and it magically started to work!
It also shows on older versions (2.3 for instance), but not on 4.0.2 !
Use setContentInfo and pass your date as a string into it.
From this link
public Notification.Builder setContentInfo (CharSequence info) Since: API Level 11
Set the large text at the right-hand side of the notification.
Large Text at the right hand side of the notification should be where you want to put time.
Also from the same link:
public Notification.Builder setWhen (long when) Since: API Level 11
Set the time that the event occurred. Notifications in the panel are
sorted by this time.
which means that setWhen only sorts the notifications, and doesn't set any text.

Categories

Resources