I want to show notifications like on picture. If there is more than one, I want to show a counter too. I didn't find info in official doc. Now I just update my notification by id:
((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE))
.notify(PUSH_NOTIFICATION_ID, notification);
How can I do it ?
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext());
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentTitle(topic);
mBuilder.setContentText(new String(message.getPayload()));
mBuilder.setAutoCancel(true);
mBuilder.mNumber = 1;//get your number of notification from where you have save notification
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(notify_id, mBuilder.build());
To create a stack, call setGroup() for each notification you want in the stack and specify a group key.
final static String GROUP_KEY_EMAILS = "group_key_emails";
// Build the notification, setting the group appropriately
Notification notif = new NotificationCompat.Builder(mContext)
.setContentTitle("New mail from " + sender1)
.setContentText(subject1)
.setSmallIcon(R.drawable.new_mail)
.setGroup(GROUP_KEY_EMAILS)
.build();
// Issue the notification
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
notificationManager.notify(notificationId1, notif);
Reference: https://developer.android.com/training/wearables/notifications/stacks.html
Related
Below is the Settings screen of Facebook app. In my app it displays "Category", here it is "Kalis Martinez". I want to achieve that in my app.
What my app is displaying is as below:
What I am doing is as below.
NotificationManager manager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannelGroup(new
NotificationChannelGroup(groupId, groupName));
String GROUP_KEY_WORK_EMAIL = "com.android.example.WORK_EMAIL";
Notification notification = new Notification.Builder(getApplicationContext(), SECONDARY_CHANNEL)
.setContentTitle(title)
.setContentText(body)
//build summary info into InboxStyle template
.setStyle(new Notification.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)
.setSmallIcon(getSmallIcon())
.setAutoCancel(true);
manager.notify(id, notification.build());
setGroup() and setGroupSummary() do grouping while displaying notifications in System tray.
How to change that label?
Use this for create a notification Group to change title like this
// The id of the group.
String groupId = "my_group_01";
// The user-visible name of the group.
CharSequence groupName = getString(R.string.group_name);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.createNotificationChannelGroup(new NotificationChannelGroup(groupId, groupName));
Or check about Notification Channels
I am working on an app in android in which i wish to notify specific users about specific events. These notifications are made by using the the Firebase Cloud Messaging data-messages. When I send a data-message to the users client, the onMessageReceived-method is called and I can handle these messages as I wish.
Now I want to make the device vibrate in the very moment the messages arrives. Therefore, I tried to build a notification and set the vibration on it, however it does not vibrate at all..
I included the VIBRATE-permission in my applications Manifest as well.
Here is my Code to build the notification:
NotificationCompat.Builder mBuilder =
(NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_icon)
.setContentTitle("Content Title")
.setContentText("Content Text")
.setVibrate(new long[] {500,500,500,500,500,500,500,500,500});
Notification note = mBuilder.build();
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, note);
Have I missed anything?
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic)
.setContentTitle(context.getString(R.string.text))
.setContentText(context.getString(R.string.app_name))
.setContentIntent(contentIntent);
mBuilder.setVibrate(new long[]{500, 500});
mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
NotificationManager mNotifyMgr =
(NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
mNotifyMgr.notify(001, mBuilder.build());
This works for me.
I am trying to modify existing notifications in android.
What I have in my app
When a notification is already in system tray and another notification appears, the second one overwrites the first notification content.
What I am looking for
If second Notification arrives then instead of overwriting the first I need to change title to show 2 New Messages and go on incrementing as notifications arrive.
Code Implemented
Bitmap icon = BitmapFactory.decodeResource(ctx.getResources(),
R.drawable.icon);
Intent launchActivity = new Intent(ctx, CordovaApp.class);
launchActivity.putExtra("heading",newsHeader);
launchActivity.putExtra("content",newsText);
PendingIntent pi = PendingIntent.getActivity(ctx,0, launchActivity, PendingIntent.FLAG_NO_CREATE);
ParseAnalytics.trackAppOpened(launchActivity);
if(pi==null){
Log.d(TAG, "Pending Intenet is null.");
}else{
Log.d(TAG, "Pending Intenet is not null.");
}
Notification noti = new NotificationCompat.Builder(ctx)
.setContentTitle(newsHeader)
.setContentText(newsText)
.setSmallIcon(R.drawable.icon)
.setLargeIcon(icon)
.setContentIntent(pi)
.setAutoCancel(true)
.build();
NotificationManager nm = (NotificationManager)ctx.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(0, noti);
Update
I implemented the solution mentioned below by #yogendra and now I am getting two separate notifications. Instead of getting stacked. Below is updated code
Notification noti = new NotificationCompat.Builder(ctx)
.setContentTitle(newsHeader)
.setContentText(newsText)
.setSmallIcon(R.drawable.icon)
.setGroup(GROUP_KEY_EMAILS)
.setLargeIcon(icon)
.setContentIntent(pi)
.setLights(Color.parseColor("green"), 5000, 5000)
.setAutoCancel(true)
.setPriority(2)
.setTicker("Notification from App")
.setGroupSummary(true)
.build();
NotificationManager nm = (NotificationManager)ctx.getSystemService(Context.NOTIFICATION_SERVICE);
int timeSeconds = (int)System.currentTimeMillis()%Integer.MAX_VALUE;
Log.i(TAG,"Timing function called "+timeSeconds);
nm.notify(timeSeconds, noti);
See your code
nm.notify(0, noti);
where
notify(int id, Notification notification)
Here 0 is the ID of notification which is to be managed with respect to each notification. If you want to show a different notification your notification id should be unique each time. If you try to post a notification with the same notification id your previously displayed notification will be replaced with the latest notification.
Solution
Now you need to display a custom notification with a custom layout and update the counter each time .
Source code to create a Custom Notification.
create global variable in your class :
private int count = 0;
private ArrayList<String> notificationList = new ArrayList<String>();
private String GROUP_KEY_EMAILS = "email";
/call method createNotification when you need to create notification and pass message what you need to show on message./
private void createNotification(String notificationMassage) {
notificationList.add(notificationMassage);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
// Create builder
Builder summaryNotification = new NotificationCompat.Builder(this)
.setContentTitle(notificationList.size()+" new messages")
.setSmallIcon(R.drawable.settings)
.setLargeIcon(largeIcon)
.setGroup(GROUP_KEY_EMAILS)
.setGroupSummary(true)
.setAutoCancel(true);
// Create style
InboxStyle nStyle = new NotificationCompat.InboxStyle();
nStyle.setBigContentTitle(notificationList.size()+" new messages");
nStyle.setSummaryText("Summery Text...<you can set as blank>");
for (String Str : notificationList) {
nStyle.addLine(Str);
}
summaryNotification.setStyle(nStyle);
mNotificationManager.notify(0, summaryNotification.build());
count++;
}
/**
please clear notification array list after tap on notification.
*/
For more detail please refer below link:
https://developer.android.com/training/wearables/notifications/stacks.html#AddGroup
https://developer.android.com/training/wearables/notifications/stacks.html#AddGroup
As Richard said in this question > Possible to continuously update text in the notification area? , I have the same issue here .
I want to update text in the notification area .
This question is answered and accepted , but the answer doesn't help me .
It's about to create text as bitmap dynamically and set it to Small Icon of notification .
But as Richard commented , the images for setSmallIcon must be predefined in the package.There is no ability to edit them on the fly.
Please kindly show me the right way to do this stuff .
It's quite simple to update your Notification.
First of all when you create it you must create it with an id.
NotificationManager mNotificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Sets an ID for the notification, so it can be updated
int notifyID = 1;
mNotifyBuilder = new NotificationCompat.Builder(this)
.setContentTitle("Title")
.setContentText("Text")
.setSmallIcon(R.drawable.ic_notify);
mNotificationManager.notify(
notifyID,
mNotifyBuilder.build());
Now to update your Notification you only need to notify the new notification with the old id and this will be updated (you don't need to reset the paraneter you don't want to update).
mNotifyBuilder.setContentText(setContentText("New Text")
mNotifyBuilder.setSmallIcon(R.drawable.ic_new_notify);
//this update your notification
mNotificationManager.notify(
notifyID,
mNotifyBuilder.build());
I tried to make one but it has one disadvantage is that, it has one empty notification in notification drawer,
public static int when = 0;
private void generateNotification(Context context) {
Log.i(TAG, "generateNotification");
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(
this.getApplicationContext(), 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mNotification = new NotificationCompat.Builder(
this)
// .setContentTitle("AppName")
// .setContentText("Message For Notification Drawer")
// .setSound(soundUri)
// .setDefaults(Notification.DEFAULT_SOUND)
// .setVibrate(new long[] { 1000, 1000 })
// .addAction(R.drawable.ic_launcher, "View", pIntent)
// .addAction(0, "Remind", pIntent)
// .setNumber(fCount)
// .setWhen(when)
.setSmallIcon(R.drawable.ic_launcher)
.setTicker(Integer.toString(when)) // Set String you want
.setAutoCancel(true)
.setContentIntent(pIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// notificationManager.notify(when, mNotification.build());
notificationManager.notify(1, mNotification.build());
when++;
}
Some question about push notifications. How do I dissmis all the notifications of my app when I press one of them? Also ... how do I dissmiss the noifications if I start the app (not from the noification)?
Here is how I send a notification from my GcmIntentService class (if it is helpfull).
private void sendNotification(Bundle extras) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
NOTIFICATION_ID = Integer.parseInt(extras.getString("id"));
Intent in = new Intent(this, PushActivity.class);
in.putExtras(extras);
in.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
String msg = extras.getString("msj");
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
in, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("MY TITLE")
.setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
I'm not sure if the notifications you are putting out are for the same event, if so you can do what is called "stacking notifications" like Gmail does when you get a second or third email. It's all in the one notification and therefore will go away as one. This is only compatible above 4.1.
This is the code Google gives along with the stacking. This any help?
mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Sets an ID for the notification, so it can be updated
int notifyID = 1;
mNotifyBuilder = new NotificationCompat.Builder(this)
.setContentTitle("New Message")
.setContentText("You've received new messages.")
.setSmallIcon(R.drawable.ic_notify_status)
numMessages = 0;
// Start of a loop that processes data and then notifies the user
...
mNotifyBuilder.setContentText(currentText)
.setNumber(++numMessages);
// Because the ID remains unchanged, the existing notification is
// updated.
mNotificationManager.notify(
notifyID,
mNotifyBuilder.build());
try android.app.NotificationManager.cancelAll()