0

I have used local notification in my app. User will get notification on daily basis.It is working fine in below 7 version but not in 8.0.

why Normal Notification Builder doesn't show notifications on Android O.

How could I show notification on Android 8 Oreo?

Is there any new piece of code to add for showing notification on Android O?

here is my Notification Service code this worked on android 7 but in android 8 is not worked

public class NotificationService extends Service {
    private static final String NOTIFICATION_DELETED_ACTION = "NOTIFICATION_DELETED";
    boolean canceledNotification = false;
    int imageId;
    int largeImageId;
    String lastCancelDate = "";
    Methods methods = new Methods();
    int notifyID = 1000;
    private final BroadcastReceiver receiver = new C03671();
    String title;
    CalendarTool tool = new CalendarTool();
    int updateMinutes = 0;



        public void onReceive(Context context, Intent intent) {
            Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
            editor.putBoolean("CANCELED_NOTIFICATION", true);
            editor.putString("CANCELED_DATE", NotificationService.this.methods.calendarToString(Calendar.getInstance(), Methods.DATEFORMAT));
            editor.apply();
            NotificationService.this.unregisterReceiver(this);
        }
    }

    public IBinder onBind(Intent intent) {
        return null;
    }

    @SuppressLint("WrongConstant")
    public int onStartCommand(Intent intent, int flags, int startId) {
        this.tool = new CalendarTool();
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
        boolean showNotification = sharedPref.getBoolean("SHOW_NOTIFICATION", true);
        this.canceledNotification = sharedPref.getBoolean("CANCELED_NOTIFICATION", false);
        this.lastCancelDate = sharedPref.getString("CANCELED_DATE", "");
        this.updateMinutes = sharedPref.getInt("UPDATE_NOTIFICATION_MIN", 0);
        if (showNotification) {
            Bitmap bitmap;
            this.title = this.methods.ReplaceNumber(this.tool.getJalaliStringLong());
            this.imageId = getResources().getIdentifier("d" + String.valueOf(this.tool.getIranianDay()), "drawable", getPackageName());
            this.largeImageId = getResources().getIdentifier("date" + String.valueOf(this.tool.getIranianDay()), "drawable", getPackageName());
            if (VERSION.SDK_INT < 24) {
                bitmap = this.methods.getFontBitmap(this, this.methods.ReplaceNumber(String.valueOf(this.tool.getIranianDay())), ContextCompat.getColor(this, R.color.colorPrimary), 50, "nazanin.ttf");
            } else {
                bitmap = BitmapFactory.decodeResource(getResources(), this.largeImageId);
            }
            int[] attrs = new int[]{16842904};
            TypedArray titleStyle = obtainStyledAttributes(R.style.NotificationTitle, attrs);
            TypedArray textStyle = obtainStyledAttributes(R.style.NotificationText, attrs);
            int titleColor = titleStyle.getColor(0, ViewCompat.MEASURED_STATE_MASK);
            int textColor = textStyle.getColor(0, -7829368);
            RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification_layout);
            contentView.setImageViewBitmap(R.id.notifImage, bitmap);
            contentView.setImageViewBitmap(R.id.notifTitle, this.methods.getFontBitmap(this, this.title, titleColor, 16, "iranyekanregular.ttf"));
            contentView.setImageViewBitmap(R.id.notifGregText, this.methods.getFontBitmap(this, this.methods.ReplaceNumber(this.tool.getGregorianStringFarsiShort()), textColor, 14, "iranyekanregular.ttf"));
            contentView.setImageViewBitmap(R.id.notifLunarText, this.methods.getFontBitmap(this, this.methods.ReplaceNumber(this.tool.getLunarStringShort()), textColor, 14, "iranyekanregular.ttf"));
            contentView.setImageViewBitmap(R.id.notifSpaceText, this.methods.getFontBitmap(this, " - ", textColor, 14, "iranyekanregular.ttf"));
            titleStyle.recycle();
            textStyle.recycle();
            PendingIntent deletePendIntent;
            NotificationManager mNotificationManager;
            Intent intent2;
            Builder mBuilder;
            if (this.canceledNotification) {
                Date lastDate = this.methods.StringDateToDate(this.lastCancelDate, Methods.DATEFORMAT);
                Calendar lastCalendar = Calendar.getInstance();
                lastCalendar.setTime(lastDate);
                Calendar nowCalendar = Calendar.getInstance();
                lastCalendar.add(12, this.updateMinutes);
                if (nowCalendar.getTime().after(lastCalendar.getTime())) {
                    deletePendIntent = PendingIntent.getBroadcast(this, 0, new Intent(NOTIFICATION_DELETED_ACTION), 0);
                    registerReceiver(this.receiver, new IntentFilter(NOTIFICATION_DELETED_ACTION));
                    mNotificationManager = (NotificationManager) getSystemService("notification");
                    intent2 = new Intent(this, MainActivity.class);
                    intent2.setFlags(603979776);
                    mBuilder = new Builder(this).setSmallIcon(this.imageId).setContent(contentView).setDeleteIntent(deletePendIntent).setContentIntent(PendingIntent.getActivity(this, 0, intent2, 0));
                    if (this.updateMinutes == 0) {
                        mBuilder.setAutoCancel(false).setOngoing(true);
                    } else {
                        mBuilder.setAutoCancel(true);
                    }
                    mNotificationManager.notify(this.notifyID, mBuilder.build());
                }
            } else {
                deletePendIntent = PendingIntent.getBroadcast(this, 0, new Intent(NOTIFICATION_DELETED_ACTION), 0);
                registerReceiver(this.receiver, new IntentFilter(NOTIFICATION_DELETED_ACTION));
                mNotificationManager = (NotificationManager) getSystemService("notification");
                intent2 = new Intent(this, MainActivity.class);
                intent2.setFlags(603979776);
                mBuilder = new Builder(this).setSmallIcon(this.imageId).setContent(contentView).setDeleteIntent(deletePendIntent).setContentIntent(PendingIntent.getActivity(this, 0, intent2, 0));
                if (this.updateMinutes == 0) {
                    mBuilder.setAutoCancel(false).setOngoing(true);
                } else {
                    mBuilder.setAutoCancel(true);
                }
                mNotificationManager.notify(this.notifyID, mBuilder.build());
            }
        }
        return 1;
    }
}
ebrahim
  • 87
  • 1
  • 10
  • 1
    You need to add [NotificationChannel](https://developer.android.com/reference/android/app/NotificationChannel) – Barns Jan 01 '19 at 17:40

1 Answers1

0

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

Navin
  • 285
  • 1
  • 8
  • 1
    Please refrain from answering duplicate questions. And do not just copy/paste someones answer [Notifications Oreo](https://stackoverflow.com/questions/43093260/notification-not-showing-in-oreo#43093261) – Barns Jan 01 '19 at 17:56