I want to stack notifications using setGroup (as described here: https://developer.android.com/training/wearables/notifications/stacks.html)
Basically, I use 0 as notification id (always the same) and builder.setGroup("test_group_key") but a new notification always replaces the previous one.
What could be the problem ?
Code:
public BasicNotifier(Context context) {
super(context);
notifManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setSound(alarmSound)
.setAutoCancel(true);
stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(getParentActivityClass());
}
public void showNotification(String title, String text, Intent intent, Class cls) {
if (text.length() > 190)
text = text.substring(0, 189) + "...";
mBuilder.setTicker(text).setContentText(text).setContentTitle(title);
Intent notificationIntent = intent == null ? new Intent() : new Intent(intent);
notificationIntent.setClass(getContext(), cls);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
mBuilder.setGroup("test_group_key");
Notification notif = mBuilder.build();
notif.flags |= Notification.FLAG_AUTO_CANCEL;
notifManager.notify(replaceOnNew ? 0 : nextId++, notif); // replaceOnNew
// is "true"
Log.i(TAG, "Notification shown: " + nextId + " = " + title);
}
EDIT:
It seams there is a problem when using NotificationManagerCompat, the notifications are not being displayed at all.
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(getContext());
notificationManager.notify(id, notif);
You don't use notification id correctly.
"To set up a notification so it can be updated, issue it with a notification ID by calling NotificationManager.notify(ID, notification). To update this notification once you've issued it, update or create a NotificationCompat.Builder object, build a Notification object from it, and issue the Notification with the same ID you used previously."
from Android Developer
So in your case, if you want to stack notification in your group, you need to specify a new id for each new notification.
Related
I have currently setup a small application which sets an alarm on the device and stores relevant information inside of an SQL database e.g. Alarm name and time. When the alarm is activated my application sends a notification to the user. Currently each notification is static which means every message is the same. I would like to now allow my application to grab the name of the alarm which has been activated and display it in the notification. Now I know that when setting multiple alarms you require multiple ID's which I have stored in side of my SQL and I'm guess the same when it comes to sending a notification. Is there a way of matching my Alarms ID with one on a notification so that it knows what message to send e.g. alarm name?
Current code for setting my Alarm
pendingIntent = PendingIntent.getBroadcast(view.getContext(), Integer.valueOf(alarm_request_code), receiver, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, myCalendar.getTimeInMillis(), pendingIntent);
intentArrayList.add(pendingIntent);
My Broadcast receiver...
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
Intent moveActivity = new Intent(context, AlarmActivity.class);
moveActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, moveActivity, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setContentTitle("Standard text")
.setContentText("Standard text")
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true);
notificationManager.notify(0, builder.build());
}
//////////////
Update on my situation which is slowly driving me crazy
I can now set the text from my SQL to my notification but its only the first from my database each time. Now I've come up with two possible solutions which are only theory's.
Is there a way in which each time the database is called to the notification it will move onto the next result?
As I mentioned above I'm setting an alarm and when the alarm goes off it then calls the notification. Now In my pending intent for the alarm I'm giving it a id which you can see as alarm_request_code is there a way of giving it to my notification ID and then setting up a statement where if the notification ID is equal to or in the list of my alarm ID's stored on my SQL then it will search for the correct text to input.
MyDatabaseHandler myDatabaseHandler = new MyDatabaseHandler(context, null, null, 1);
Cursor cursor = myDatabaseHandler.getAllProducts();
// Information we are trying to acquire but can only get first result
String name = cursor.getString(cursor.getColumnIndexOrThrow("alarm_name"));
String date = cursor.getString(cursor.getColumnIndexOrThrow("alarm_date"));
// Alarm ID FROM SQL which we want to match with the NOTIFICATION ID.....
String alarm_request_code = cursor.getString(cursor.getColumnIndexOrThrow("alarm_code"));
////
Log.i("The reciever is working", "perfect");
//create an intent to service
Intent service_Intent = new Intent(context, MessageService.class);
context.startService(service_Intent);
//Notification
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
Intent moveActivity = new Intent(context, AlarmActivity.class);
moveActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Works with moveActivity to move the user back to main application.
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, moveActivity, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setContentTitle(name)
.setContentText(date)
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true);
notificationManager.notify(Integer.valueOf(alarm_request_code), builder.build());
}
}
To update text store mBuilder variable
and update later in this way:
mBuilder.setContentTitle(head);
mBuilder.setContentText(body);
mNotificationManager.notify(SIMPLE_ID, mBuilder.build());
EDIT
private void simple() {
Intent intent = new Intent(context, AlarmActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle(getResources().getString(R.string.app_name))
.setContentText(getResources().getString(R.string.app_name))
.setContentIntent(pIntent);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
mBuilder.setSmallIcon(R.drawable.ic_done_24dp);
} else {
mBuilder.setSmallIcon(someiconLollipop);
mBuilder.setColor(somecolorLollipop]);
}
noti = mBuilder.build();
//use flags if you need:
noti.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
mNotificationManager.notify(SIMPLE_ID, mBuilder.build());
}
to update the text:
private void showText() {
String head = yournewhead;
String body = yournewbody;
mBuilder.setContentTitle(head);
mBuilder.setContentText(body);
mNotificationManager.notify(SIMPLE_ID, mBuilder.build());
}
Per the docs you can do the following:
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());
The trick here is your notify id, which must be the same in order to behave appropriately, in your case 0.
So as long it has not been dismissed it should work accordingly. I will suggest having a unique id as a constant for your custom notification such as 123.
notificationManager.notify(0, builder.build());
I have an application that receive PUSH notifications. When notification "not1" is received, it shows as usual using notification manager. But when "not2" is received, is not showing below "not1" but overriding it. I need show all notifications.
I thought that using different requestCode for PendingIntent will solve the problem, but not. I also tried with PendingIntent.FLAG_UPDATE_CURRENT flag and many others (including no flags). My code right now is like this:
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = null;
Notification notification = null;
Intent notificationIntent = new Intent(context.getApplicationContext(), SplashActivity.class);
notificationIntent.putExtra("text", text);
notificationIntent.putExtra("url", url);
PendingIntent intent = PendingIntent.getActivity(context, requestID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder = setBuilderNormalNotification(builder, context, text, intent);
notification = new NotificationCompat.BigTextStyle(builder).bigText(text).build();
notificationManager.notify(0, notification);
Where function setBuilderNormalNotification is the next:
private static NotificationCompat.Builder setBuilderNormalNotification(NotificationCompat.Builder builder, Context context, String text, PendingIntent intent)
{
builder
.setContentTitle(context.getString(R.string.app_name))
.setSmallIcon(getNotificationIcon())
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), getNotificationIcon()))
.setContentText(text)
.setContentIntent(intent)
.setWhen(0)
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
.setAutoCancel(true);
return builder;
}
What Am I doing wrong, please?
Thanks a lot.
In your code notificationManager.notify(0, notification); That 0 is the constant ID you are using, since it is always the same the notifications stack.
My app generates notifications like so :
final Bundle intentExtras = intent.getExtras();
final String title = intentExtras.getString(Constants.EXTRA_TITLE);
final String body = intentExtras.getString(Constants.EXTRA_BODY);
final Integer notifID = intentExtras.getInt(Constants.EXTRA_ID);
// this pending intent will launch after notification is selected
Intent notificationIntent = new Intent(context, NotificationReceiverActivity.class);
notificationIntent.putExtras(intentExtras);// pass the extra data forward to NotificationReceiverActivity
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, notifID, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
// build notification to show on the notification bar
try
{
final String packagename = context.getPackageName();
PackageInfo packageinfo = context.getApplicationContext().getPackageManager().getPackageInfo(packagename, 0);
final int iconid = packageinfo.applicationInfo.icon;
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setAutoCancel(false)
.setContentIntent(pendingIntent)
.setSmallIcon(iconid)
.setContentTitle(title)
.setContentText(body);
// build notification and set alerts
Notification notification = mBuilder.build();
notification.defaults |= Notification.DEFAULT_ALL;
// show notification
NotificationManager notificationMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationMgr.notify(notifID, notification);
}
catch (android.content.pm.PackageManager.NameNotFoundException e)
{}
each notifID is unique ! However, if the app is showing two or more notifications, and the user selects one of them, both notifications for the app are cleared !
I have tried using setAutoCancel(false) or setAutoCancel(true) but it doesn't make a difference.
Additionally, there are no calls to NotificationManager.cancel() or NotificationManager.cancelAll()
How can I prevent the app from clearing all notifications for the app after a user has selected just one of them ?
FURTHER INVESTIGATION...
I've found that the notifications for the app disappear after I launch the app.
However, no .cancel() or .cancelAll() in sight... /confused
Answer: Crosswalk (XWalkView) took the liberty of .cancelAll() notifications !!!
/facepalm x 100000
https://github.com/wuhengzhi/crosswalk/commit/32205e256dbb35ee70c895de4fe7c76d165b30dd
I implemented Android app in Android device, I also have received the notification from the server.But the problem I am facing is, when the receive activity is open for 1st notification data and when I receive the 2nd Notification, on clicking the notification 2nd, it is not replacing the Receive activity with updated notification data.
Please help me on this
The following code is being used:-
private void sendNotification(String msg) {
Log.d(TAG, "Preparing to send notification...: " + msg);
mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
Context context = getApplicationContext();
//Notification notification = new Notification(icon, message, when); //UM
Log.e(TAG, "Input Extra is...: " + msg);
Intent notificationIntent = new Intent(context, ResultShowActivity.class);
notificationIntent.putExtra("Message",msg);
//notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP );
int iUniqueId = (int) (System.currentTimeMillis() & 0xfffffff);
int when= (int) System.currentTimeMillis() ;
//PendingIntent contentIntent = PendingIntent.getActivity(this, iUniqueId,notificationIntent, 0);
PendingIntent contentIntent = PendingIntent.getActivity(this, iUniqueId,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT );
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.gcm_cloud)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setAutoCancel(true)
.setSound(alarmSound)
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
//mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
mNotificationManager.notify(when, mBuilder.build());
Log.d(TAG, "Notification sent successfully.");
}
Welcome to Stack Overflow! To get the best out of this website, you may want to be more specific about your issue. Perhaps add the affected code.
In your case though, it looks like the issue is around your PendingIntent flags. Add FLAG UPDATE CURRENT to update the extras bundle, if that is what you meant to do.
I want to auto cancel my notification when user clicks on notification. The following code works good in all the devices except Android lollipop device. In Lollipop device, notification goes only when user swipes it off.
#SuppressWarnings("deprecation")
public void sendNotification(int id){
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Test")
.setContentText("Jump to next screen")
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true);
mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONLY_ALERT_ONCE;
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, NextActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0,
resultIntent, 0);
//PendingIntent.FLAG_UPDATE_CURRENT
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// id, if there is a need to update the notification later on.
mNotificationManager.notify(id, mBuilder.build());
Log.v(TAG, "Notification ID value " + id);
//mNotificationManager.cancel(id);
}
What is missing in this code?
Looks good to me. My auto cancel still works on 5.0.2. Let me give you a portion of my code:
public static void notif(int id, String title, String text, Context context, Class activity) {
// get an instance of the NotificationManager service
if (mNotifyMgr == null)
mNotifyMgr = (NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(context, activity);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent notifIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.launcher)
.setContentTitle(title)
.setContentText(text)
.setContentIntent(notifIntent);
mNotifyMgr.notify(id, mBuilder.build());
}
modify following:
setAutoCancel(true) -> setAutoCancel(false);
then on action activity do the following
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancelAll();
if you want to set special condition you can set by
intent.putExtra("key", "value")