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.
Related
How can I use an application's notification to start a new application. The NotificationManager does not provide me with a method that lets me access the generated notifications. Any suggestions? Thanks.
You have to create notification with PendingIntent. From Android Developers:
Context context = getApplicationContext();
CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";
Intent notificationIntent = new Intent(this, MyClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
private static final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
Where MyClass is activity you want to start from notification.
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 be able to fire a notification to alert the users about a timer that has finished, however i do not wish to have an intent when you click the notification.
I've tried passing in null for the intent
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
int icon = R.drawable.icon;
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";
notification.setLatestEventInfo(context, contentTitle, contentText, null);
mNotificationManager.notify(HELLO_ID, notification);
You may pass the parameter
PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0)
instead of
null
on
notification.setLatestEventInfo(context, contentTitle, contentText, null);
The last parameter in setLatestEventInfo() is a PendingIntent and not an Intent. If you need the notification to not do anything when tapped is to pass an empty PendingIntent which is done as follows: PendingIntent.getActivity(context, 0, null, 0).
Interesting question and I would like to see if this works. I did a little digging and found a lot of ppl asking the same question. cbursk seems to have found a hack to get this intended functionality, which is to pass a Dialog to the notification intent instead of an Activity. I'm guessing the Dialog does nothing, or is programmed to dismiss itself right away, not sure. But I'm currently looking at this thread and going to test it out.
I'm trying to send 2 strings using Notification, but I don't know how should I do it. I used putExtra for my strings, but on the other activity I get null. I don't know what to do on the other activity. I read about this on net, but I didn't undersand. Can anyone help me?
Here is my code :
private void setNotifiy(){
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence NotificationTicket = "You have a notification";
//CharSequence NotificationContent = ;
CharSequence contentTitle = "You are close to "+name_shop+"!!!";
CharSequence contentText ="So,you can go for shopping:)";
long when = System.currentTimeMillis();
Notification notification = new Notification(R.drawable.icon,
NotificationTicket, when);
Context context = getApplicationContext();
Intent notificationIntent = new Intent(this, ShopsOnMap.class);
notificationIntent.putExtra("latshop",lat_choose);
notificationIntent.putExtra("longshop", long_choose);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notificationManager.notify(NOTIFICATION_ID, notification);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
}
In the class ShopsOnMap I wrote just this :
String q = getIntent().getStringExtra("latshop");
System.out.println("!!"+q+"$$");
and I obtain q=null.
I need a simple example, or any idea that could help me. Thanks.
Use FLAG_UPDATE_CURRENT in your getActivity() call, instead of 0, for the fourth parameter, so your new Intent extras are applied to the PendingIntent.
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.