Addline of NotificationCompat.InboxStyle is not working - android

I am trying to make a chat application where my user will get notifications. The volume of notifications are so high, if I will make one entry for each notifications then it will fill up all the places, so I thought of applying BigTextView notifications or Stack of notifications.
I wrote below piece of code:
NotificationManager notificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
if(listMessage.size() <= 5)
listMessage.add(messagetype + ":" + msg);
else
{
listMessage.remove(4);
listMessage.add(messagetype + ":" + msg);
}
Intent notificationIntent = new Intent(this, GcmActivity.class);
notificationIntent.putExtra("title", messagetype);
notificationIntent.putExtra("message", msg);
PendingIntent intent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
NotificationCompat.Builder mBuilder;
mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My MESSENGER")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("MESSAGES"))
.setContentText(msg)
.setAutoCancel(true)
.setLights(Color.WHITE, 1000, 5000)
.setDefaults(Notification.DEFAULT_VIBRATE |
Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS)
.setContentIntent(intent);
NotificationCompat.InboxStyle inboxStyle =
new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle("MESSAGES");
for(int j= 0;j < listMessage.size();j++)
{
inboxStyle.addLine(listMessage.get(j));
}
mBuilder.setStyle(inboxStyle);
notificationManager.notify(0, mBuilder.build());
This seems not to add lines in the notification. it just shows the setContentText and it shows nothing.

You may not have the expanded view of the notification.
You need to swipe down within the notification (it works better with two fingers).

Related

How to group android notifications like whatsapp? as setGroup not work for me

I am currently test on Oreo and lollipop devices. What I am done so far:
final static String GROUP_KEY_NOTIFY = "group_key_notify";
int notificationId0 = 100;
int notificationId1 = 101;
int notificationId2 = 102;
int notificationId3 = 103;
NotificationCompat.Builder builderSummary =
new NotificationCompat.Builder(this)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentTitle("A Bundle Example")
.setContentText("You have 3 new messages")
.setGroup(GROUP_KEY_NOTIFY)
.setGroupSummary(true);
NotificationCompat.Builder builder1 =
new NotificationCompat.Builder(this)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentTitle("New Message")
.setContentText("You have a new message from Kassidy")
.setGroup(GROUP_KEY_NOTIFY);
NotificationCompat.Builder builder2 =
new NotificationCompat.Builder(this)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentTitle("New Message")
.setContentText("You have a new message from Caitlyn")
.setGroup(GROUP_KEY_NOTIFY);
NotificationCompat.Builder builder3 =
new NotificationCompat.Builder(this)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentTitle("New Message")
.setContentText("You have a new message from Jason")
.setGroup(GROUP_KEY_NOTIFY);
NotificationManager notifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notifyMgr.notify(notificationId1, builder1.build());
notifyMgr.notify(notificationId2, builder2.build());
notifyMgr.notify(notificationId3, builder3.build());
notifyMgr.notify(notificationId0, builderSummary.build());
What I am noticing, if there is 4 or more notifications occurs then they are bundled together but when there are less than 4 notification they are not bundled in android device above N. I read the documentations and doing what they are saying like using setGroup method and make separate notification object for summaryNotification. But nothing gets work for me.
You can use this link for reference for creating bundled notification.
Example:
String GROUP_KEY_WORK_EMAIL = "com.android.example.WORK_EMAIL";
Notification newMessageNotification = new
NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
.setSmallIcon(R.drawable.new_mail)
.setContentTitle(emailObject.getSenderName())
.setContentText(emailObject.getSubject())
.setLargeIcon(emailObject.getSenderAvatar())
.setGroup(GROUP_KEY_WORK_EMAIL)
.build();
Notification summaryNotification =
new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
.setContentTitle(emailObject.getSummary())
//set content text to support devices running API level < 24
.setContentText("Two new messages")
.setSmallIcon(R.drawable.ic_notify_summary_status)
//build summary info into InboxStyle template
.setStyle(new NotificationCompat.InboxStyle()
.addLine("Alex Faarborg Check this out")
.addLine("Jeff Chang Launch Party")
.setBigContentTitle("2 new messages")
.setSummaryText("janedoe#example.com"))
//specify which group this notification belongs to
.setGroup(GROUP_KEY_WORK_EMAIL)
//set this notification as the summary for the group
.setGroupSummary(true)
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(emailNotificationId1, newMessageNotification);
notificationManager.notify(SUMMARY_ID, summaryNotification);
You have to bundle the notifications into a group using Summary Notification Builder to ensure proper grouping. Below is my working code:
NotificationManager notificationManager;
NotificationCompat.Builder summaryNotificationBuilder;
int bundleNotificationId = 100;
int singleNotificationId = 100;
private void notificationGroup(Map<String, String> message) {
// Create Notification
String contentTitle = message.get("contentTitle");
String contentText = message.get("contentText");
Bitmap bm = BitmapFactory.decodeResource(getApplication().getResources(),
R.drawable.user3);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Notification Group Key
String groupKey = "bundle_notification_" + bundleNotificationId;
// Notification Group click intent
Intent resultIntent = new Intent(this, MainActivity.class);
resultIntent.putExtra("notification", "Summary Notification");
resultIntent.putExtra("notification_id", bundleNotificationId);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, bundleNotificationId, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// We need to update the bundle notification every time a new notification comes up
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
if (notificationManager.getNotificationChannels().size() < 2) {
NotificationChannel groupChannel = new NotificationChannel("bundle_channel_id", "bundle_channel_name", NotificationManager.IMPORTANCE_LOW);
notificationManager.createNotificationChannel(groupChannel);
NotificationChannel channel = new NotificationChannel("channel_id", "channel_name", NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
}
summaryNotificationBuilder = new NotificationCompat.Builder(this, "bundle_channel_id")
.setGroup(groupKey)
.setGroupSummary(true)
.setContentTitle(contentTitle)
.setContentText(contentText)
.setSmallIcon(R.drawable.app_logo)
.setLargeIcon(bm)
.setAutoCancel(true)
.setContentIntent(resultPendingIntent);
if (singleNotificationId == bundleNotificationId)
singleNotificationId = bundleNotificationId + 1;
else
singleNotificationId++;
// Individual notification click intent
resultIntent = new Intent(this, NotificationActivity.class);
resultIntent.putExtra("notification", "Single Notification");
resultIntent.putExtra("notification_id", singleNotificationId);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
resultPendingIntent = PendingIntent.getActivity(this, singleNotificationId, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this, "channel_id")
.setGroup(groupKey)
.setContentTitle(contentTitle)
.setContentText(contentText)
.setSmallIcon(R.drawable.app_logo)
.setLargeIcon(bm)
.setSound(defaultSoundUri)
.setAutoCancel(true)
.setGroupSummary(false)
.setContentIntent(resultPendingIntent);
notificationManager.notify(singleNotificationId, notification.build());
notificationManager.notify(bundleNotificationId, summaryNotificationBuilder.build());
}
Happy Coding!

Activity not opens on notification clicked

I am new to android programming please help me out what I am missing.
I receive notification but my messageActivity is not opened
NotificationManager notificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder mbuilder = new NotificationCompat.Builder(getApplicationContext())
.setContentTitle("Message from " + name)
.setContentText(message)
.setAutoCancel(true)
.setSmallIcon(R.mipmap.ic_launcher)
.setSound(soundUri);
if (strVibratePreference)
mbuilder.setVibrate(new long[]{100, 100});
notificationManager.notify(110, mbuilder.build());
Intent intent=new Intent(MessageService.this,MessageActivity.class);
intent.putExtra("name",name);
intent.putExtra("phno",_from);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(MessageService.this,0,intent,0);
mbuilder.setContentIntent(pendingIntent);
add this end code
Notification n = mbuilder.build();
notificationManager.notify(YOUR_NOTIF_ID, n);
Intent intent = new Intent(MessageService.this, MessageActivity.class);
intent.putExtra("name",name);
intent.putExtra("phno",_from);
PendingIntent pIntent = PendingIntent.getActivity(MessageService.this, (int) System.currentTimeMillis(), intent, 0);
// Build notification
// Actions are just fake
Notification noti = new Notification.Builder(MessageService.this)
.setContentTitle("New mail from " + "test#gmail.com")
.setContentText("Subject")
.setContentIntent(pIntent)
.setSmallIcon(R.mipmap.ic_launcher)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);

