Прежде чем вы сможете доставить уведомление на Android 8.0 и выше, вы должны зарегистрировать канал уведомлений вашего приложения в системе, передав экземпляр NotificationChannel
Если все, что вам нужно, это сделать уведомление из вашего приложения, то вы можете использовать этот код.
public class makeNotification {
private static final String ChannelId = "ChannelId";
private static final String CHANNEL_ID = "cahhnel";
private static final int NOTIFICATION_ID = 99;
public static void makenotification(Context context) {
Intent intent = new Intent(context,DemoVolleyActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,0);
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(
CHANNEL_ID,
"Channel Name",
NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context,ChannelId)
.setColor(ContextCompat.getColor(context, R.color.colorPrimary))
.setSmallIcon(R.drawable.ic_lock_black_24dp)
.setContentTitle("My Notification")
.setContentText("notification_body")
.setDefaults(Notification.DEFAULT_VIBRATE)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN
&& Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
notificationBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
}
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
}
}