how to control notification icons? - android

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));
}

Related

Why is multiple notifications not displayed simultaneously?

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.

android - notifications without alerting the user

I currently have an ongoing notification that displays some information about my app. While this information is useful, there's no need for the user to actually see that I've created the notification. Its basically there to tell them that "hey, my app is doing stuff in the background, click here to go back to it".
My notification works correctly, however whenever I call ".notify()" my notification shows the text from my notification in the notification bar at the top of the screen, like a text message does.
I want to to do it silently, so when I call notify it doesn't show my notifications text in the small notification bar before doing the "roll up" animation.
Context context = getApplicationContext();
Intent notificationIntent = new Intent(this, AudioPlayer.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
Notification notification = new Notification(R.drawable.ic_launcher,
title, System.currentTimeMillis());
RemoteViews contentView = new RemoteViews(getPackageName(),
R.layout.note_layout);
~~~ remote views stuff ~~~
contentView.setTextViewText(R.id.notification_title, title);
contentView.setTextViewText(R.id.notification_text, author);
notification.contentView = contentView;
notification.contentIntent = contentIntent;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
mNotificationManager.notify(8675309, notification);
I'm not sure that the OS allow that.
What you want to hide is called Ticker.
You can try to set an empty Ticker Text:
Notification notification = new Notification(R.drawable.ic_launcher,
"", System.currentTimeMillis());

Flagging issues in android

I'm trying to get my Notification to not cancel when the user presses "Clear All" So far I have the intent working properly on everything except this:
Intent intent = new Intent(getBaseContext(), MainActivity.class);
intent.addFlags(Notification.FLAG_ONGOING_EVENT);
intent.addFlags(Notification.FLAG_NO_CLEAR);
PendingIntent contentIntent = PendingIntent.getActivity(
getBaseContext(), 0, intent, 0);
The question I have at this point is: Are my flags correct?
Yes, your flags look pretty much correct, although I don't know if you even need the FLAG_NO_CLEAR. I currently have an app which creates an ongoing (non-cancellable) notification - I only use the FLAG_ONGOING_EVENT and it works fine for me. I pretty much just copied it from a tutorial and then added the ongoing event flag.
Here's some sample code:
String text = "notification";
Notification notification = new Notification(R.drawable.icon, text,
System.currentTimeMillis());
//launch the activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent().setComponent(ComponentName.unflattenFromString("com.example/com.example.MyActivity")), 0);
// set the label and text...
notification.setLatestEventInfo(this, getText(R.string.notification_label),
text, contentIntent);
notification.flags = Notification.FLAG_ONGOING_EVENT;
// Send the notification.
// We use a string id because it is a unique number. We use it later to cancel.
NotificationManager mNM;
mNM.notify(R.string.notification_label, notification);

Notification text clears on press clear on status bar?

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.

make notification when clicking app bring to front

I want make a notification, that when clicked on it will bring my app from the background to the front. I am using the following code:
NotificationManager noma = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
PendingIntent pen = PendingIntent.getActivity(Timer.this, 0, intent, 0);
intent.putExtra("key", "trigerred");
String body = "This is a message";
String title = "This is title";
Notification no = new Notification(R.drawable.ic_launcher, body, System.currentTimeMillis());
no.defaults = Notification.DEFAULT_ALL;
no.setLatestEventInfo(this, title, body, pen);
noma.notify(uniqueID, no);
When I click on the notification that makes a new intent but I want the last created intent brought to the front. How i can do this?
You need to set the FLAG_ACTIVITY_SINGLE_TOP flag on the intent that you pass to getActivity.
This should bring you back to the all ready running activity when clicking your notification.
See here for a list of the different launch flags.
Try this
PendingIntent pen = PendingIntent.getActivity(Timer.this, 0, intent,Intent.FLAG_ACTIVITY_TASK_ON_HOME);

Categories

Resources