benjamindean / flutter_vibration

Handle vibration on iOS and Android in Flutter apps
219 stars 68 forks source link

Added web support #43

Closed roulljdh closed 3 years ago

roulljdh commented 3 years ago

Hello, I added web support by interfacing with the JS Vibration API https://developer.mozilla.org/en-US/docs/Web/API/Vibration_API

I created an internal web package and referenced it on the main pubspec.yaml

Compared to the vanilla package, the web API lacks support for hasAmplitudeControl and hasCustomVibrationsSupport functions so on the method call, I just returned a Future with the value of false.

I ran the example project and it worked. I also installed this modified package via path on my existing app and created a web build and it worked like a charm.

It will work on Android devices on Chromium browsers and Firefox. For iOS,/iPadOS Safari doesn't support it though. I have an iPad and was able to confirm it. Though I am curious if it will work on Chromium browsers for iPhones. The iPad doesn't have a vibration motor so I can't test that myself. The hasVibrator function will return true on desktops even though they don't have a vibration motor. That's just how the browser API works and for mobile the MDN has notes regarding that

Beginning in Chrome 55, this is not supported in cross-origin iframes. Notes Beginning in Chrome 60, this method requires a user gesture. Otherwise it returns false.

So on the user side they have to be invoked using an event like click to trigger.

I'll leave the rest to you with regards to documentation, changelog etc.

I hope you would look into this and consider adding it.

Best regards.

benjamindean commented 3 years ago

Thank you for the contribution! I reviewed it and everything looks great, but I'll be able to merge it and release a new version on Monday.