5

I want to create a notification (from a BroadcastReceiver - if that makes a difference)

I don't know why, but It's just not showing up!

private void showNotification(Context context, String text) {

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.notification_icon)
                        .setContentTitle(getString(R.string.app_name))
                        .setContentText(text)
                        .setAutoCancel(true);

        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        mNotificationManager.notify(0, mBuilder.build());

    }

I have tried this (and this)

Community
  • 1
  • 1
Saman Miran
  • 424
  • 1
  • 6
  • 14

4 Answers4

2

Create a notification from a BroadcastReceiver - that makes the difference! You should rewrite this string in your code:

NotificationManager mNotificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Plo_Koon
  • 2,953
  • 3
  • 34
  • 41
  • 1
    I should have said this earlier: The BroadcastReceiver is in a Service Class, so that doesn't make a difference – Saman Miran Nov 23 '13 at 10:34
2

Thanks to all answers, but I got it solved unexpectedly. I don't know which point really solved it, but I tried this: 1. Clean 2. Rebuild 3. Close Android studio 4. Reboot 5. Restart android studio

Saman Miran
  • 424
  • 1
  • 6
  • 14
1

The issues can be the following:

-1: you add the method showNotification, in a position where it's not called by the broadcast receiver.

-2: you are not registering the broadcast receiver

-3: replace this

mNotificationManager.notify(0, mBuilder.build());

with

mNotificationManager.notify(1, mBuilder.build());

If the point 3 is not working, please, attach your whole broadcast receiver code, and where you are calling it.

iGio90
  • 3,251
  • 7
  • 30
  • 43
0
Go to -> File -> Invalid caches -> Invalidate and restart

The action done the job

Anand
  • 4,355
  • 2
  • 35
  • 45