10

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.

Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
Carnivoris
  • 793
  • 3
  • 7
  • 23

5 Answers5

31

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.

McIntosh
  • 2,051
  • 1
  • 22
  • 34
4

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

Heisenberg
  • 1,500
  • 3
  • 18
  • 35
Munish Kapoor
  • 3,141
  • 2
  • 26
  • 41
  • I'm sorry but what is AppConfig? Is there a library I need to include to use that? Eclipse doesn't seem to know it if there is so I'd have to add it to my build path. – Carnivoris Jan 27 '12 at 15:51
  • Appconfig is a class and the IS_NOTIFICATION is a static member you can delete this line Bundle bundle=new Bundle(); bundle.putBoolean(AppConfig.IS_NOTIFICATION, true); notifyIntent.putExtras(bundle); – Munish Kapoor Jan 28 '12 at 07:43
  • Unfortunately, I still get no notification sent. I'm calling it from the onPostExecute() method of an ASyncTask class. I confirm that the ASyncTask is complete by a message in LogCat, but I get no notification sent to the notification bar. – Carnivoris Jan 28 '12 at 17:00
  • Hello, I have tried it with AsyncTask it's working you can download source from here: http://webheavens.com/AsyncTaskDemo.rar – Munish Kapoor Jan 29 '12 at 17:14
  • After getting sidetracked on other issues with the app, I finally got back around to trying this. It worked! Thank you! – Carnivoris Feb 08 '12 at 20:59
  • @MunishKapoor how to start my app while a user clicks on the notifiction? – Qadir Hussain Nov 25 '13 at 05:50
  • @MunishKapoor im getting this AppConfig cannot be resolved to a variable – Qadir Hussain Nov 25 '13 at 06:50
2

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.

SteveCav
  • 6,649
  • 1
  • 50
  • 52
2

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);
Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
Nitesh Khosla
  • 875
  • 8
  • 20
  • I'm running into similar issues with this as I have before. Intent notificationIntent = new Intent(this, CastrRecorder.class); That line gets marked by Eclipse and the only resolution for it is to remove the arguments. Also, this is being called within a class that extends ASyncTask and getActivity() doesn't work. – Carnivoris Jan 27 '12 at 15:15
0

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.

Emad
  • 1