I am trying to recieve multiple notifications on my mobile.But each time i send the notification.The previous notification gets overwritten by the new one.I watched the other questions where they said to have multiple Id's for notifications I am doing that also but I don't know where I am going wrong.
Here's how I create my notification.(It is being created in a service).
private void GenerateNotification(String data)
{
String ns=Context.NOTIFICATION_SERVICE;
manager=(NotificationManager) getSystemService(ns);
int icon=R.drawable.ic_launcher;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, data, when);
notification.flags |=Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
Context context = getApplicationContext();
CharSequence contentTitle = "The Best Essay";
CharSequence contentText = data;
Intent notificationIntent = new Intent(this,MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
manager.notify(HELLO_ID, notification);
HELLO_ID++;
}
Where HelloID increments to recieve multiple notifications with unique id's.Please tell me where I am doing wrong.
So the issue is that the intent you're using is the same each time. If you try to notify the user using an Intent that is the same as one used in a notification that is already being displayed, android considers them duplicates. This is because each notification when clicked will end up doing the exact same thing (so android is like "why do I need to display two of these?").
The thing to do is say "hey, am I already displaying a notification? If so, I'm going to create a new notification that will override the current one, but convey the fact that there are actually two things that I'm notifying about". Consider the text messaging application. When you get a second unread text message, it overrides the first notification about the original text message and replaces it with a notification tell you that you have two new text messages. Make sense?
Related
My app sends notifications but the problem is that if there are some notification pending in the action bar, I want to update the message adding the new message to the old message.
For example, if the message is Hi!, and the device receive another message (Pamela) before the user has opened the old message, I want to show only one notification with the message Hi! Pamela.
My code for send notifications is:
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, msg, when);
String title = this.getString(R.string.app_name);
Intent notificationIntent = new Intent(getApplicationContext(),
FirstActivity.class);
notificationIntent.putExtra("message", msg);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
int notifyID = 1;
PendingIntent intent = PendingIntent.getActivity(this, notifyID,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(this, title, msg, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
Is it possible to get the old message of PendingIntent?
Thanks in advance.
One way to do this is to concatenate the string, by saving what is already displayed like, before the following is called for the first time:
notificationIntent.putExtra("message", msg);
//save "Hi" , concat "Pamela" update the notification with the same notification id
You save the string msg, then concatenate the string that you receive in the next notification. However, to identify, which notifications contain parts of String to be concatenated, is required in this case. Either you can set prefixes to messages or identify by notification id.
Not the ideal way to do it i feel, but can't think of anything else.
It's not possible to retrieve the existing notification from the NotificationManager. You need to keep track of it yourself independently of the notification, and save the unread messages somewhere else (maybe in a SQLite Database or SharedPreferences file).
You will need to determine if the user swipes away the notification, which means you will have to use the NotificationCompat.Builder class to create the notification. You can use the setDeleteIntent method to supply an Intent to trigger when the notification is swiped away, at which point you delete the old messages from wherever you are storing them so that a new message will show a notification with only the new message.
I have included a unique id for creating PendingIntent as well as in mNM.notify() method. When I set two notifications to display at the same time they do not get displayed simultaneously. The first notification get displayed with the time given for the second notification. Many people have had this problem and the only suggestion was to give unique IDs. But that doesn't work! Please Help. Below is my showNotification() method.
private void showNotification() {
/*create intent for show notification details when user clicks notification*/
Intent intent =new Intent(getApplicationContext(), MainActivity.class);
Random random = new Random();
int id = random.nextInt();
intent.setData((Uri.parse("custom://"+System.currentTimeMillis())));
// This is the 'title' of the notification
CharSequence title = "Reminder!" + id;
// This is the icon to use on the notification
int icon = R.drawable.ic_dialog_alert;
// This is the scrolling text of the notification
CharSequence text = task;
// What time to show on the notification
long time = System.currentTimeMillis();
Notification notification = new Notification(icon, text, time);
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this,id, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, title, text, contentIntent);
// Clear the notification when it is pressed
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Send the notification to the system.
mNM.notify((int)System.currentTimeMillis(), notification);
// Stop the service when we are finished
stopSelf();
}
I haven't tried this unsupported answer.
I think your looking for the .setOngoing(true)
The user can't cancel this notification so I imagine the OS can't cancel it either.
I am using push notifications in my app. I need to display a notification when a push notification delivered. If I send another notification (without clearing the previous notification), it will replace the old notification.
This is the code I use:
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.ic_launcher;
CharSequence tickerText = "New notification Pending";
long time = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, time);
notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
// Context context = getApplicationContext();
CharSequence contentTitle = "Notifications";
CharSequence contentText = newMessage;
Intent notificationIntent = new Intent(this, LoginActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText,
contentIntent);
mNotificationManager.notify(1, notification);
But I don't want to replace the notification, I want to add it as a new notification.
You can also use System.currentTimeMillis() to assign unique id to your notification.
int id = (int) System.currentTimeMillis();
mNotificationManager.notify(id, notification);
You need to supply a different ID as the notification ID each time. The best approach would be to send an ID field to GCM which can be then accessed via Intent.getExtras().getInt() in your GCMIntentService's onMessage() method.
If this is not possible, I'd suggest using something like (int)Math.random()*10 to generate a random integer number as your notification ID. This will (partially) ensure that your notifications will not replace each other.
simple you have to
change Notification id
mNotificationManager.notify(1, notification);
instead of 1
for more refer this Link
Use a new notification ID every time instead of hardcoding to 1:
int i = x; //Generate a new integer everytime
mNotificationManager.notify(i, notification);
I am not sure what your use case is, but the Android design guidelines recommend not doing it at all.
Don't:
Do:
We need Unique notification id which will generate the new notifications.
Post a notification to be shown in the status bar. If a notification with
the same id has already been posted by your application and has not yet been canceled, it will be replaced by the updated information.
#param id An identifier for this notification unique within your application.
#param notification A {#link Notification} object describing what to show the user.
Must not be null.
public void notify(int id, Notification notification)
{
notify(null, id, notification);
}
Example :
int id =(int) System.currentTimeMillis();
mNotificationManager.notify(id, notify);
I have some kind of SMS application. So everytime the phone received a new message, it should have a notification and upon clicking it, will launch the activity. Right now, when receiving 1 message, it notifies, removes in the status bar and doesn't not launch the activity. But when receiving 2 or more messages, the first notification cannot launched upon clicking while the rest(2nd, 3rd notification...) can. Below are my codes.
Intent newIntent = new Intent(context, PreviewActivity.class);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK+0);
newIntent.setAction(strFxPrimaryKey);
Bundle newBundle = intent.getExtras();
newBundle.putInt(GlobalConstants.PRIMARY_ID, Integer.parseInt(strFxPrimaryKey));
newIntent.putExtras(newBundle);
int icon = R.drawable.notification_fx;
CharSequence tickerText = context.getString(R.string.fx_received);
long when = System.currentTimeMillis();
Notification newNotification = new Notification(icon, tickerText, when);
newNotification.flags |= Notification.FLAG_AUTO_CANCEL; //| Notification.FLAG_ONGOING_EVENT;
PendingIntent contentIntent = PendingIntent.getActivity(context, Integer.parseInt(strFxPrimaryKey), newIntent, 0);
newNotification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
String newNotificationService = Context.NOTIFICATION_SERVICE;
NotificationManager newNotificationManager = (NotificationManager) context.getSystemService(newNotificationService);
newNotificationManager.notify(Integer.parseInt(strFxPrimaryKey), newNotification);
context.startActivity(newIntent);
context.removeStickyBroadcast(intent);
Based on the answers here in the stackoverflow, to create a unique intent, it should have unique action that's why I set the primary key as the action. I also set the request code to primary key to have a unique pending intent. Is there something missing out in my code? Thanks.
EDITED
By the way, whenever I remove the context.startActivity(newIntent);, it works right. Can anyone tell me why? Thanks.
I was trying to switch from tabview (those took about 1/3rd of a small screen) to fullscreens selectable via notification messages.
So far so good, everything done following the instructions on many howto's its all working like a charm. (that is on the sdk emulator)
now i've transfered the app to my actual android telephone and now it doesn't switch the screens anymore via the notifications. it ALWAYS opens the MainActivity.
private void DroiDCNotification(int NotificationID, CharSequence tickerText, CharSequence contentTitle, CharSequence contentText) {
//throw new UnsupportedOperationException("Not yet implemented");
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.droidc_icon; // icon from resources
long when = System.currentTimeMillis(); // notification time
Context context = getApplicationContext(); // application Context
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
// the next two lines initialize the Notification, using the configurations above
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(NotificationID, notification);
}
So how can i make the notification called by a specific Activity open that specified Activity?
I was trying to switch from tabview (those took about 1/3rd of a small screen) to fullscreens selectable via notification messages.
I have no idea why you think that this is a good idea. Notifications are not designed for this role. Please use an options menu.
So how can i make the notification called by a specific Activity open that specified Activity?
It is going to execute the PendingIntent. Your PendingIntent wraps an Intent identifying MainActivity.class. If you do want it to use MainActivity.class, change MyActivity.class to whatever class you wish.
Hi Just use this twolines in yr activity
Intent notificationIntent = new Intent(context, SamplePushActivity.class);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);