Introduction :
If you want to add vibration, react-native provides one API for that. You can create vibrations in single occurrence or pattern. Starting and canceling a vibration is easy. Just call one method and it will do that. Let me show you how.
Permission in Android :
You need to add one permission in Android for vibration. Open the AndroidManifest.xml file inside the android folder and add the below line :
Add this as a direct child.
<uses-permission android:name="android.permission.VIBRATE"/>
You don’t need anything on iOS.
vibrate() method :
vibrate() method is used to produce vibration. Import Vibration from react-native and call this method Vibration.vibrate(). It also take two optional parameters :
vibrate(?pattern: number | Array, ?repeat: boolean)
The vibration duration is 400 ms by default on Android and iOS. You can change it on Android. Pass a number as the first argument for that. This number will represent the new duration in ms.
You can also pass one array of numbers as the first argument representing a vibration pattern (vibration pattern in milliseconds). This works for both Android and iOS.
The second parameter repeat is used to mark it as a repeat pattern. If you set it as true, it will repeat the vibration until you cancel it. It is false by default.
Canceling a vibration :
cancel() method is used to cancel vibration. You don’t have to cancel it if repeat is false.