Home » Android » android – Notification channel not responding to user changes-Exceptionshub

android – Notification channel not responding to user changes-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

What I’ve done: I set notifications using AlarmManager. When I do this I pass a string of the notification type I would like to occur e.g. if I’d like a silent notification, I pass the string “Silent” into the method, “Beep” for a beep sound notification. Notification channels have been set up for all possible notification types (silent, vibrate, beep and audio).

The outcome: On first install, the user’s stored notification preference is called from Firebase and the notifcations come in perfectly for each of the notification types.

The problem: However, when the user changes their notification preference from within the app e.g. from “Beep” to “Vibrate”, the notifications still “Beep” until I reinstall the app. Then it starts to “Vibrate”.

I know that notification channels cannot be changed but that’s fine as I’ve created all the possible notification channels – so how come it still doesn’t work? Note that the new Notification Channel intent that Android have introduced for Android 8 is not an option because I need the user to select from my audio files, not theirs. Also, the UI looks terrible.

Here I set my notification using AlarmManager:

public void setAlarm(Integer activityHour, Integer activityMinute, Integer timingPreference, Integer requestCode, String activity, String notificationType) {
        Calendar currentTime = Calendar.getInstance();
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, activityHour);
        c.set(Calendar.MINUTE, activityMinute - timingPreference);
        c.set(Calendar.SECOND, 0);

        if (c.after(currentTime)) {
            AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
            intent.putExtra("Activity", activity);
            intent.putExtra("Notification Type", notificationType);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), requestCode, intent, 0);

            alarmManager.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
            // alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
        }
    }

Here is my BroadcastReceiver:

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String activity = intent.getExtras().getString("Activity");
        String notificationType = intent.getExtras().getString("Notification Type");

        NotificationHelper notificationHelper = new NotificationHelper(context, notificationType);
        NotificationCompat.Builder nb = notificationHelper.getChannelNotification(activity, notificationType);
        notificationHelper.getManager().notify(1, nb.build());
    }
}

Here is my NotificationHelper:

public class NotificationHelper extends ContextWrapper {

    private NotificationManager mManager;

    public NotificationHelper(Context base, String notificationTypes) {
        super(base);
        createChannels(notificationTypes);
    }

    private void createChannels(String type) {

        if (type.equals("Silent")) {
            NotificationChannel notificationChannelSilent = new NotificationChannel("Channel Silent", "Silent", NotificationManager.IMPORTANCE_HIGH);
            notificationChannelSilent.enableLights(true);
            notificationChannelSilent.setLightColor(R.color.royalBlue);
            notificationChannelSilent.setSound(null,null);
            notificationChannelSilent.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
            getManager().createNotificationChannel(notificationChannelSilent);
        }

        if (type.equals("Vibrate")) {
            NotificationChannel notificationChannelVibrate = new NotificationChannel("Channel Vibrate", "Vibrate", NotificationManager.IMPORTANCE_HIGH);
            notificationChannelVibrate.enableLights(true);
            notificationChannelVibrate.enableVibration(true);
            notificationChannelVibrate.setSound(null,null);
            notificationChannelVibrate.setLightColor(R.color.royalBlue);
            notificationChannelVibrate.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
            getManager().createNotificationChannel(notificationChannelVibrate);
        }

        if (type.equals("Beep")) {
            NotificationChannel notificationChannelBeep = new NotificationChannel("Channel Beep", "Beep", NotificationManager.IMPORTANCE_HIGH);
            Uri beepURI = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getApplicationContext().getPackageName() + "/" + R.raw.beep);
            notificationChannelBeep.enableLights(true);
            notificationChannelBeep.enableVibration(true);
            notificationChannelBeep.setLightColor(R.color.royalBlue);
            notificationChannelBeep.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
            AudioAttributes beepAttributes = new AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .build();
            notificationChannelBeep.setSound(beepURI, beepAttributes);
            getManager().createNotificationChannel(notificationChannelBeep);
        }

        if (type.equals("Audio")) {
            NotificationChannel notificationChannelAudio = new NotificationChannel("Channel Audio", "Audio", NotificationManager.IMPORTANCE_HIGH);
            Uri audioURI = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getApplicationContext().getPackageName() + "/" + R.raw.audio);
            notificationChannelAudio.enableLights(true);
            notificationChannelAudio.enableVibration(true);
            notificationChannelAudio.setLightColor(R.color.royalBlue);
            notificationChannelAudio.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
            AudioAttributes audioAttributes = new AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .build();
            notificationChannelAudio.setSound(audioURI, audioAttributes);
            getManager().createNotificationChannel(notificationChannelAudio);
        }
    }

    public NotificationManager getManager() {
        if (mManager == null) {
            mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        }
        return mManager;
    }

    public NotificationCompat.Builder getChannelNotification(String activity, String notificationType) {
        if (notificationType.equals("Silent")) {
            return new NotificationCompat.Builder(getApplicationContext(), "Channel Silent")
                    .setContentTitle(activity)
                    .setContentText("You can do it.")
                    .setSmallIcon(R.drawable.wali_logo);
        } else if (notificationType.equals("Vibrate")) {
            return new NotificationCompat.Builder(getApplicationContext(), "Channel Vibrate")
                    .setContentTitle(activity)
                    .setContentText("You can do it.")
                    .setSmallIcon(R.drawable.wali_logo)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setVibrate(new long[] { 1000, 1000});
        } else if (notificationType.equals("Beep")) {
            return new NotificationCompat.Builder(getApplicationContext(), "Channel Beep")
                    .setContentTitle(activity)
                    .setContentText("You can do it.")
                    .setSmallIcon(R.drawable.wali_logo)
                    .setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getApplicationContext().getPackageName() + "/" + R.raw.beep));
        } else if (notificationType.equals("Audio")) {
            return new NotificationCompat.Builder(getApplicationContext(), "Channel Audio")
                    .setContentTitle(activity)
                    .setContentText("You can do it.")
                    .setSmallIcon(R.drawable.wali_logo)
                    .setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getApplicationContext().getPackageName() + "/" + R.raw.audio));
        } else {
            return new NotificationCompat.Builder(getApplicationContext(), "Silent")
                    .setContentTitle(activity)
                    .setContentText("You can do it.")
                    .setSmallIcon(R.drawable.wali_logo);
        }
    }
}
How to&Answers: