Java – Android notifications from BroadcastReceiver are not displayed

Android notifications from BroadcastReceiver are not displayed… here is a solution to the problem.

Android notifications from BroadcastReceiver are not displayed

I want to create a notification (from BroadcastReceiver – if there is a difference).

I don’t know why, it just didn’t show 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 triedthis (and this)

Solution

Create notifications from BroadcastReceiver – this is important!
You should override this string in your code:

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

Related Problems and Solutions