Search code examples
ionic-frameworkcapacitorandroid-vibration

Vibration not working in background mode (Ionic/capacitor))


I am working on a project in which the client needs vibration in background-mode. The method

vibrator.vibrate()

is working when the screen is on and app is open. But when the screen is off or app is minimized (not killed), the vibration function not working. Anybody know the solution?


Solution

  • Ok I got how to get the background mode vibration in ionic app. Its possible using capacitor haptics vibration and foreground service plugins.

    import {Haptics} from "@capacitor/haptics";
    import { ForegroundService } from '@ionic-native/foreground-service/ngx';
    ...
    constructor(private fg_service:ForegroundService){}
    ...
    this.fg_service.start('App name', 'Service name', 'drawable/fsicon');
    Haptics.vibrate({duration:500});