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.
Related
I am working on an android app, and I am currently trying to create a notification, and once the user clicks on the notification, an activity should be launched. The notification is being created fine but the activity is not being started.
Below is the code for the notification.
private void showNotification(String username, String password)
{
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager notificationManager = (NotificationManager)context.getSystemService(ns);
CharSequence tickerText = "Username Copied";
long when = System.currentTimeMillis();
int icon = R.drawable.icon;
Notification notification = new Notification(icon, tickerText, when);
CharSequence contentTitle = "BPM - Login Management";
CharSequence contentText = "Username Copied";
Intent intentNotification = new Intent(context, CopyPassword.class);
intentNotification.setAction(Long.toString(when));
notification.flags |= Notification.FLAG_AUTO_CANCEL;
intentNotification.putExtra("password", password);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intentNotification, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
final int NOTIFICATION_ID = 1;
notificationManager.notify(NOTIFICATION_ID, notification);
}
The notificiation is being generated from within a normal java class not an android activity in case this makes any difference.
Thanks for your help.
Remove the setAction this isn't needed.
Try:
Intent intentNotification = new Intent(context, CopyPassword.class);
intentNotification.putExtra("password", password);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intentNotification, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
Also ensure the CopyPassword Activity is declared in your AndroidManifest
I wrote a blog post about it: http://blog.blundell-apps.com/notification-for-a-user-chosen-time/ :-D
When I click on my status bar notification it loads my webview, but it reloads it everytime. is there a way to load the saved state? Thanks here is what I have:
#Override
public void onStart(Intent intent, int startid) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.notification_icon;
CharSequence tickerText = "Now playing...";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence contentTitle = "Music Promotion";
CharSequence contentText = "Now Playing...";
Intent notificationIntent = new Intent(this, mainmenu.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(HELLO_ID, notification);
}
add the following:
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
This is what the docs say about FLAG_ACTIVITY_REORDER_TO_FRONT:
If set in an Intent passed to Context.startActivity(), this flag will
cause the launched activity to be brought to the front of its task's
history stack if it is already running.
i changed the launch mode in the android manifest to singleInstance and that seemed to have done the trick
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.
The following code gives the same content after I send two notifications with different content say Content1 and Content2. The resulting activity always shows only Content2. What could be the reason for it?
public void onReceive(Context context, Intent intent) {
abortBroadcast();
mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.icon;
CharSequence tickerText = intent.getStringExtra("NOTIFICATION_TITLE");
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
CharSequence contentTitle = intent.getStringExtra("NOTIFICATION_TITLE");
CharSequence contentText = intent.getStringExtra("NOTIFICATION_DETAILS");
Intent notificationIntent = new Intent(context,CustomActivity.class);
notificationIntent.putExtra("TITLE", contentTitle);
notificationIntent.putExtra("DETAILS", contentText);
//notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(notifUUID.hashCode(), notification);
}
Got the answer! the fix was simple - just added:
notificationIntent.setAction(String.valueOf(notifUUID.hashCode()));
Any unique value (timestamp would have worked too) set as the intent action would work.
I am trying to program my notification to RESUME my app, instead of simply starting a new instance of my app... I am basically looking for it to do the same thing as when the Home button is long-pressed and the app is resumed from there.
Here is what I am currently doing:
void notifyme(String string){
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager)
getSystemService(ns);
int icon = R.drawable.notification_icon; // icon from resources
CharSequence tickerText = string + " Program Running..."; // ticker-text
long when = System.currentTimeMillis(); // notification time
Context context = getApplicationContext(); // application Context
CharSequence contentTitle = *********; // expanded message title
CharSequence contentText = string + " Program Running...";//expanded msg text
Intent notificationIntent = new Intent(this, Main.class);
PendingIntent contentIntent = PendingIntent.getActivity(
this, 0, notificationIntent, 0);
// the next two lines initialize the Notification, using the configurations
// above
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText,
contentIntent);
final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
}
I am guessing that the new Intent line is where the problem lies... any help would be appreciated!
you need to set your flags
notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Also, if you never ever want there to be a duplicate activity give it this attribute in the manifest
android:launchMode="singleTask"