Word Wrap on basic Android Notifications - android

Here is how I do my basic Notification:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Title")
.setContentText(msg);
mBuilder.setAutoCancel(true);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NID, mBuilder.build());
I am not using the BigViewStyle, just your basic notification. If the incoming msg is too long, it doesn't show the entire sentence. Is there anyway to do this without using BigViewStyle?

Is there anyway to do this without using BigViewStyle?
No, simply because the size of a regular Notification is limited.

Related

Multiline Notification title

I need to show the notification title in two lines but am not able to do so. I have tried to use setStyle() for this but doesn't seem to be working.
My Notification builder code ::
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.mipmap.icon_not);
mBuilder.setColor(getResources().getColor(R.color.colorPrimary));
mBuilder.setContentTitle("")
.setStyle(new NotificationCompat.BigTextStyle().setBigContentTitle(notTitle))
.setContentText(notBodyInt)
.setAutoCancel(true)
.setContentIntent(resultPendingIntent);
Still the notification title is getting wrapped up in a single line.

Automatic push notification without server part

Is there a way to organize automatic notification pushes without server part for android?
I have a service on pc (not on phone), that periodically add data to google doc or google disc.
So on every new info i want to push notification.
For me it's seems like not possible, but may be not?
What you can do is create a local notification in your service class. So once you have updated the data you can just build a new notification like this.
private void sendUpdateNotification() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
builder.setContentIntent(resultPendingItent)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher))
.setTicker(msg)
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle("My App")
.setContentText(msg);
notificationManager.notify(notifyID, builder.build());
}

Some Devices are not displaying Message body, only title is visible

This is how i am displaying Push Notification to user.
Notification notification = new Notification.Builder(getApplicationContext())
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setContentIntent(intent)
.setContentTitle("Title")
.setStyle(new Notification.BigTextStyle()
.bigText(msg))
.setSound(soundUri)
.build();
notificationManager.notify(0, notification);
This is working fine on Samsung s5, Samsung s7. But on samsung Note3 , it only displays title, and does not display message body. This may be occurring in some other devices as well.
Kindly guide me that what can be the reason of this.
Adding .setContentText(msg) to your call chain should solve the problem.
Android documentation for NotificationCompat states that "If the platform does not provide rich notification styles, [setStyle(Style style)] has no effect". I'm going to take a leap of faith and say that this is the cause of the problem.
Try this code
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setContentIntent(intent)
.setContentTitle("Title")
.setStyle(new Notification.BigTextStyle()
.bigText(msg))
.setSound(soundUri)
.setContentText(detail);
NotificationManager mNotifyMgr =
(NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, mBuilder.build());

Android PushNotification using GCM

I have implemented GCM PushNotification in Android and it works perfectly fine.
I even get notifications on regular basis.
I just have one query that when I get the notifications on my device tray and if I don't click it and if other notification also appears after some time then will it show two different notifications or just one i.e latest one.
Right now I am getting only the latest one.
Any help will be appreciated.
Yes you can show all notification and new one will not replace old one by passing uniqueId in mNotificationManager.notify(uniqueId, mBuilder.build())
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.icon)
.setContentTitle("FleeGroups Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(message))
.setContentText(message)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL);
mBuilder.setContentIntent(resultPendingIntent);
mNotificationManager.notify(uniqueId, mBuilder.build());

Notification setAutoCancel(true) doesn't work

I'm trying to show a notification that is removed when the user taps on it.
I'm using the NotificationCompat class to build my notification and I call setAutoCancel(true) on my builder. This is the piece of code:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("title")
.setAutoCancel(true)
.setContentText("content");
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mBuilder.build());
The notification is correctly added but when I tap on it nothing happens! What am I doing wrong?
Using setContentIntent should solve your problem:
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0));
In your example:
NotificationCompat.Builder mBuilder= new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("title")
.setAutoCancel(true)
.setContentText("content")
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0));
NotificationManager notificationManager= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mBuilder.build());
Often you might want to direct the user to the relevant content and so might replace 'new Intent()' with something else.
I uploaded a demo to github.
I know an answer has already been accepted, but I had the same problem with a different solution so I will share it here.
For me, I was using the same NotificationCompat.Builder object to create a notification which called setOngoing(true). This was for an upload progress notification which should not be removed while working.
Anyways, after the task was complete, I called setAutoCancel(true) but the notification was still not swiping away. What I had to do also was call setOngoing(false).
It seems pretty obvious now, but it might save someone else some time in the future.

Categories

Resources