In this part of the Mendix Native Series, you’ll learn how to use JavaScript to trigger device vibration in a Mendix Native app. This article explains how vibration works in native mobile apps, platform support for Android and iOS, and how to implement vibration for better user feedback, alerts, and interactions using a custom JavaScript action. Perfect for enhancing UX in Mendix Native applications.
Go to MX Studio Pro Create a new module JavaScript.
Right Click on JavaScript module -> Add Other -> JavaScript action -> give the name JS_VibrateDevice.

Open JS_VibrateDevice JavaScript action

Go to Settings -> Under the General tab add a string parameter(duration).

Go to the code section and add the below code to import the vibration.
import { Vibration } from "react-native";

Now remove this code code
throw new Error("JavaScript action was not implemented");
And add the below code between Begin & End user code
let vibrationValue = 500; // default vibration duration
if (duration !== undefined && duration !== null && duration !== "") {
const parsedDuration = Number(duration);
if (!isNaN(parsedDuration)) {
vibrationValue = parsedDuration;
}
}
Vibration.vibrate(vibrationValue);

now right Click on JavaScript module -> Add page -> Native mobile & give the name JavaScript_Native -> Inside navigation layout select NativePhone_Default(Atlas_Core).

Go to JavaScript_Native page -> take a button & give the name Vibrate.

Double click on Vibrate button -> In onClick events select call a nanoflow & create a new nanoflow(ACT_VibrateDevice).

Open ACT_VibrateDevice nanoflow -> drag & drop the JS_VibrateDevice JavaScript action inside this nanoflow.

Double click on JS_VibrateDevice and pass the duration value in miliseconds.

Go to Navigation -> Native mobile & add a new item(JavaScript) and select JavaScript_Native page.


Now run the application & test it.

When you tap the Vibrate button, the device will vibrate instantly.
Conclusion:
I hope this article has helped you understand How to Vibrate a Device Using JavaScript In Mendix Native App.
Thanks for reading this! See you in the next blog post.
