private void triggerNotification(String s)
{
CharSequence title = "TASK";
CharSequence message = s;
notificationManager = (NotificationManager)c.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.vianetlogo, s, System.currentTimeMillis());
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
PendingIntent pendingIntent = PendingIntent.getActivity(c, 0, null, 0);
notification.setLatestEventInfo(c, title, message, pendingIntent);
notificationManager.notify(NOTIFICATION_ID, notification);
}
Here it's saving the last notification in status bar if there are mutiple notifications at the same time. Is there any way that it will save mutiple notifications on status bar ?
your answer is here :
If the PendingIntent has the same operation, action, data, categories, components, and flags it will be replaced.
Depending on the situation i usually solve this by providing a unique request code either as static values (0,1,2) or the row id of the data I'm receiving from the DB.
Related
I am developing an application in which i implemented Push notification functionality.
My code of onMessage - GCMIntentService.java is:
#Override
protected void onMessage(Context context, Intent data) {
String message = data.getExtras().getString("message");
displayMessage(context, message);
generateNotification(context, message);
}
And code of generateNotification -
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, GCMMessageView.class);
notificationIntent.putExtra("message", message);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
This code is working fine. I am getting push message and notification.
=>But when i send more then one message the notification gets over write. And i am able to see only last notification in status bar.
The need of application is that - if i send more then one notification then all should be display in status bar.
So please guide me what should i do for that in my code.
Try to put different notification id (i.e. nid) in intent and notify instead of 0 for each new notification this will prevent over writing your notification
PendingIntent intent = PendingIntent.getActivity(context, nid,notificationIntent, 0);
and
notificationManager.notify(nid, notification);
Hope this will help you
Get random numbers:
Random random = new Random();
int m = random.nextInt(9999 - 1000) + 1000;
Instead of:
notificationManager.notify(0, notification);
use:
notificationManager.notify(m, notification);
You need to update your Notification ID when you fire a Notification. in
notificationManager.notify(ID, notification);
How?
To set up a notification so it can be different, issue it with a notification ID by calling NotificationManager.notify(ID, notification). To change this notification once you've issued it, create a NotificationCompat.Builder object, build a Notification object from it, and issue the Notification with the Different ID you are not used previously.
If the previous notification is still visible, the system updates it from the contents of the Notification object. If the previous notification has been dismissed, a new notification is created instead.
For more information go to official docs
hello you need to pass unique notification id in notify method.
Below is the code for pass unique notification id:
//"CommonUtilities.getValudeFromOreference" is the method created by me to get value from savedPreferences.
String notificationId = CommonUtilities.getValueFromPreference(context, Global.NOTIFICATION_ID, "0");
int notificationIdinInt = Integer.parseInt(notificationId);
notificationManager.notify(notificationIdinInt, notification);
// will increment notification id for uniqueness
notificationIdinInt = notificationIdinInt + 1;
CommonUtilities.saveValueToPreference(context, Global.NOTIFICATION_ID, notificationIdinInt + "");
//Above "CommonUtilities.saveValueToPreference" is the method created by me to save new value in savePreferences.
Let me know if you need more detail information or any query. :)
notificationManager.notify(Different_id_everytime, notification);
I am using Phonegap with GCM plugin to show GCM notification.
I am able to display notification correctly but I am not able to show multiple notification. I tried Notification passes old Intent Extras but No results yet.
Please help.
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
int iUniqueId = (int) (System.currentTimeMillis() & 0xfffffff);
// saveCallIntent.setData((Uri.parse("custom://"+System.currentTimeMillis())));
Intent notificationIntent = new Intent(context, JainDharma.class);
// set intent so it does not start a new activity
//notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, iUniqueId, notificationIntent, 0);
//PendingIntent contentIntent = PendingIntent.getActivity(context, UNIQUE_INT_PER_CALL, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
//notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
If you mean that the last notification overrides the previous one, and you wish to change it, you need to change the following :
notificationManager.notify(0, notification);
Use a different id (instead of 0) for each notification and you'll see all of them.
However, you might want to reconsider whether it's such a good idea to show multiple notifications for the same app. I would find that annoying as a user of your app.
i spend a lot of time to resolve my problem. i have written a messenger client in android. my application receive income message and raise a notification. in notification bar i display each income message in a notification item. when click to notification item it will open a conversation activity to list all messages from the beginning until now. everything dose perfect but when i click another item in notification bar, nothing happen! (it must reload data for another conversation). This is my code to raise a notification:
private void showNotification(String message, Class activity, Message messageObject) {
//Get the Notification Service
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence text = message;//getText(R.string.service_started);
Notification notification = new Notification(R.drawable.ic_launcher, text, System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent callbackIntent = new Intent(context, activity);
if(messageObject != null)
{
callbackIntent.putExtra("conversation", MessageManager.getProvider().getConversation(messageObject.getConversationId()));
}
//callbackIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
int myUniqueValue = new Random().nextInt();
PendingIntent contentIntent = PendingIntent.getActivity(context, myUniqueValue, callbackIntent, PendingIntent.FLAG_ONE_SHOT);
notification.setLatestEventInfo(context, messageObject.getFrom(), text, contentIntent);
notificationManager.notify(messageObject.getFrom(), myUniqueValue, notification);
}
This is code block to call showNotification function
showNotification(message.getBody(), ConversationActivity.class, messageObject);
Despite your efforts to supply a unique value, those PendingIntents are all treated as the same by the system, so once you click one, the rest are rendered inert.
You'll need to add something distinguishing to callbackIntent; I suggest inventing a data URI that includes the conversation ID or something else that's guaranteed to be different for each notification (see setData).
Finally, I'd encourage you to try to collapse multiple notifications into one icon—you don't want to spam the user. See the Notifications section in the Android Design guide, under "Stack your notifications".
i changed my code and it work very well
private void showNotification(Context context, CharSequence contentTitle, CharSequence contentText, CharSequence notificationContent, Class activity, Conversation conversation) {
//Get the Notification Service
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, notificationContent, System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent callbackIntent = new Intent(context, activity);
if(conversation != null)
{
callbackIntent.putExtra("conversation", conversation);
}
callbackIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
int myUniqueValue = new Random().nextInt();
PendingIntent contentIntent = PendingIntent.getActivity(context, myUniqueValue, callbackIntent, PendingIntent.FLAG_ONE_SHOT);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notificationManager.notify(myUniqueValue, notification);
}
I want to show a notification after receiving a WAP push message in my phone.I know that NOTIFICATION_SERVICE is i need to be used. But i don't know how to use this. Please help me to overcome this problem. I am able to receive the message in my phone and it is printing in the log correctly. Written some code for showing the notification but it is not working. Attaching the code along with please have a look on it and expecting a proper guiding on this.
private void showNotification(String text,Context con) {
Notification n = new Notification();
n.flags |= Notification.FLAG_SHOW_LIGHTS;
n.flags |= Notification.FLAG_AUTO_CANCEL;
n.defaults = Notification.DEFAULT_ALL;
n.icon = R.drawable.ic_sms_wap;
//n.when = System.currentTimeMillis();
// Simply open the parent activity
// Change the name of the notification here
//n.setLatestEventInfo(this, NOTIF_TITLE, text, pi);
mNotifMan.notify(NOTIF_CONNECTED, n);
}
hard to answer without knowing exactly what your problem is (ie do you get an error etc..) but you could try this:
private void showNotification(String text,Context con) {
if (mNotifMan==null) {
mNotifMan=(NotificationManager) con.getSystemService(NOTIFICATION_SERVICE);
}
Notification n = new Notification(R.drawable.ic_sms_wap,text,System.currentTimeMillis());
n.flags |= Notification.FLAG_SHOW_LIGHTS;
n.flags |= Notification.FLAG_AUTO_CANCEL;
n.defaults = Notification.DEFAULT_ALL;
// n.icon = R.drawable.ic_sms_wap;
// n.when = System.currentTimeMillis();
// Simply open the parent activity
// Change the name of the notification here
//n.setLatestEventInfo(this, NOTIF_TITLE, text, pi);
mNotifMan.notify(null,NOTIF_CONNECTED, n);
}
I am currently developing an android app where I create a notification Intent which adds parameters to the bundle of the intent. When the notification is clicked it calls an activity and gets the data from the bundle. However, the first time the app is used it works fine, but when you click on a different item its supposed to pass different data onto the notification activity but for some reason its not replacing the old data with the new.
I have tried to call bundle.removeExtra("companyPassword") before I use the putExtra but it doesn't seem to make any difference. Below is the code for the notification
private void notification(String companyName, String companyURL, String companyUsername, String loginPassword)
{
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager)getSystemService(ns);
int icon = R.drawable.icon;
CharSequence tickerText = "Click notification to copy password";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence contentTitle = "PM - Login Management";
CharSequence contentText = "Click here to copy password";
Intent notificationIntent = new Intent(ShowLogins.this, DataManagement.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
notificationIntent.removeExtra("companyName");
notificationIntent.removeExtra("companyURL");
notificationIntent.removeExtra("companyUsername");
notificationIntent.removeExtra("companyPassword");
notificationIntent.putExtra("companyName", companyName);
notificationIntent.putExtra("companyURL", companyURL);
notificationIntent.putExtra("companyUsername", companyUsername);
notificationIntent.putExtra("companyPassword", loginPassword);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
final int NOTIFICATION_ID = 1;
notification.defaults |= Notification.DEFAULT_SOUND;
//notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(NOTIFICATION_ID, notification);
finish();
And below is the code for the Notification activty where it retrieves the data that is passed into the bundle
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
// setContentView(R.layout.data_mangement);
Bundle bundle = this.getIntent().getExtras();
company = bundle.getString("companyName");
companyURL = bundle.getString("companyURL");
username = bundle.getString("companyUsername");
password = bundle.getString("companyPassword");
bundle.clear();
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
Encryption encryption = new Encryption();
String decryptedPassword = encryption.decrypt(password);
clipboard.setText(decryptedPassword);
toastNotification("Password Successfully Copied\n\nPaste into password field");
bundle.clear();
finish();
moveTaskToBack(true);
} catch (Exception ex) {
Log.d("DataManagement Error", ex.toString());
}
}
For some reason when the activity for the notification is called it is only returning the data that was first sent when an item was first selected instead of retrieving the new data.
Thanks for any help you can provide.
However, the first time the app is used it works fine, but when you click on a different item its supposed to pass different data onto the notification activity but for some reason its not replacing the old data with the new.
That is because you are not sufficiently changing your Intent and you are not using FLAG_UPDATE_CURRENT in your PendingIntent. Try the latter and see if it helps.