React Native Ringer Mode - Get and set ringer mode on Android devices.
⚠️ This package only supports Android. All functions will returnundefinedon non-Android devices. See #3.
npm install react-native-ringer-modeimportReactfrom'react';import{View,Text,Button}from'react-native';import{useRingerMode,RINGER_MODE}from'react-native-ringer-mode';constmodeText={[RINGER_MODE.silent]: 'Silent',[RINGER_MODE.normal]: 'Normal',[RINGER_MODE.vibrate]: 'Vibrate',};exportdefaultfunctionApp(){const{ mode, error, setMode }=useRingerMode();return(<View><Text>Ringer Mode: {mode!==undefined ? modeText[mode] : null}</Text><View><Buttontitle="Silent"onPress={()=>setMode(RINGER_MODE.silent)}/><Buttontitle="Normal"onPress={()=>setMode(RINGER_MODE.normal)}/><Buttontitle="Vibrate"onPress={()=>setMode(RINGER_MODE.vibrate)}/></View><View><Text>{error?.message}</Text></View></View>);}getRingerMode is an async function and resolves the current ringer mode of the device.
(Resolves undefined on non-Android devices.)
importReact,{useEffect,useState}from'react';import{View,Text}from'react-native';import{RINGER_MODE,getRingerMode,RingerModeType,}from'react-native-ringer-mode';constmodeText={[RINGER_MODE.silent]: 'Silent',[RINGER_MODE.normal]: 'Normal',[RINGER_MODE.vibrate]: 'Vibrate',};exportdefaultfunctionApp(){const[mode,setMode]=useState<RingerModeType|undefined>();useEffect(()=>{(async()=>{try{constcurrentMode=awaitgetRingerMode();setMode(currentMode);}catch(error){console.error(error);}})();},[]);return(<View><Text>Ringer Mode: {mode!==undefined ? modeText[mode] : null}</Text></View>);}setRingerMode is an async function that sets the given ringer mode to the device and resolves the mode if it is set.
(Resolves undefined on non-Android devices.)
importReactfrom'react';import{View,Button}from'react-native';import{setRingerMode,RINGER_MODE,RingerModeType,}from'react-native-ringer-mode';exportdefaultfunctionApp(){constsetMode=(mode: RingerModeType)=>{try{setRingerMode(mode);}catch(error){console.error(error);}};return(<View><Buttontitle="Silent"onPress={()=>setMode(RINGER_MODE.silent)}/><Buttontitle="Normal"onPress={()=>setMode(RINGER_MODE.normal)}/><Buttontitle="Vibrate"onPress={()=>setMode(RINGER_MODE.vibrate)}/></View>);}From N onward, ringer mode adjustments that would toggle Do Not Disturb are not allowed unless the app has been granted Do Not Disturb Access. See AudioManager#setRingerMode.
If you want to change the ringer mode from Silent mode or to Silent mode, you may run into the Not allowed to change Do Not Disturb state error. The example below checks the DND access and if user hasn't given the access opens the settings for it.
First you need to add the line below to your AndroidManifest.xml to be able to see your app in the settings.
<uses-permissionandroid:name="android.permission.ACCESS_NOTIFICATION_POLICY" />And you can check and request permission before setting the ringer mode. Example code below:
importReactfrom'react';import{View,Button}from'react-native';import{useRingerMode,RINGER_MODE,checkDndAccess,requestDndAccess,RingerModeType,}from'react-native-ringer-mode';exportdefaultfunctionApp(){const{ mode, setMode }=useRingerMode();constchangeMode=async(newMode: RingerModeType)=>{// From N onward, ringer mode adjustments that would toggle Do Not Disturb// are not allowed unless the app has been granted Do Not Disturb Access.// @see https://developer.android.com/reference/android/media/AudioManager#setRingerMode(int)if(newMode===RINGER_MODE.silent||mode===RINGER_MODE.silent){consthasDndAccess=awaitcheckDndAccess();if(hasDndAccess===false){// This function opens the DND settings.// You can ask user to give the permission with a modal before calling this function.requestDndAccess();return;}}setMode(newMode);};return(<View><Buttontitle="Silent"onPress={()=>changeMode(RINGER_MODE.silent)}/><Buttontitle="Normal"onPress={()=>changeMode(RINGER_MODE.normal)}/><Buttontitle="Vibrate"onPress={()=>changeMode(RINGER_MODE.vibrate)}/></View>);}See the contributing guide to learn how to contribute to the repository and the development workflow.
MIT