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.
Related
I have a method for creating notification. I want to clear notification number as soon as a user clicks on status notification.
public void createNotification(){
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification=null;
notification = new Notification(R.drawable.ic_launcher, "You have a new message", System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.number = **count**++;
Intent intent = new Intent();
intent.setClass(TabInterfaceActivity.this, TabInterfaceActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.contentIntent = activity;
notification.setLatestEventInfo(this, " New Message", message, notification.contentIntent);
}
If by "I want to clear notification number" you mean you want to keep the notification itself still displayed, but you want just to update/remove counter, then you should set your notification PendingIntent to point to your code that would handle this scenario for you, and when it completes it shall redirect to your target TabInterfaceActivity (or you can make it more flexible and pass target in PendingIntent's Bundle).
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);
I would like to create a notification that clears automatically when the user presses it and has no other behaviuour. The notification below works fine but when i press on it it takes me to the activity "MyActivity" (even having to define an intent seems a bit unecessary when I don't want to use it...)
Using FLAG_AUTO_CANCEL doesn't seem to have any effect at all.
Update: Sorry, I have found the FLAG_AUTO CANCEL does work, that is the notification is cleared from the status bar. I guess I am really tring to write an intent that does nothing (or completely delete the intent).
Code...
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("My content title");
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);
Use an empty intent without an actual action.
PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(), 0);
I don't think you can. You can have MyActivity exit immediately though: call finish() from onCreate().
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 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);