How to group pushnotifications in android? - android

I have implemeted push notification in android,and it comes perfectly as per needed,But only one problem is it displaying as individualy ,I want it in grouping,means if 5 notifications are there,it should display "5 messages" not a list of notification(which is currentl[y coming),My code is as belo,I have found solution that style can do this,and have tried but no change,
code
#SuppressWarnings("null")
public void sendNotification(String title, String description) {
Random random = new Random();
int id = random.nextInt(50);
NotificationCompat.Builder myNotification = null;
NotificationManager notificationManager = null;
notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent myIntent = new Intent(context, ActivityHome.class);
myIntent.putExtra("push", "push");
TaskStackBuilder taskbuiler = TaskStackBuilder.create(this);
taskbuiler.addParentStack(ActivityHome.class);
taskbuiler.addNextIntent(myIntent);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, myIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
myNotification = new NotificationCompat.Builder(context);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
String[] events = new String[6];
// Sets a title for the Inbox in expanded layout
inboxStyle.setBigContentTitle("Event tracker details:");
// Moves events into the expanded layout
for (int i = 0; i < events.length; i++) {
inboxStyle.addLine(events[i]);
}
// Moves the expanded layout object into the notification object.
myNotification.setStyle(inboxStyle);
myNotification.setStyle(new NotificationCompat.InboxStyle());
myNotification.setContentTitle(title);
myNotification.setContentText(description);
myNotification.setContentIntent(pendingIntent);
myNotification.setDefaults(Notification.DEFAULT_SOUND);
myNotification.setAutoCancel(true);
myNotification.setSmallIcon(R.drawable.app_icon);
myNotification.build();
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(id, myNotification.build());
}

Check this out:
https://developer.android.com/training/wearables/notifications/stacks.html
Maybe this helps.

Related

Notification Content Title and Content Text are not shwoing

I am generating local notification with multiple lines. All lines are showing properly but contentTitle and contentText are not showing.
Here is my code to generate a notification:
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(notificationIntent);
int num = (int) System.currentTimeMillis();
PendingIntent pendingIntent = stackBuilder.getPendingIntent(num, PendingIntent.FLAG_UPDATE_CURRENT);
String id = "notification_channel_id";
CharSequence name = "Message_Notification";
Bitmap bm = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, id)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("title")
.setContentText("time")
.setAutoCancel(true)
.setGroupSummary(true)
.setGroup(GROUP_KEY)
.setContentIntent(pendingIntent)
.setChannelId(id);
if (subTitle != null) {
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle("Dose Reminder");
if (subTitle.contains("---")) {
String[] splitStrings = subTitle.split("---");
for (int i = 0; i < splitStrings.length; i++) {
inboxStyle.addLine(splitStrings[i]);
}
} else {
inboxStyle.addLine(subTitle);
// Moves the expanded layout object into the notification object.
notificationBuilder.setStyle(inboxStyle);
}
// Moves the expanded layout object into the notification object.
notificationBuilder.setStyle(inboxStyle);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notificationBuilder.setColor(ContextCompat.getColor(context, R.color.colorAccent));
} else {
notificationBuilder.setLargeIcon(bm);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
// The user.-visible description of the channel.
String description = "Notifications contains messages which was sent by dev team.";
int importance = NotificationManager.IMPORTANCE_MAX;
NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_DEFAULT);
// Configure the notification channel.
mChannel.setDescription(description);
mChannel.enableLights(true);
// Sets the notification light color for notifications posted to this
// channel, if the device supports this feature.
mChannel.setLightColor(Color.RED);
if (notificationManager != null) {
notificationManager.createNotificationChannel(mChannel);
}
}
// Sets an ID for the notification
// Gets an instance of the NotificationManager service
final NotificationManager mNotifyMgr =
(NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
// Builds the notification and issues it.
if (mNotifyMgr != null) {
mNotifyMgr.notify(num, notificationBuilder.build());
}
When notification comes "title" and "time" are not showing. It showing "Dose Reminder" and subTitles in multiple lines.
Am I missing something?
setBigContentTitle(CharSequence title)
Overrides ContentTitle in the big form of the template.
https://developer.android.com/reference/android/app/Notification.InboxStyle
Check Use MessagingStyle section
https://developer.android.com/training/notify-user/build-notification
JFYI by default timestamp is gone in push notifications, Android 7 (Nougat) onwards

Notification within BroadCastReceiver doesnt work

I faced an issue with Notification within BroadcastReceiver().
as I know, My code worked properly before but it doesn't work now.
sometimes NotificationTicker appear but no title and content has been appeared.
here is my code. my searches couldn't help me to find where is the problem.
here is my CODE:
private void MyNotification(Context context) {
String NotificqationText = "NotificqationText";
String NotificationTitle = "NotificationTitle ";
String NotificationTicker = "NotificationTicker";
PendingIntent MyPendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, Splash.class), 0);
NotificationCompat.Builder MyNB = new NotificationCompat.Builder(context);
MyNB.setSmallIcon(R.drawable.icon);
MyNB.setContentTitle(NotificationTitle);
MyNB.setContentText(NotificqationText);
MyNB.setTicker(NotificationTicker);
MyNB.setAutoCancel(true);
MyNB.setContentIntent(MyPendingIntent);
Bitmap MyPicture = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon);
MyNB.setLargeIcon(MyPicture);
NotificationCompat.BigPictureStyle MyPicStyle = new NotificationCompat.BigPictureStyle().bigPicture(MyPicture);
MyPicStyle.setSummaryText("Etude can makes our life Enlightened");
MyNB.setStyle(MyPicStyle);
MyNB.setStyle(new NotificationCompat.BigTextStyle());
NotificationCompat.BigTextStyle MyText = new NotificationCompat.BigTextStyle();
MyText.bigText(NotificqationText);
MyText.setBigContentTitle(NotificationTitle);
NotificationManager MyNotifyManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
MyNotifyManager.notify(1, MyNB.build());
}
I used Toast message to find my broadcastreceiver works or not and find broadcast works properly and only notification has problem
Try this code :
private void MyNotification(Context context) {
String NotificqationText = "NotificqationText";
String NotificationTitle = "NotificationTitle ";
String NotificationTicker = "NotificationTicker";
Intent intent = new Intent(this, Splash.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |(Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TASK));
PendingIntent MyPendingIntent = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
Bitmap MyPicture = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon);
Notification MyNB = new Notification.Builder(this)
.setSmallIcon(R.drawable.icon)
.setLargeIcon(MyPicture)
.setStyle()
.setBigContentTitle(NotificationTitle)
.setContentTitle(NotificationTitle)
.setContentText(NotificqationText)
.setTicker(NotificationTicker)
.setAutoCancel(true)
.setContentIntent(MyPendingIntent)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
MyNB.flags |= Notification.FLAG_SHOW_LIGHTS;
MyNB.flags |= Notification.FLAG_AUTO_CANCEL;
MyNB.defaults = Notification.DEFAULT_ALL;
notificationManager.notify((int)System.currentTimeMillis(), MyNB);
}

