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
Related
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!
I have tried below code for it with custom Notification but its not working. I want to display counter in my custom notification like music player .
final RemoteViews remoteViews = new RemoteViews(getPackageName(),
R.layout.layout_workout);
remoteViews.setTextViewText(R.id.layout_workout_tv_name, type + " " + videoList.get(position).getName());
remoteViews.setTextViewText(R.id.layout_workout_tv_time, strTime);
final Intent pauseWorkout = new Intent();
pauseWorkout.setAction(ACTION_PAUSE);
final PendingIntent pauseWorkoutPendingIntent = PendingIntent.getBroadcast(this, 101, pauseWorkout, PendingIntent.FLAG_UPDATE_CURRENT);
final Intent resumeWorkout = new Intent();
resumeWorkout.setAction(ACTION_RESUME);
final PendingIntent resumeWorkoutPendingIntent = PendingIntent.getBroadcast(this, 102, resumeWorkout, PendingIntent.FLAG_UPDATE_CURRENT);
final Intent skipWorkout = new Intent();
skipWorkout.setAction(ACTION_SKIP);
final PendingIntent skipExercisePendingIntent = PendingIntent.getBroadcast(this, 103, skipWorkout, PendingIntent.FLAG_UPDATE_CURRENT);
final Intent completeSetWorkout = new Intent();
completeSetWorkout.setAction(ACTION_COMPLETE_SET);
final PendingIntent completeSetPendingIntent = PendingIntent.getBroadcast(this, 104, completeSetWorkout, PendingIntent.FLAG_UPDATE_CURRENT);
final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(getNotificationIcon())
.setColor(ContextCompat.getColor(this, R.color.colorAccent))
.setAutoCancel(false)
.setContent(remoteViews)
.setShowWhen(false)
.setOngoing(true);
notificationBuilder.setWhen(System.currentTimeMillis());
remoteViews.setOnClickPendingIntent(R.id.layout_workout_btn_complete_set, completeSetPendingIntent);
remoteViews.setOnClickPendingIntent(R.id.layout_workout_btn_pause, pauseWorkoutPendingIntent);
remoteViews.setOnClickPendingIntent(R.id.layout_workout_btn_resume, resumeWorkoutPendingIntent);
remoteViews.setOnClickPendingIntent(R.id.layout_workout_btn_skip, skipExercisePendingIntent);
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
I have also tried below code as well
.setPriority(Notification.PRIORITY_MAX)
For the higher priority notifications, use below code snippet...
NotificationCompat.Builder mBuilder =
(NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.some_small_icon)
.setContentTitle("Title")
.setContentText("This is a test notification with MAX priority")
.setPriority(Notification.PRIORITY_MAX);
How to create custom default notification? I am using Remote view to create the notification. but its not what was expect. Below is the image attched. I want to create the notification as that, with two button at bottom(like The Big Meeting is defined.
Any tutorial will be really helpful.
Below is code snippet what I have written.
Intent snoozeIntent = new Intent(LockableActivity.INTENT_SNOOZE);
PendingIntent pendingSnoozeIntent = PendingIntent.getBroadcast(context, 0, snoozeIntent, 0);
Intent gotItIntent = new Intent(LockableActivity.INTENT_GOT_IT);
PendingIntent pendingGotItIntent = PendingIntent.getBroadcast(context, 0, gotItIntent, 0);
RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.view_notification);
remoteView.setTextViewText(R.id.tv_title, entity.getClientName() + " appointment is late");
remoteView.setTextColor(R.id.tv_title, context.getResources().getColor(android.R.color.black));
remoteView.setOnClickPendingIntent(R.id.btn_snooze, pendingSnoozeIntent);
remoteView.setOnClickPendingIntent(R.id.btn_got_it, pendingGotItIntent);
NotificationManager mNotificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(entity.getClientName() + " appointment is late")
.setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle().bigText(context.getString(R.string.notification_content)))
.setContent(remoteView);
But here I have my xml. Is there other way by which I could DD THE BUTTONs at bottom.
You need to set your remote view to the bigContentView field in the notification
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(entity.getClientName() + " appointment is late")
.setAutoCancel(true).build();
notification.bigContentView = remoteView;
mNotificationManager .notify(0,notification);
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.
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).