title: Cordova/Phonegap wake up app on incoming webrtc phone call
body: <p>I have written an audio/video call hybrid app written with cordova/phonegap. I used</p>
  
  <p>1) webrtc for actual streaming of audio or video</p>
  
  <p>2) server sent events (SSE) to listen for incoming calls. <a href="https://apifriends.com/api-streaming/server-sent-events/" rel="noreferrer">https://apifriends.com/api-streaming/server-sent-events/</a></p>
  
  <p>3) simple-peer <a href="https://github.com/feross/simple-peer" rel="noreferrer">https://github.com/feross/simple-peer</a></p>
  
  <p>When the app is open, everything goes great, i can receive incoming call signals etc and connect. The problem is that when the app is off. I cannot open app or get any kind of indication of call status.</p>
  
  <p>I decided to do some digging quickly found out that this was not possible from within a webview. I tried some plugins like <a href="https://github.com/katzer/cordova-plugin-background-mode" rel="noreferrer">https://github.com/katzer/cordova-plugin-background-mode</a> which worked great for some time but would quickly drain the battery and your app may be rejected on appstore notices.</p>
  
  <p>When my app is opened, incoming calls will be correctly captured. all i need is to open the app. I read about background push messages (silent) but failed to get these to open app. The push messages handlers were only called when app is in foreground. </p>
  
  <p>So i finally fell on this doc and its the closest i got with this answer. There is no direct support for background service in Phonegap/Cordova apps.</p>
  
  <blockquote>
    <p>The reason is that your hybrid app is written in JS, and your JS code
    runs in an Activity that has a webview. As soon as you exit your app,
    the app is suspended.</p>
    
    <p>If you really want to have background service in android, you can
    write your Android service in Java (native) and call your service from
    your JS code. (Native for background service &amp; JS for your app)</p>
  </blockquote>
  
  <p><a href="https://forum.ionicframework.com/t/how-to-run-cordova-plugin-in-android-background-service/6677/3" rel="noreferrer">https://forum.ionicframework.com/t/how-to-run-cordova-plugin-in-android-background-service/6677/3</a></p>
  
  <p>From the above, i gather i would have to do the actual logic of detecting incoming calls from the java service and if i detect anything i can force my application to open. Below is simple logic i do once app is open. </p>
  
  <pre><code>if (documentLoaded &amp;&amp; isLoggedIn)
  {
  //url returns json only if there is an incoming call otherwise empty
  
  var source = new EventSource("https://www.url.com/api/sse/incomingcall.php?userid=" + userid, {
          withCredentials: true
      });
  source.addEventListener("incomingcalls", function (event) {
      var callSession = JSON.parse(event.data);
      // HandleIncomingCall();
  }, false);
  }
  </code></pre>
  
  <p>My issue is that i am not a java coder and havent written a plugin before. how would i create an android service that will listen to above url periodically and if detect any json open my app. </p>