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.

  • Debugging native code in Android on the *device* (not emulator)
  • What does the ic_launcher-web.png in my project root do?
  • How to Unit test Retrofit api calls
  • How to set scrollbar thumb programmatically in Android?
  • View contents of database file in Android Studio
  • Android get accelerometers on earth coordinate system
  • Related posts:

    25.1.0 Android support lib is breaking fab behavior
    How to add application icon in notification tray?
    How can I create Android Application plugins/extensions that can be installed as an APK or from mark...
    Concurrent Modification Exception : adding to an ArrayList
    using jsoup with proguard closing force close
    DrawerLayout modify sensitivity?
  • Android: making a fullscreen application
  • Change DatePickerDialog's button texts
  • Having application running above other app
  • Android Column GridView, variable width like GooglePlus App
  • Infinite Scrolling Image ViewPager
  • Rectangle shape drawable, specify top and bottom stroke colors?
  • 5 Solutions collect form web for “Android notification not working”

    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() {
        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);
            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

    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.

    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.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.