Search code examples
cordovaphonegap-pluginscordova-plugins

Stop navigator.notification.beep(100);


I've a cordova app with a beep notification:

navigator.notification.beep(100);

It works well, but I want the user to be able to stop the device from beeping.

How do I remove the notification or stop the device from beeping?


Solution

  • Did you give this a try? While an user in your app you could make use of setInterval and play the beep-noise once every 5 seconds:

    var stop = false;
    ...
    ...
    var inter = setInterval(function(){
    
       if(!stop)navigator.notification.beep(1);
       else clearInterval(inter);
    
    },5000);
    ...
    function stopBeepNoise(){ stop = true; }
    

    The delay of the interval should correspond to the length of the beep-noise. For android the length of the beep-noise approximately takes 5 seconds because of what I can see within the source-code of this plugin is that 5 seconds(long timeout) have to pass so that a timeout has ended while the program is an loop and/or the length of noise is reached so that isPlaying returns false so you can not absolutely be sure that after 5 seconds a beep-noise has ended it might be less than 5 seconds:

    for (long i = 0; i < count; ++i) {
          notification.play();
          long timeout = 5000;
          while (notification.isPlaying() && (timeout > 0)) {
               timeout = timeout - 100;
               try {
                   Thread.sleep(100);
               } catch (InterruptedException e) {
               }
          }
    }  
    

    of course for ios it looks different but here I would suggest to test the adjusted delay in real situations on a ios/winphone/-device.

    stopBeepNoise could be called by touching a button and so stopping the interval.