How to call Dialog box from Notification bar in android? - android

I would like to call an android dialog box from Notification bar in android instead of leading to a url.Here is my code which I am using to call a url from notification.Tell me how to do it for a dialog box.Thanks
notifyDetails.defaults |= Notification.DEFAULT_ALL;
Context context = getApplicationContext();
CharSequence contentTitle = "Notification Details...";
CharSequence contentText = "Browse Android Official Site by clicking me";
Intent notifyIntent = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("http://www.android.com"));
PendingIntent intent =
PendingIntent.getActivity(SimpleNotification.this, 0,
notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent);
mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);

just make the pendingintent open up one of your activities and have your activity be complete transparent and just open a dialog.
EDIT adding flag to intent
Intent notifyIntent = new Intent(context,YourActivityClassHere.class);
notifyIntent.setFlag(Intent.FLAG_ACTIVITY_NEW_TASK);
//UNIQUE_ID if you expect more than one notification to appear
PendingIntent intent = PendingIntent.getActivity(SimpleNotification.this, UNIQUE_ID,
notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Related

Remove Notification from Notification area once it is clicked

I am displaying a notification whenever a new message is received that contains particular keywords. I have used following code to show the notification in the notification area,
String contentTitle = "V-Card Received";
String contentText = "You have reeived a new V-Card";
mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, receiveVCard.class);
notificationIntent.putExtra("sender", sender);
notificationIntent.putExtra("vCardString", messages[i].getDisplayMessageBody());
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
int icon = R.drawable.contactvcard;
CharSequence tickerText = "V-Card Received";
long when = System.currentTimeMillis();
notifyDetails = new Notification(icon, tickerText, when);
notifyDetails.setLatestEventInfo(context, contentTitle, contentText, pendingIntent);
mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
notifyDetails.flags =Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
Now I want to remove the notification once the user clicks on it. I Have used Notification.FLAG_AUTO_CANCEL to cancel the notification. But it is not removing the notification even if the user clicks on it. Is there any other way to remove the notification, when the user clicks on the notification.
You are basically setting flags after notification has been put.
You need to swap the last two lines of the code you have provided. Set flags before calling nm.notify();
try this it works fine for me
--> 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;
notificationManager.notify(i, notification);
this is the prototype of the notification i used in one of my app
Notification notification=new Notification(R.drawable.ic_stat_download_interrupted,getResources().getString(R.string.dint),System.currentTimeMillis());
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification_download_complete);
contentView.setImageViewResource(R.id.notimage, R.drawable.ic_stat_download_interrupted);
contentView.setTextViewText(R.id.nottext, getResources().getString(R.string.dint));
contentView.setTextViewText(R.id.nottitle, update.initialDetail.fileName);
notification.contentView = contentView;
notification.flags=Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(getApplicationContext(), MainDashboard.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.putExtra(EXTRA_NOTIFICATION_SHOW_DOWNLOADS, true);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(),0,notificationIntent, 0);
notification.contentIntent=contentIntent;
nm.notify(update.updateId.intValue(), notification);
its working for me
notifyDetails.flags =Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL;
Hope this is helpful for you.

Android:Notification starts new activity instead of resuming old one if originally activity is not started from launcher

I have spent a week to find a solution but with no success, so I need a help.
When my application goes to background then notification appears and displays status of activity.
It works perfectly if the application is started from launcher (with action = android.intent.action.MAIN and category = android.intent.category.LAUNCHER).
But if the application is started f.e. from the gallery using action android.intent.action.SEND or android.intent.action.SEND_MULTIPLE and category android.intent.category.DEFAULT, then notification starts new activity instead of resuming existing one.
The code for notification:
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager notificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.icon;
Notification notification = new Notification(icon, null, 0);
notification.flags |= Notification.FLAG_ONGOING_EVENT|Notification.FLAG_AUTO_CANCEL|Notification.FLAG_ONLY_ALERT_ONCE;
Context context = this.getApplicationContext();
CharSequence contentTitle = "XXX";
CharSequence contentText = "XXX";
Intent notificationIntent = new Intent(context, MyClass.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent activityIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, activityIntent);
notificationManager.notify(NOTIFICATION_ID, notification);
In other words, I have an application "My Application" with activity "My Activity". If "My Activity" is started on the top of gallery and creates notification, then after pressing the notification "My application" is started instead of resuming the gallery with "My activity".
Do you have any idea how to heal it?
Intent myIntent = new Intent(this, MainActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(
getBaseContext(), 0, myIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notification.flags = notification.flags
| Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_NO_CLEAR;

How to write a notification that does absolutly nothing when clicked?

Ive seen many examples of how to make something happen when the user clicks on a notification but I actually don't want anything to happen. If the user clicks on the notification I want the notification to simply dissapear and the user NOT taken anywhere.
In my code below, while FLAG_AUTO_CANCEL clears the notification from the status bar, when the user clicks on a my notification they are taken to "MyActivity".
How can I create a notification that does nothing?
Notification notification = new Notification(R.drawable.success, res.getString(R.string.messages_sent), System.currentTimeMillis());
//Define the expanded message and intent
Context context = getApplicationContext();
CharSequence contentTitle = res.getString(R.string.messages_sent_ellipsis);
Intent notificationIntent = new Intent(this, MyActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0 );
notification.setLatestEventInfo(context, contentTitle, mStrSuccessMessage, contentIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
//Pass the notification to the notification manager
mNotificationManager.notify(1, notification);
Changing
Intent notificationIntent = new Intent(this, MyActivity.class);
to
Intent notificationIntent = new Intent();
would do the job. The gist is if you want nothing, give it a blank intent.

Android Notification

How to pass value of notification's when one row is clicked ,to another activity for displaying details in android?
Check this out:
Notification n = new Notification(icon, text, when);
String contentTitle = "I am expanded title";
String contentText = "I am expanded text";
Intent intent = new Intent(this, TargetActivity.class);
// Put additional stuff into the created intent
PendingIntent contentIntent = PendingIntent.getActivity(this, 1, intent, 1);
n.setLatestEventInfo(this, contentTitle, contentText, contentIntent);
Put your information in the intent, using Intent.putExtra methods, and retrieve them in the target activity.

Android Status Bar Notifications - Opening the correct activity when selecting a notification

I have been having a problem with a notification not opening/going to the correct activity when it has been clicked.
My notification code (located in a class which extends Service):
Context context = getApplicationContext();
CharSequence contentTitle = "Notification";
CharSequence contentText = "New Notification";
final Notification notifyDetails =
new Notification(R.drawable.icon, "Consider yourself notified", System.currentTimeMillis());
Intent notifyIntent = new Intent(context, MainActivity.class);
PendingIntent intent =
PendingIntent.getActivity(context, 0,
notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT | Notification.FLAG_AUTO_CANCEL);
notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent);
((NotificationManager)getSystemService(NOTIFICATION_SERVICE)).notify(NOTIFICATION_ID, notifyDetails);
If I click the notification while the application which created the service is open, the notification disappears (due to the FLAG_AUTO_CANCEL) but the activity does not switch.
If I click the notification from the home screen, the notification disappears and my app is brought to the front, however it remains on the activity which was open before going to the home screen, instead of going to the main screen.
What am I doing wrong? How do I specify the activity that will be pulled up?
May have actually answered my own question:
Intent notifyIntent = new Intent(Intent.ACTION_MAIN);
notifyIntent.setClass(getApplicationContext(), Main.class);

Categories

Resources