Notificationcompat with API 26

NotificationCompat with API 26

Create the NotificationChannel only if API >= 26

public void initChannels(Context context) {
if (Build.VERSION.SDK_INT < 26) {
return;
}
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel("default",
"Channel name",
NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("Channel description");
notificationManager.createNotificationChannel(channel);
}

And then just use:

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, "default");

So your notifications are working with both API 26 (with channel) and below (without).

Does notification in Android SDK 26 and above supports SDK below 26?

Short Answer:

Yes it works but with minor changes.

Long Answer:

So I found the detailed way to test on devices with SDK below 26:

  1. You can configure API level in setting up an emulator as the following figure:

image

In the image above, I have selected Pixel emulator and can select the desired API level to be installed (My API level is 22)


  1. Then proceed to installing the API and configuring the emulator
  2. Once you have your emulator up & running, pay attention to the code below:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel Channel = new NotificationChannel(
"channel",
"myChannel",
NotificationManager.IMPORTANCE_HIGH
);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
Uri sound = Uri.parse(
ContentResolver.SCHEME_ANDROID_RESOURCE +
"://" +
getApplicationContext().getPackageName() +
"/" +
R.raw.void_event
);

Channel.setDescription("My Event Channel");
Channel.setSound(sound, audioAttributes)

NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(Channel);

}

The code above sets channel priority(NotificationManager.IMPORTANCE_HIGH) and sound (setSound(sound, audioAttributes)) working only if your API is above 26.


  1. What you can do is that you can also add these lines of code when notification is going to be triggered such as tapping a button or receiving push notification (where you are working with NotificationCompat.Builder):
// Define sound 
Uri sound = Uri.parse(
ContentResolver.SCHEME_ANDROID_RESOURCE +
"://" +
context.getPackageName() +
"/" +
R.raw.void_event
);

`enter code here`NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
Notification notification = new NotificationCompat.Builder(context,"channel")
// The next 2 lines of code is for adding sound & priority for API < 26
// For devices with API > 26, it just ignores it
.setSound(sound, AudioManager.STREAM_ALARM)
.setPriority(NotificationManager.IMPORTANCE_HIGH)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("Notification")
.setContentText("my notification")
.setCategory(NotificationCompat.CATEGORY_ALARM)
.build();

notificationManager.notify(0,notification);

How can I create a notification channel for Android API level 26 (before Android Oreo)?

There is no way to create notification channels for API level < 26, because notification channels were added in API 26.

Fortunately, that means that your code is already correct. You're already doing the right thing by only creating the channel on API level >= 26.

When you create the notification with NotificationCompat.Builder, you can simply call setChannelId with your string unconditionally, and it will ignore it for you on Android versions where channels are not supported ("No-op on versions prior to Build.VERSION_CODES.O").

You can then pass the returned notification to startForeground, as described in the guide you linked.

Android - Notification Channel API = 26 is not working properly

i found this in the documentation. May be it will help you :

On Android 8.0 (API level 26) and above, importance of a notification is determined by the importance of the channel the notification was posted to. Users can change the importance of a notification channel in the system settings (figure 12). On Android 7.1 (API level 25) and below, importance of each notification is determined by the notification's priority.

And also :

Android O introduces notification channels to provide a unified system to help users manage notifications. When you target Android O, you must implement one or more notification channels to display notifications to your users. If you don't target Android O, your apps behave the same as they do on Android 7.0 when running on Android O devices.

And finally :

  • Individual notifications must now be put in a specific channel.
  • Users can now turn off notifications per channel, instead of turning off all notifications from an app.
  • Apps with active notifications display a notification "badge" on top of their app icon on the home/launcher screen.
  • Users can now snooze a notification from the drawer. You can set an automatic timeout for a notification.
  • Some APIs regarding notification behaviors were moved from Notification to NotificationChannel. For example, use NotificationChannel.setImportance() instead of NotificationCompat.Builder.setPriority() for Android 8.0 and higher.

Notifications fail to display in Android Oreo (API 26)

Starting with Android O, you are required to configure a NotificationChannel, and reference that channel when you attempt to display a notification.

private static final int NOTIFICATION_ID = 1;
private static final String NOTIFICATION_CHANNEL_ID = "my_notification_channel";

...

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_DEFAULT);

// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setVibrate(new long[]{0, 100, 100, 100, 100, 100})
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Content Title")
.setContentText("Content Text");

notificationManager.notify(NOTIFICATION_ID, builder.build());

A couple of important notes:

  1. Settings such as vibration pattern specified in the NotificationChannel override those specified in the actual Notification. I know, its counter-intuitive. You should either move settings that will change into the Notification, or use a different NotificationChannel for each configuration.
  2. You cannot modify most of the NotificationChannel settings after you've passed it to createNotificationChannel(). You can't even call deleteNotificationChannel() and then try to re-add it. Using the ID of a deleted NotificationChannel will resurrect it, and it will be just as immutable as when it was first created. It will continue to use the old settings until the app is uninstalled. So you had better be sure about your channel settings, and reinstall the app if you are playing around with those settings in order for them to take effect.

How to create NotificationChannel for lower API's ( 26 ) ?

Not possible. The Android OS does not support it. You can add the channelId to the notification with NotificationCompat.Builder(Context context, String channelId), but the OS will ignore it pre-Oreo.

My collegue tested it on API 15, 22, 23, and 26: https://stackoverflow.com/a/45979726/1310343

Android Notification Not Showing On API 26

From the documentation:

Android O introduces notification channels to provide a unified system to help users manage notifications. When you target Android O, you must implement one or more notification channels to display notifications to your users. If you don't target Android O, your apps behave the same as they do on Android 7.0 when running on Android O devices.

(emphasis added)

You do not seem to be associating this Notification with a channel.



Related Topics



Leave a reply



Submit