Home>
I want to achieve

NotificationChannelI want to reset.

What we know now

The following program has been completed as a notification when the API is 26 or more.

if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.O)
{
    // Definition of notification channel
    NotificationChannel notificationChannel = new NotificationChannel (channelID,
                                                                      channelID,
                                                                      NotificationManager.IMPORTANCE_DEFAULT);
    // --------------------
    // Addendum area
    // --------------------
    // Create a notification channel
    notificationManager.createNotificationChannel (notificationChannel);
    // Notification generation and configuration
    Notification.Builder builder = new Notification.Builder (this, channelID)
            .setContentTitle ("MyTitle")
            .setContentText ("MyMessage")
            .setSmallIcon (R.drawable.MyIcon);
    // Issuing notifications
    notificationManager.notify (1, builder.build ());
}
else else
{
    // Normal notification settings
    ...
}

The above program works fine, but this time in the programPostscript areaI would like to add the following code to.

// The initial values ​​for isSound and isVibrate are false.
// Also, this initial value is managed by another activity, and it is switched by Switch.
// Disable notification sound
if (isSound == false){
    notificationChannel.setSound (null, null);
}
// Disable vibration
if (isVibrate == false)
{
    notificationChannel.enableVibration (false);
}


As a result of adding this program, the notification sound and vibration were not disabled "at first".

While conducting various investigations there,Uninstall the application and then install it againIt was invalidated by that.
* The operation here is again on Android Studio.RunUnlike running, actually uninstall the application manually and thenRunIs to execute and install.

And after uninstalling the app, as an experimentisSoundWhenisVibrateInitial value oftrueWhen I installed the app, of course, the notification sound and vibration were not disabled.

Expected

Probably, the notification setting of the application is defined on the OS side when the application is installed, and it is changed later (RunReinstallation by method or methodNotificationChannelI think that it is not toothy by changing the setting of.

I want to achieve (details)

This time, the notification sound and vibrationSwitchI would like to determine the presence or absence by switching with.
In other words, you need to change the notification settings within the app.

I would like to add the following method to realize the notification setting.

// Notification settings (planned to call multiple)
private void setNotification ()
{
    if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.O)
    {
        ...
    }
    else else
    {
        ...
    }
}


However, as you can see from the previous results,NotificationChannelSettings for the appRunNot only the reinstallation by, but also the setting is not changed unless it is manually uninstalled.

of course,isSoundWhenisVibrateAfter changing the abovesetNotificationCalling the method did not work as expected.
Again,NotificationChannelThe setting ofInitial value at the time of installationWas decided byisSoundWhenisVibrateIs alive all the time.

How to doNotificationChannelWill it be possible to reset?
Thank you.

Supplement

The following processing is described in the processing with API less than 26, hereWorks properlydoing.

if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.O)
{
    ...
}
else else
{
    Notification.Builder builder = new Notification.Builder (this);
    builder.setContentTitle ("MyTitle");
    builder.setContentText ("MyMessage");
    builder.setSmallIcon (R.drawable.MyIcon);
    // Enable notification sound
    if (isSound == true)
    {
        builder.setSound (alarmSound);
    }
    // Enable vibration
    if (isVibrate == true)
    {
        builder.setVibrate (new long [] {wait, vibrate, sleep, vibrate, sleep});
    }
}


setSoundOrseteVibrateIf i did not set, the notification sound and vibration would not be enabled, so this is not disabled.activationWe are doing.

As you can see, it is doubtful that the old API can be set, but the new API cannot.
However, looking at the current situation, I think I'm probably trying to do something I can't do.
If that is not possible for some reason, we can take the time to pursue this issue.

  • Answer # 1

    Ref: https://developer.android.com/training/notify-user/channels?hl=ja#UpdateChannel

    Once you have created a notification channel, you cannot programmatically change the display or ringing behavior of the notification channel.

    I don't know when this code is called, but after the first callNotificationChannelI don't think it is possible to update the setting value of.

    Only the user can change the channel behavior from the system settings. Add an item that opens this system setting to your app's settings UI so that users can easily access the notification settings.

    I wondered if it was recommended to put a link button from the app to the settings.

    Thinking about what I would probably want to do, I suddenly came up with ↓

    Separate NotificationChannel

    Create separate notification channels for each vibration and sound setting, and then connect them to the appropriate channel when notifying.
    (Still, if it is changed in the user settings, it cannot be overwritten.

    Delete and recreate NotificationChannel every time

    Ref: https://developer.android.com/training/notify-user/channels?hl=ja#DeleteChannel

    Looking at the reference

    Note: The notification settings screen shows the number of deleted channels as an anti-spam mechanism. To remove the test channel on your development device, reinstall the app or erase the data associated with your copy of the app.

    It seems that it is not a very good method