Notification is not shown on the above V26 API. Notification cshown on V25, but when I checked it on V27, than the notification does not appear. My code is for StartService and create a notification.
Inside mainctivity onCreate ()
Intent intent1 = new Intent(this, NotificationService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
ContextCompat.startForegroundService(this, intent1);
} else {
startService(intent1);
}
NotificationService extends service
onCreateMethod I call the showotification method, which will trigger the translation.
@Override
public void onCreate() {
super.onCreate();
session = SessionManager.getInstance(getApplicationContext());
showNotification();
}
private void showNotification() {
if (session.isNotificationOn() && session.isLogged()) {
Intent notificationIntent = new Intent(this, NotificationReceiver.class);
notificationIntent.putExtra(NotificationReceiver.CODE, NotificationReceiver.TYPE_START_CODE);
sendBroadcast(notificationIntent);
}
}
Class NotificationReceiver
In this class, I create a notification and notify about this notification.
@Override
public void onReceive(Context context, Intent intent) {
buildNotification(context);
}
Inside the buildNotification () method
private void buildNotification(Context context) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context,NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.notification)
.setAutoCancel(false)
.setOngoing(true)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentTitle(context.getString(R.string.app_name)) .setContentText(context.getString(R.string.notification_pull_down_information))
.setShowWhen(false)
.setColor(Color.BLACK);
String CHANNEL_ID = "my_channel_01";
CharSequence name = context.getString(R.string.app_name);
int importance = NotificationManager.IMPORTANCE_HIGH;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
}
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
manager.createNotificationChannel(mChannel);
}
manager.notify(notificatioCode, builder.build());
}
But on v26 above the notification does not appear. Please help me.