Notification Not Showing in Oreo

Notification not showing in Oreo

In Android O it's a must to use a channel with your Notification Builder

below is a sample code :

// Sets an ID for the notification, so it can be updated.
int notifyID = 1;
String CHANNEL_ID = "my_channel_01";// The id of the channel.
CharSequence name = getString(R.string.channel_name);// The user-visible name of the channel.
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
// Create a notification and set the notification channel.
Notification notification = new Notification.Builder(MainActivity.this)
.setContentTitle("New Message")
.setContentText("You've received new messages.")
.setSmallIcon(R.drawable.ic_notify_status)
.setChannelId(CHANNEL_ID)
.build();

Or with Handling compatibility by:

NotificationCompat notification =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setChannelId(CHANNEL_ID).build();

Now make it notify

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

// Issue the notification.
mNotificationManager.notify(notifyID , notification);

or if you want a simple fix then use the following code:

NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mNotificationManager.createNotificationChannel(mChannel);
}

Updates:
NotificationCompat.Builder reference

NotificationCompat.Builder(Context context)

This constructor was deprecated in API level 26.0.0
so you should use

Builder(Context context, String channelId)

so no need to setChannelId with the new constructor.

And you should use the latest of AppCompat library currently 26.0.2

compile "com.android.support:appcompat-v7:26.0.+"

Source from Android Developers Channel on Youtube

Also, you could check official Android Docs

Notification not showing below Android Oreo version

After some playing around I got it to work, building on the linked answer from notTdar

public class LocationService extends Service {

//notifications
public static PendingIntent pendingIntent;
public static PendingIntent pendingCloseIntent;

private static final int NOTIFICATION_ID = 100;

Notification notification;
NotificationManager notificationManager;

private static final String CHANNEL_ID = "location_notification_channel_id";
private static final String CHANNEL_NAME = "Location Notification Service";

Context context;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

context = getApplicationContext();
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

//open main activity when clicked
pendingIntent = PendingIntent.getActivity(context, 0,
new Intent(context, MainActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP),
0);

//action when notification button clicked
Intent intentAction = new Intent(context, ActionReceiver.class);
intentAction.putExtra("location_service","service_notification");
pendingCloseIntent = PendingIntent.getBroadcast(context,0, intentAction, PendingIntent.FLAG_UPDATE_CURRENT);

setNotification();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//create foreground service
startForeground(NOTIFICATION_ID, notification);
pushLocation(intent);
} else {
notificationManager.notify(NOTIFICATION_ID, notification);
pushLocation(intent);
}

return LocationService.START_STICKY;
}

private void setNotification() {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notification = notificationBuilder
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("Running")
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.setContentIntent(pendingIntent)
.setWhen(System.currentTimeMillis())
.setTicker("Running")
.addAction(android.R.drawable.ic_menu_close_clear_cancel, "STOP", pendingCloseIntent)
.setOngoing(true)
.build();

} else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N | Build.VERSION.SDK_INT == Build.VERSION_CODES.N_MR1) {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);
notification = notificationBuilder
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("Running")
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.setContentIntent(pendingIntent)
.setWhen(System.currentTimeMillis())
.setTicker("Running")
.addAction(android.R.drawable.ic_menu_close_clear_cancel, "STOP", pendingCloseIntent)
.setOngoing(true)
.build();
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
channel.setImportance(NotificationManager.IMPORTANCE_NONE);
channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
notificationManager.createNotificationChannel(channel);

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);
notification = notificationBuilder
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("Running")
.setPriority(PRIORITY_MIN)
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.setContentIntent(pendingIntent)
.setWhen(System.currentTimeMillis())
.setTicker("Running")
.addAction(android.R.drawable.ic_menu_close_clear_cancel, "STOP", pendingCloseIntent)
.setOngoing(true)
.build();
}
}
}

It looks a bit messy with the repeat code, but it works.

If anyone has any tips to condense it, it would be appreciated!

Notification not working in android version Oreo

Add channel id to builder:

NotificationCompat.Builder mbuilder = (NotificationCompat.Builder)
new NotificationCompat.Builder(getApplicationContext(),
CHANNEL_ID)// add channel id

Android notification not showing on Oreo

I think you are missing mManager.createNotificationChannel(mChannel);

Oreo Notification Icon not displaying

Here i use the size of icon

mipmap-mdpi : 24*24
mipmap-hdpi : 36*36
mipmap-xhdpi : 48*48
mipmap-xxhdpi : 72*72

and I have store it into mipmap named as logo_white.png so while building a notification you can use this like

 notificationBuilder.setSmallIcon(getNotificationIcon())...


notificationBuilder = new NotificationCompat.Builder(this, "default")
.setSmallIcon(icon)
.setContentTitle(getResources().getString(R.string.app_name))
.setStyle(new NotificationCompat.BigTextStyle().bigText(data.get("message")))
.setContentText(data.get("message"))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
.setContentIntent(pendingIntent)
.setPriority(Notification.PRIORITY_MAX);

Here is the method

private int getNotificationIcon() {
boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
return useWhiteIcon ? R.mipmap.logo_white : R.mipmap.app_icon;
}

Here app_icon is normal app icon and logo_white is white transparent app icon

Android Why Notification is not working on below Oreo version?

place the small notif icon into drawable folders.

To provide support for all Android versions, developers should:
Place status bar icons for Android 3.0 and later in the drawable...

check documentation

Notifications do not work on Android Oreo

As in version >= 8.0 there is update for show notification that is you have to define channel as below

String CHANNEL_ID = "my_channel_01";            // The id of the channel.

then used channel id to your notification builder

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "01")
.setContentTitle(title)
.setSmallIcon(R.mipmap.ic_notification)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(message))
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setChannelId(CHANNEL_ID)
.setContentIntent(pendingIntent);

Then add the code below for version >=8.0

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {       // For Oreo and greater than it, we required Notification Channel.
CharSequence name = "My New Channel"; // The user-visible name of the channel.
int importance = NotificationManager.IMPORTANCE_HIGH;

NotificationChannel channel = new NotificationChannel(CHANNEL_ID,name, importance); //Create Notification Channel
notificationManager.createNotificationChannel(channel);
}

notificationManager.notify(notifyID /* ID of notification */, notificationBuilder.build());

I hope, This will help you to show notification in status bar in version >= 8.0



Related Topics



Leave a reply



Submit