简体   繁体   中英

Android notifications from a BroadcastReceiver not showing

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 )

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

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.

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

Go to -> File -> Invalid caches -> Invalidate and restart

The action done the job

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM