It seems there was a misunderstanding or an issue with the previous implementation. You are correct that SOUND_SETTINGS opens general sound settings, not a sound picker, and RINGTONE_PICKER with TYPE: 2 should work for notification sounds.
If RINGTONE_PICKER gave you an error, the specific error message is crucial for debugging. However, let's provide a robust solution using expo-intent-launcher that correctly opens the notification sound picker and handles the result, as well as an explanation of how it relates to notification channels.
Key Libraries:
First, ensure you have expo-intent-launcher installed:
npx expo install expo-intent-launcher expo-application expo-notifications
expo-intent-launcher: To launch Android intents.
expo-application: To get the app's package name for channel settings (if needed).
expo-notifications: (Optional) If you plan to update a notification channel's sound programmatically.
There are two primary ways to approach "notification sounds for a specific channel" on Android, and it's important to understand the difference:
Scenario 1: Your app opens a system sound picker, the user chooses a sound, and your app programmatically sets it for a notification channel.
This is the most common use case if you want to provide an in-app button for users to select a custom sound. The user selects a sound, and your app receives its URI, which you then use with expo-notifications to update your channel.
Here are the Android intent settings options for this:
import * as IntentLauncher from 'expo-intent-launcher';
import { Platform, Alert } from 'react-native';
import * as Notifications from 'expo-notifications'; // For updating channel sound
// --- IMPORTANT: Replace with your actual channel ID ---
const YOUR_NOTIFICATION_CHANNEL_ID = 'my_important_notifications';
// Make sure this channel is created first, e.g.:
/*
async function createNotificationChannel() {
await Notifications.setNotificationChannelAsync(YOUR_NOTIFICATION_CHANNEL_ID, {
name: 'Important Alerts',
importance: Notifications.AndroidImportance.HIGH,
sound: 'default', // Initial sound (can be 'default' or a URI)
vibrationPattern: [0, 250, 250, 250],
// Add other channel properties as needed
});
}
// Call createNotificationChannel() on app start or when needed.
*/
/**
* Opens the Android system notification sound picker, allows the user to choose a sound,
* and returns its URI. It then attempts to update a specific notification channel's sound.
*
* @returns {Promise<string|null>} The URI of the selected sound, or null if cancelled/error.
*/
const selectNotificationSoundAndSetChannel = async () => {
if (Platform.OS !== 'android') {
Alert.alert('Not Available', 'This feature is only available on Android.');
return null;
}
try {
const result = await IntentLauncher.startActivityForResultAsync(
'android.intent.action.RINGTONE_PICKER', // The standard Android intent for picking sounds
{
extras: {
// KEY_RINGTONE_TYPE (int):
// 1 = RingtoneManager.TYPE_RINGTONE (for calls)
// 2 = RingtoneManager.TYPE_NOTIFICATION (This is what you want for notifications)
// 4 = RingtoneManager.TYPE_ALARM
'android.intent.extra.ringtone.TYPE': 2,
// KEY_RINGTONE_TITLE (String): Title for the picker dialog
'android.intent.extra.ringtone.TITLE': 'Select Your Notification Sound',
// KEY_RINGTONE_SHOW_DEFAULT (boolean): Show the "Default sound" option
'android.intent.extra.ringtone.SHOW_DEFAULT': true,
// KEY_RINGTONE_SHOW_SILENT (boolean): Show the "Silent" option
'android.intent.extra.ringtone.SHOW_SILENT': true,
// KEY_RINGTONE_EXISTING_URI (Uri): Optionally pre-select an existing sound's URI
// 'android.intent.extra.ringtone.EXISTING_URI': 'content://settings/system/notification_sound'
},
}
);
// result.resultCode: -1 (Activity.RESULT_OK), 0 (Activity.RESULT_CANCELED)
if (result.resultCode === -1) { // User selected a sound
const selectedUri = result.data?.data; // The chosen sound URI is in result.data.data
if (selectedUri) {
console.log('Selected Notification Sound URI:', selectedUri);
// --- Now, update your notification channel with the selected sound ---
try {
await Notifications.setNotificationChannelAsync(YOUR_NOTIFICATION_CHANNEL_ID, {
name: 'Important Alerts', // Must match the original channel name
importance: Notifications.AndroidImportance.HIGH, // Must match original importance
sound: selectedUri, // Set the sound using the chosen URI
vibrationPattern: [0, 250, 250, 250], // Retain other properties
// Add any other channel properties here that were previously set
});
Alert.alert(
'Sound Set!',
`Notification sound for channel "${YOUR_NOTIFICATION_CHANNEL_ID}" updated.`
);
return selectedUri;
} catch (channelError) {
console.error('Failed to update notification channel sound:', channelError);
Alert.alert(
'Error Setting Sound',
'Selected sound, but failed to update the notification channel. Please ensure the channel exists and permissions are correct.'
);
return null;
}
} else {
console.warn('Sound picker returned OK, but no URI was selected.');
Alert.alert('No Sound Selected', 'The picker was closed, but no specific sound was chosen.');
return null;
}
} else if (result.resultCode === 0) { // User cancelled the selection
console.log('Notification sound picker cancelled by user.');
Alert.alert('Selection Cancelled', 'You cancelled the sound selection.');
return null;
} else { // Unexpected result
console.log('Notification sound picker returned an unexpected result code:', result.resultCode);
Alert.alert('Error', 'An unexpected error occurred with the sound picker.');
return null;
}
} catch (error) {
console.error('Failed to open notification sound picker:', error);
if (error.code === 'ERR_ACTIVITY_NOT_FOUND') {
// This is the error you might have seen if the system couldn't find the picker.
// It's highly unlikely on a standard Android device, but possible on highly customized ROMs.
Alert.alert(
'Feature Not Available',
'Your device does not support opening a notification sound picker directly, or the system component is missing.'
);
} else {
Alert.alert(
'Error',
`An error occurred while trying to open the sound picker: ${error.message || 'Unknown error'}.`
);
}
return null;
}
};
// How you would use this with a button:
// <Button title="Choose Notification Sound" onPress={selectNotificationSoundAndSetChannel} />