onclick to get the content of Local notification generated from database android

I have generated multiple Local notifications by matching data from database.
Now i want that on clicking on the notification it will take to the particular products.
Code:----------
for (int i=0;i < alertList.size();i++) {
Random random = new Random();
int m = random.nextInt(9999 - 1000) + 1000;
msg=alertList.get(i).getProf_name()+" have to take "+alertList.get(i).getMed_name()+" ;"+alertList.get(i).getMed_id();
Log.d("MediMSg",""+msg);
Log.d("MediMSg",""+msg);
Intent notificationIntent = new Intent(context, MedicineNotificationActivity.class);
notificationIntent.putExtra("pushmsg",msg);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MedicineNotificationActivity.class);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(100, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Notification notification = builder.setContentTitle("Test")
.setContentText(msg)
.setTicker("Notification from Test")
.setSmallIcon(getNotificationIcon())
.setPriority(Notification.PRIORITY_MAX)
.setAutoCancel(true)
.setColor(0x0091ea)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(pendingIntent).build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(m,notification);
Log.d("Random",""+m);
}

Android Wear Preview Stacked Notifications

Having trouble sending stacked notifications using setGroup. The moment I invoke setGroup no notifications are being sent on the device or the Android Wear emulator. Some sample code...
Intent intent1 = new Intent(this, AddActivity.class);
PendingIntent pIntent1 = PendingIntent.getActivity(this, 0, intent1, 0);
NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(this);
nBuilder.setContentTitle("Fence Monitor").setContentText("FENCE " + status).setSmallIcon(R.drawable.ic_launcher).setContentIntent(pIntent1);
NotificationCompat.Builder nBuilder1 = new NotificationCompat.Builder(this);
Notification secondNotification = nBuilder1.setContentTitle("Fence Monitor").setContentText("This is additional information related to this notification").setSmallIcon(R.drawable.ic_launcher).build();
WearableNotifications.Action.Builder aBuilder = new WearableNotifications.Action.Builder(android.R.drawable.ic_input_add,"Add Content",pIntent1);
WearableNotifications.Action action = aBuilder.build();
RemoteInput.Builder rBuilder = new RemoteInput.Builder(QUICK_REPLY);
RemoteInput rInput = rBuilder.setAllowFreeFormInput(true).setLabel("QUICK REPLY").build();
WearableNotifications.Builder wBuilder = new WearableNotifications.Builder(nBuilder);
Notification notification = wBuilder.setGroup(FENCE_NOTIFICATIONS_GROUP,order).addAction(action).addPage(secondNotification).addRemoteInputForContentIntent(rInput).build();
//Notification notification = wBuilder.setGroup(FENCE_NOTIFICATIONS_GROUP,order).build();
NotificationManagerCompat nManager = NotificationManagerCompat.from(this);
int notificationId = (new Random()).nextInt();
Log.d("Notification Id",""+notificationId);
nManager.notify(notificationId, notification);
I was playing around with stacked notifications and realized that having a summary notification is needed for your notification to show up (at-least in my case it was). The android documentation: https://developer.android.com/wear/notifications/stacks.html#AddSummary mentions that is important but doesn't say it is required.
Either ways, try this and see if it works. I was able to get your code to work by adding a summary notification.
/**
* Create a summary notification
*/
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.alert_dialog_icon);
WearableNotifications.Builder wearableBuilder = new WearableNotifications
.Builder(builder)
.setGroup(FENCE_NOTIFICATIONS_GROUP,
WearableNotifications.GROUP_ORDER_SUMMARY);
Notification summaryNotification = wearableBuilder.build();
/**
* Notify. First publish the summary notification and then send out the
* other multi-page notification.
*/
NotificationManagerCompat nManager = NotificationManagerCompat.from(this);
int notificationId = (new Random()).nextInt();
Log.d("Notification Id",""+notificationId);
nManager.notify(notificationId, summaryNotification);
notificationId = (new Random()).nextInt();
Log.d("Notification Id",""+notificationId);
nManager.notify(notificationId, notification);
The WearableNotificationsSample included in the Android Wear Developer Preview shows how you can build stacked notifications:
public Notification[] buildNotifications(Context context, BuildOptions options) {
NotificationCompat.Builder childBuilder1 = new NotificationCompat.Builder(context)
.setContentTitle(context.getString(R.string.first_child_content_title))
.setContentText(context.getString(R.string.first_child_content_text));
Notification child1 = new WearableNotifications.Builder(childBuilder1)
.setGroup(EXAMPLE_GROUP_KEY, 0)
.build();
NotificationCompat.Builder childBuilder2 = new NotificationCompat.Builder(context)
.setContentTitle(context.getString(R.string.second_child_content_title))
.setContentText(context.getString(R.string.second_child_content_text))
.addAction(R.mipmap.ic_app_notification_studio,
context.getString(R.string.second_child_action),
NotificationUtil.getExamplePendingIntent(
context, R.string.second_child_action_clicked));
Notification child2 = new WearableNotifications.Builder(childBuilder2)
.setGroup(EXAMPLE_GROUP_KEY, 1)
.build();
Notification summary = buildBasicNotification(context, options)
.setGroup(EXAMPLE_GROUP_KEY, WearableNotifications.GROUP_ORDER_SUMMARY)
.build();
return new Notification[] { summary, child1, child2 };
}
Just recently I did a blog post which had a complete example that you might want to try out: http://android-developers.blogspot.com/2014/05/stacking-notifications-for-android-wear.html
Bitmap bitmapMila = BitmapFactory.decodeResource(getResources(), R.drawable.mila128);
// Nuke all previous notifications and generate unique ids
NotificationManagerCompat.from(this).cancelAll();
int notificationId = 0;
// String to represent the group all the notifications will be a part of
final String GROUP_KEY_MESSAGES = "group_key_messages";
// Group notification that will be visible on the phone
NotificationCompat.Builder builderG = new NotificationCompat.Builder(this)
.setContentTitle("2 Pet Notifications")
.setContentText("Mila and Dylan both sent messages")
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(bitmapMila);
Notification summaryNotification = new WearableNotifications.Builder(builderG)
.setGroup(GROUP_KEY_MESSAGES, WearableNotifications.GROUP_ORDER_SUMMARY)
.build();
// Separate notifications that will be visible on the watch
Intent viewIntent1 = new Intent(this, MainActivity.class);
PendingIntent viewPendingIntent1 =
PendingIntent.getActivity(this, notificationId+1, viewIntent1, 0);
NotificationCompat.Builder builder1 = new NotificationCompat.Builder(this)
.addAction(R.drawable.ic_action_done, "Treat Fed", viewPendingIntent1)
.setContentTitle("Message from Mila")
.setContentText("What's for dinner? "
+ "Can we have steak?")
.setSmallIcon(R.drawable.ic_launcher);
Notification notification1 = new WearableNotifications.Builder(builder1)
.setGroup(GROUP_KEY_MESSAGES)
.build();
Intent viewIntent2 = new Intent(this, MainActivity.class);
PendingIntent viewPendingIntent2 =
PendingIntent.getActivity(this, notificationId+2, viewIntent2, 0);
NotificationCompat.Builder builder2 = new NotificationCompat.Builder(this)
.addAction(R.drawable.ic_action_done, "Water Filled", viewPendingIntent2)
.setContentTitle("Message from Dylan")
.setContentText("Can you refill our water bowl?")
.setSmallIcon(R.drawable.ic_launcher);
Notification notification2 = new WearableNotifications.Builder(builder2)
.setGroup(GROUP_KEY_MESSAGES)
.build();
// Issue the group notification
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(notificationId+0, summaryNotification);
// Issue the separate wear notifications
notificationManager.notify(notificationId+2, notification2);
notificationManager.notify(notificationId+1, notification1);

NotificationCompat.BigTextStyle Content disappears on new notification

String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(ns);
int icon = R.drawable.ic_launcher;
CharSequence tickerText = title;
long when = System.currentTimeMillis();
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if(alarmSound == null){
alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
if(alarmSound == null){
alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
}
Intent intent = new Intent();
PendingIntent pendingIntent
= PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.BigTextStyle bigxtstyle =
new NotificationCompat.BigTextStyle();
bigxtstyle.bigText(text);
bigxtstyle.setBigContentTitle(title);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setStyle(bigxtstyle)
.setSmallIcon(icon)
.setAutoCancel(true)
.setSound(alarmSound)
.setDeleteIntent(pendingIntent)
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0));
Notification noti = mBuilder.build();
mNotificationManager.notify(notificationid++, noti);
This code works, and displays the text as advertised with word wrapping. However, when a subsequent notification happens the previous notification loses its text. Can anyone help resolve this issue? It could be something I'm setting incorrectly, I am new to the android apis.
Android only displays one expanded notification by default. Your notification doesn't lose its content, it just gets compressed and only shows the normal one-line content. But it seems that you don't specify this, therefore the compressed notification is empty.
You may use setContentTitle() and setContentText() to set the one-line texts.

Categories

Resources