Notification doesn't shown on above V26 API.Notification cshown on v25 but when i checked it on v27 than the notification doesn't appear. My code to StartService and create 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 method shownotification which will invoke the broadcastreceiver.
@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);
}
}
NotificationReceiver class
At this class, i build the notification and notify the notification to show.
@Override
public void onReceive(Context context, Intent intent) {
buildNotification(context);
}
Inside 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";// The id of the channel.
CharSequence name = context.getString(R.string.app_name);// The user-visible name of the channel.
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 notification doesn't appear. Please help me.