Starting an activity from a notification - android

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

Related

using NotificationManager into an android service

I have a service running in the background and I want to notify user if there is a new data, I've used a notificationmanager, and the notification appears, but when clicking on it it doesn't do anything (it is supposed to show an activity
I'm new in android and here is my code
NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = newNotification(R.drawable.shoppingcarticon, "Nouvelle notification de ShopAlert", System.currentTimeMillis());
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(this,GooglemapActivity.class);
PendingIntent activity = PendingIntent.getService(this, 0, intent, 0);
notification.setLatestEventInfo(this, "This is the title", "This is the text", activity);
notification.number += 1;
notificationmanager.notify(0, notification);
your help will be appreciated.
You can use this code. Change Main.Class to your activity class, And Title and content text as you need them.
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.ic_action_search;
CharSequence tickerText = "Pet Parrot";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Context context = getApplicationContext();
CharSequence contentTitle = "Hungry!";
CharSequence contentText = "your parrot food meter is: ";
Intent notificationIntent = new Intent(context, Main.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(1, notification); // Log.d("test", "Saving Data to File from Service.");
The following line:
PendingIntent activity = PendingIntent.getService(this, 0, intent, 0);
should look this way:
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
since you're trying to start an Activity, not a Service. Hope this helps.

Using an application's notification to invoke another application/activity

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.

Android notification not working

I've been attempting to get a notification of a successful upload from an ASyncTask to work all day. I'm not getting any errors from my current code but I can't get the notification to show in the notification bar (or anywhere else). I get no messages in LogCat and no notification appears in the Notification bar. This is my code:
Notification mNotification = new Notification(icon, tickerText, when);
CharSequence contentTitle = "upload completed.";
CharSequence contentText = "upload completed.";
Intent notificationIntent = new Intent(context, CastrActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_NO_CREATE);
mNotification.contentIntent = contentIntent;
mNotification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mNotification);
This is called from the onPostExecute() method of an ASyncTask. I'm a bit confused on the PendingIntent part, to be honest. Any clarification of what I suspect to be incorrect code there would be greatly appreciated.
Even though your problem is solved, I'll just post how I solved my problem that the notification was not showing, perhaps it might help other people reading the answers:
In my notification building I was missing the icon. As soon as I added something like setSmallIcon(R.drawable.ic_launcher) the notification was shown.
I have created the class to show notifications:
public class NotificationData {
public static NotificationManager mNotificationManager;
public static int SIMPLE_NOTFICATION_ID;
private Context _context;
public NotificationData(Context context) {
_context = context;
}
public void clearNotification() {
mNotificationManager.cancel(SIMPLE_NOTFICATION_ID);
}
public void SetNotification(int drawable, String msg, String action_string, Class cls) {
mNotificationManager = (NotificationManager) _context.getSystemService(Context.NOTIFICATION_SERVICE);
final Notification notifyDetails = new Notification(drawable, "Post Timer", System.currentTimeMillis());
long[] vibrate = { 100, 100, 200, 300 };
notifyDetails.vibrate = vibrate;
notifyDetails.ledARGB = 0xff00ff00;
notifyDetails.ledOnMS = 300;
notifyDetails.ledOffMS = 1000;
// notifyDetails.number=4;
notifyDetails.defaults =Notification.DEFAULT_ALL;
Context context = _context;
CharSequence contentTitle = msg;
CharSequence contentText = action_string;
Intent notifyIntent = new Intent(context, cls);
// Bundle bundle = new Bundle();
// bundle.putBoolean(AppConfig.IS_NOTIFICATION, true);
notifyIntent.putExtras(bundle);
PendingIntent intent = PendingIntent.getActivity(_context, 0,notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent);
mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
}
}
How to use this class:
NotificationData notification; //create object
notification = new NotificationData(this);
notification.SetNotification(R.drawable.notification, "Notification Title", "Click to open", YourClassName.class);
Add permission android.permission.VIBRATE
Try this:
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.icon; // icon from resources
CharSequence tickerText = "Any thing"; // ticker-text
long when = System.currentTimeMillis(); // notification time
Context context21 = getApplicationContext(); // application Context
CharSequence contentTitle = "Anything"; // expanded message title
CharSequence contentText = (CharSequence) extras.get("message"); // expanded message text
Intent notificationIntent = new Intent(this, MainStart.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.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
/* long[] vibrate = { 0, 100, 200, 300 };
notification.vibrate = vibrate;
notification.ledARGB = Color.RED;
notification.ledOffMS = 300;
notification.ledOnMS = 300;*/
notification.setLatestEventInfo(context21, contentTitle, contentText, contentIntent);
mNotificationManager.notify(Constants.NOTIFICATION_ID, notification);
Another thing to try is to make sure your manifest contains
<permission android:name="android.permission.STATUS_BAR_SERVICE" android:protectionLevel="signature" />
Also mine seemed to ignore successive notifications with the same NOTIFICATION_ID.
For me this kept happening and I had no idea why but the problem was that the icon I set was too large so it was giving me some random error.

Activity fails to update on intent receive

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.

Android: How to resume an App from a Notification?

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"

Categories

Resources