Notification not coming from an Android service

I have a service class that receives message from server and I should notify the user about the message. I got this code from internet for showing notification -
public void showNotification(String msg){
logger.debug("GOT NEW TEXT MESSAGE0: " + msg);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent intent = new Intent(this, NotificationReceiver.class);
PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);
logger.debug("GOT NEW TEXT MESSAGE1: " + msg);
Notification n = new Notification.Builder(this)
.setContentTitle("New Message Received")
.setContentText(msg)
.setContentIntent(pIntent)
.setAutoCancel(true)
.addAction(0, "Ok", pIntent).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, n);
}
As per the logs, I am getting the message correctly -
GOT NEW MESSAGE0: hello
GOT NEW MESSAGE1: hello
But still I am not getting any notification. Is there any problem in my code.
Although (as far as i know) it is not documented, notifications need their small icon set in order to show.
Try the following:
Notification n = new Notification.Builder(this)
.setSmallIcon(android.R.drawable.ic_dialog_info) // this is required
.setContentTitle("New Message Received")
.setContentText(msg)
.setContentIntent(pIntent)
.setAutoCancel(true)
.addAction(0, "Ok", pIntent).build();
Try this Sample.
Intent intent = new Intent(context, ResultActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder b = new NotificationCompat.Builder(context);
b.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setTicker("Hearty365")
.setContentTitle("Default notification")
.setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
//.setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND)
.setContentIntent(contentIntent)
.setContentInfo("Info")
.addAction(android.R.drawable.ic_media_pause, "Start", contentIntent)
.addAction(android.R.drawable.ic_dialog_alert, "Pause", contentIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, b.build());
When calling PendingIntent.getActivity() provide a flag (i.e. PendingIntent.FLAG_UPDATE_CURRENT) instead of just 0.
Reference to all possible constants: https://developer.android.com/reference/android/app/PendingIntent.html

Android Notification.InboxStyle still shows default notification layout

Here's my code :
public static void pushNotification(Activity currentActivity, String title, String content[]){
NotificationManager nm = (NotificationManager) currentActivity.getSystemService(Context.NOTIFICATION_SERVICE);
Intent resultIntent = new Intent(currentActivity.getBaseContext(), FileSharingActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(currentActivity);
stackBuilder.addParentStack(FileSharingActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
nm.notify(nextId++, new Notification.InboxStyle(new Notification.Builder(currentActivity.getBaseContext())
.setTicker("Ticker")
.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.setContentTitle("Content title")
.setContentText("Content text")
.setNumber(4)
.setContentIntent(resultPendingIntent))
.addLine("First Message")
.addLine("Second Message")
.addLine("Third Message")
.addLine("Fourth Message")
.setBigContentTitle("Here Your Messages")
.setSummaryText("+3 more")
.build());
}
Here's the result :
Device is S3 running Android version 4.4.2.
Why is the InboxStyle not applied?
Maybe you can try this:
Notification notif = new Notification.Builder(mContext)
.setContentTitle("5 New mails from " + sender.toString())
.setContentText(subject)
.setSmallIcon(R.drawable.new_mail)
.setLargeIcon(aBitmap)
.setStyle(new Notification.InboxStyle() //Style is here
.addLine(str1)
.addLine(str2)
.setContentTitle("")
.setSummaryText("+3 more"))
.build();
More info here: http://developer.android.com/reference/android/app/Notification.InboxStyle.html
EDIT:
I do it like this in my Android Project:
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification n = new Notification.Builder(this)
.setContentTitle(titel)
.setContentText(text)
.setTicker(ticker)
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.setAutoCancel(true)
.getNotification();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
n.flags |=Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, n);
I had to swipe down the notification to see the expanded information.
The code works just fine.

Notification inbox style however gets updated when a notification comes

I am trying to make a notification style where I want all notification would under one hood and they will be shown line wise. If a new notification comes, it should be under the second latest. The list of notification should be of 5, that means only latest 5 will be displayable.
I am using this below code for this, I don't know how to achieve this, I tried my hands StackBuilder too however I got that, this would work from higher API than I am using one now.
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, GcmActivity.class);
notificationIntent.putExtra("title", messagetype);
notificationIntent.putExtra("message", msg);
PendingIntent intent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("MY MESSENGER").setContentText("MESSAGES");
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle("MESSAGES" + " Details");
inboxStyle.addLine(messagetype + ":" + msg);
mBuilder.setStyle(inboxStyle);
mBuilder.setContentIntent(intent);
mBuilder.setAutoCancel(true);
notificationManager.notify(Integer.parseInt("0"), mBuilder.build());
I know I can do this by adding any number of line while creating
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
However I want that when ever my GCMIntentService will be called, this list will be updated.
I tried the work using this below code as well however that did not work either
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, GcmActivity.class);
notificationIntent.putExtra("title", messagetype);
notificationIntent.putExtra("message", msg);
PendingIntent intent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setAutoCancel(true)
.setContentTitle("MY MESSENGER")
.setSmallIcon(R.drawable.ic_launcher)
.setContentText("MESSAGES");
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
for(int j= 0; j<listMessage.size(); j++)
inboxStyle.addLine(listMessage.get(j));
mBuilder.setStyle(inboxStyle);
mBuilder.setContentIntent(intent);
notificationManager.notify(0, mBuilder.build());
I had the same issue and fixed it by persisting the notifications on the database to load them when a new one arrives, and delete them on click or delete.
DatabaseHelper dbHelper = new DatabaseHelper(this);
Cursor notifications = dbHelper.getAllNotifications();
NotificationCompat.Builder mBuilder = extractNotifications(title, msg, contentIntent, notifications);
dbHelper.insertNotification(title + ": " + msg);
private NotificationCompat.Builder extractNotifications(String title, String msg, PendingIntent contentIntent, Cursor notifications) {
NotificationCompat.Builder mBuilder;
mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_icon)
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(NOTIFICAITONS))
.setContentText(msg)
.setAutoCancel(true)
.setLights(Color.WHITE, 1000, 5000)
.setDefaults(Notification.DEFAULT_VIBRATE |
Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS)
.setContentIntent(contentIntent);
NotificationCompat.InboxStyle inboxStyle =
new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle(NOTIFICAITONS);
while (notifications.moveToNext())
{
inboxStyle.addLine(notifications.getString(notifications.getColumnIndex(DatabaseHelper.NOTIFICATION_MESSAGE)));
}
inboxStyle.addLine(title + ": " + msg);
mBuilder.setStyle(inboxStyle);
return mBuilder;
You need to do that yourself, by assigning own ID to your notification you will be able to update it later. And you need to build the notification yourself too, by concatenating messages

Categories

Resources