In my application I'm displaying notification text and icon on the notification bar but my problem is that when the user presses clear on the notification bar it gets cleared. I want to prevent it!? And some other issues too:
I want to create notifications for my application by different activities, say one on start it displays "Welcome to app", the second activity displays "please select", in "Data send activity" it displays "Records sent successfully"!!
How to remove the notification on application exit.
How to disable users to clear the notification when they press the clear button on the notification bar
How to remove a notification from the status bar when the user presses it to open an activity?
Any help?
My current code
private void Notification(String notificationTickerText, String Title,
String text, Notification nt) {
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.nicon;
CharSequence tickerText = notificationTickerText;
long when = System.currentTimeMillis();
nt = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence contentTitle = Title;
CharSequence contentText = text;
Intent notificationIntent = new Intent(this, frmLogin.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
nt.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notificationManager.notify(1, nt);
}
If you read http://developer.android.com/guide/topics/ui/notifiers/notifications.html that will probably answer all your questions.
It mentions that you can use the following to prevent a notification from being cleared:
FLAG_NO_CLEAR flag
Add this to the flags field to indicate that the notification should not be cleared by the "Clear notifications" button. This is particularly useful if your notification is on-going.
You can use the FLAG_AUTO_CANCEL to cancel your notifications, but I'm not entirely sure if that will work when combined with FLAG_NO_CLEAR. If it doesn't you'll have to cancel the notification manually.
Related
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 would like to control Launching Application from notification bar. So i would like to intercept any action in any icon in this bar. the code given below give an example of how a notification icon start the application that much with it.
private void handleCommand(Intent intent){
// In this sample, we'll use the same text for the ticker and the expanded notification
CharSequence text = getText(R.string.service_running);
// Set the icon, scrolling text and timestamp
Notification notification = new Notification(R.drawable.statusbar_icon, text,
System.currentTimeMillis());
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, AppLockerActivity.class), 0);
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, text,
text, contentIntent);
startForegroundCompat(R.string.service_running, notification);
startMonitorThread((ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE));
}
I would like to detect all the intent and implement a service of authentication that need a password befor running applications from the notification bar.
Ok so, if i understood well the question...why don't make Intent to send to some kind of Login section?
Like this:
private void handleCommand(Intent intent){
// In this sample, we'll use the same text for the ticker and the expanded notification
CharSequence text = getText(R.string.service_running);
// Set the icon, scrolling text and timestamp
Notification notification = new Notification(R.drawable.statusbar_icon, text,
System.currentTimeMillis());
//create an intent and add some Extra or whatever else you need
Intent intent = new Intent(this, YOUR_LOGIN_CLASS.class);
intent.addExtra("WHATEVER TO RETRIEVE TO SEE IF COME FROM NOTIFICATION",whatever);
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
intent, 0);
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, text,
text, contentIntent);
startForegroundCompat(R.string.service_running, notification);
startMonitorThread((ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE));
}
I raised a notification . What i want is , that when the user clicks on the notification , my activity is brought to the front without creating a new instance of it .
For this , I added the flag , REORDER_TO_FRONT , but still oncreate is being called instead of onNewIntent when i click on the notification .
This is my code -
int icon = R.drawable.android;
long when = System.currentTimeMillis();
CharSequence text = "new message";
CharSequence contentTitle = stanza.getFrom(); // message title
CharSequence contentText = stanza.getBody(); // message text
Intent notificationIntent = new Intent(context, ChatBox.class);
notificationIntent.putExtra("buddyid",stanza.getFrom());
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,notificationIntent, 0);
// the next two lines initialize the Notification, using the configurations above
Notification notification = new Notification(icon, text, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(1,notification);
Have u tried:
Intent.FLAG_ACTIVITY_CLEAR_TOP
with your notificationIntent.addFlag();
The solution for me for this was to make a broadcast receiver that listens to broadcast actions that the notification triggers. So basically:
Notification triggers a broadcast action with an extra the name of the activity to launch.
Broadcast receiver catches this when the notification is clicked, then creates an intent to launch that activity using the FLAG_ACTIVITY_REORDER_TO_FRONT flag
Activity is brought to the top of activity stack, no duplicates.
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?
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.