1

I am working with AlarmManager, and when the alarm starts it also shows notifications. I have created notifications for Oreo and for before Oreo. Notifications before Oreo work properly - I can disable sounds and set lights, but I cannot make this work in Oreo. I had the similar issue with vibrations, but was able to find a working solution. I have tried A LOT of things (1, 2, 3, 4, 5, 6...), from NotificationCompat, to changing importance, but was unable to make it work.

My issue is only with Oreo notifications, I cannot disable sound (it goes of every time), and I cannot make light blink. I have went through a bunch of SO questions, and official documentation. Some solutions are obsolete, deprecated (NotificationCompat.Builder), others do not work at all (including some examples from the official documentation).

Here is my code for both Oreo (not working) and for older (working):

//region Oreo notifications
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

    CharSequence name = "AlarmNotification";
    String description = "Alarm notification";
    int importance = NotificationManager.IMPORTANCE_DEFAULT;

    NotificationChannel mChannel = new NotificationChannel(channelIdOreo, name, importance);
    mChannel.setDescription(description);
    mChannel.setShowBadge(true);
    mChannel.enableLights(true);
    mChannel.setLightColor(Color.RED);

    if (notificationManager != null) {
        notificationManager.createNotificationChannel(mChannel);
    }

    Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);

    if (sNotifications.equals("false")) {
        //NOT WORKING
        mChannel.setSound(null, null);
    }
    //VIBRATION WORKING
    if (sVibration.equals("true")) {
        if (vibrator != null && vibrator.hasVibrator()) {
            VibrationEffect effect = VibrationEffect.createOneShot(1000, VibrationEffect.DEFAULT_AMPLITUDE);
            vibrator.vibrate(effect);
        }
    }

    Notification notification = new Notification.Builder(context, channelIdOreo)
            .setContentTitle(contentTitleText)
            .setContentText(contentContentText)
            .setNumber(1)
            .setSmallIcon(whiteLogo)
            .setBadgeIconType(whiteLogo)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .build();

    if (notificationManager != null) {
        notificationManager.notify(notificationCode, notification);
    }
}
//endregion

//region Pre-Oreo notifications
else {
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, channelId)
            .setSmallIcon(whiteLogo)
            .setLargeIcon(largeIcon)
            .setContentTitle(contentTitleText)
            .setContentText(contentContentText)
            .setOngoing(false)
            .setNumber(1)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setAutoCancel(true);

    mBuilder.setLights(colorPPDOrange, 1000, 2000);

    if (sNotifications.equals("true")) {
        mBuilder.setSound(uri);
    }
    if (sVibration.equals("true")) {
        mBuilder.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
    }
    mBuilder.setContentIntent(pendingIntent);
    if (notificationManager != null) {
        notificationManager.notify(notificationCode, mBuilder.build());
    }
}
//endregion
Banana
  • 2,435
  • 7
  • 34
  • 60

1 Answers1

0

Finally found an answer. Every time I change anything related to my channel I had to have my channel id changed to a completely new id, that was NEVER used before. It cannot just be different from the current id. Other than this I replaced my string resource with actual string in code. Also I had to move a few lines of code as shown below.

This is how my code looks like now:

String channelIdOreo = "FfffffF";

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

    //region Oreo otification
    Notification notification = new Notification.Builder(context, channelIdOreo)
            .setContentTitle(contentTitleText)
            .setContentText(contentContentText)
            .setNumber(1)
            .setSmallIcon(whiteLogo)
            .setBadgeIconType(whiteLogo)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .build();
    //endregion

    NotificationChannel mChannel = new NotificationChannel(channelIdOreo, "Channel human readable title and stuff", NotificationManager.IMPORTANCE_DEFAULT);

    mChannel.enableLights(true);
    mChannel.setLightColor(Color.YELLOW);

    //region Conditions from settings
    if (sNotifications.equals("true")) {
        mChannel.setSound(uri, null);
    } else {
        mChannel.setSound(null, null);
    }

    if (sVibration.equals("true")) {
        mChannel.setVibrationPattern(new long[]{1000, 1000, 1000, 1000, 1000});
    } else {
        mChannel.enableVibration(false);
    }
    //endregion

    if (notificationManager != null) {
        notificationManager.createNotificationChannel(mChannel);
    }
    if (notificationManager != null) {
        notificationManager.notify(notificationCode, notification);
    }
}

Hope this will save some time to someone.

Banana
  • 2,435
  • 7
  • 34
  • 60
  • 1
    maybe consider putting those "regions" into their own methods. this saves on inline comments and makes it easier to read, while the names of the methods are telling what they actually do. – Martin Zeitler Aug 07 '18 at 17:39
  • 1
    @MartinZeitler I am currently refactoring all my code, and doing just that :) – Banana Aug 07 '18 at 18:01