android 意图从我的应用打开通知通道设置

cgfeq70w  于 2023-04-04  发布在  Android
关注(0)|答案(3)|浏览(122)

为了打开我之前在应用中创建的通知通道的设置,我需要发送什么Intent?我需要从应用链接到它。

fdbelqdn

fdbelqdn1#

要打开单个通道的通知设置,请使用ACTION_CHANNEL_NOTIFICATION_SETTINGS

Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS)
    .putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName())
    .putExtra(Settings.EXTRA_CHANNEL_ID, yourChannelId);
startActivity(intent);

要打开应用程序的通知设置(即查看所有频道),请使用ACTION_APP_NOTIFICATION_SETTINGS

Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS)
    .putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
startActivity(intent);
lx0bsm1f

lx0bsm1f2#

下面是通知设置和细粒度通道设置的代码段:

private void openNotificationSettings() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
        intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
        startActivity(intent);
    } else {
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.parse("package:" + getPackageName()));
        startActivity(intent);
    }
}

@RequiresApi(26)
private void openChannelSettings(String channelId) {
    Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
    intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
    intent.putExtra(Settings.EXTRA_CHANNEL_ID, channelId);
    startActivity(intent);
}
yhxst69z

yhxst69z3#

Kotlin代码,支持比AndroidO更老的版本,支持Lollipop的edge case:

fun openAppNotificationSettings(context: Context) {
    val intent = Intent().apply {
        when {
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> {
                action = Settings.ACTION_APP_NOTIFICATION_SETTINGS
                putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName)
            }
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP -> {
                action = "android.settings.APP_NOTIFICATION_SETTINGS"
                putExtra("app_package", context.packageName)
                putExtra("app_uid", context.applicationInfo.uid)
            }
            else -> {
                action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
                addCategory(Intent.CATEGORY_DEFAULT)
                data = Uri.parse("package:" + context.packageName)
            }
        }
    }

    context.startActivity(intent)
}

相关问题