2014-02-21 70 views
1

我正在開發針對firefox操作系統的應用程序,並且出現報警問題。如果手機被鎖定,警報不會觸發。如果手機處於阻止狀態,則報警丟失

手機處於阻止狀態時會出現問題,因爲通知桌面不會觸發。但是,當您解鎖手機時,該APP將處於後臺,但沒有任何事情發生。

編輯

解決方案中的代碼。然後,我認爲問題消失了。

我可以利用這個功能添加報警:

// the solution for the app working right is writting this window.onload function 
window.onload = function() { 
    function addAlarm(){ 
      // Data alarm will be fire 
      var myDate = new Date(); 

      myDate.setMinutes(myDate.getMinutes()+1); 

      // information for alarm 
      var data = { 
       foo: "bar" 
      } 

      var request = navigator.mozAlarms.add(myDate, "ignoreTimezone", data); 

      request.onsuccess = function() { 
       console.log("La alarma ha sido programada"); 
      }; 

      request.onerror = function() { 
       console.log("Ha ocurrido un error: " + this.error.name); 
      }; 
    } 

    //That get the alarm event to fire the alarm and also add a desktop notification. 

    navigator.mozSetMessageHandler("alarm", function (mozAlarm) { 
     console.log('fired Alarm'); 
     // function that add a desktop notification 
     notifyMe(); 
     $('#alarmFired').append('<p>alarm fired: '+JSON.stringify(mozAlarm)+'</p>'); 

    }); 

} 

功能的桌面通知:

function notifyMe() { 
    var options = {body: "notification body", icon : 'http://www.famfamfam.com/lab/icons/mini/icons/icon_accept.gif'} 

    // Let's check if the browser supports notifications 
    if (!("Notification" in window)) { 
    alert("This browser does not support desktop notification"); 
    } 

    // Let's check if the user is okay to get some notification 
    else if (Notification.permission === "granted") { 
    // If it's okay let's create a notification 
    var notification = new Notification("Hi there!", options); 
    } 

    // Otherwise, we need to ask the user for permission 
    // Note, Chrome does not implement the permission static property 
    // So we have to check for NOT 'denied' instead of 'default' 
    else if (Notification.permission !== 'denied') { 
    Notification.requestPermission(function (permission) { 

     // Whatever the user answers, we make sure we store the information 
     if(!('permission' in Notification)) { 
     Notification.permission = permission; 
     } 

     // If the user is okay, let's create a notification 
     if (permission === "granted") { 
     var notification = new Notification("Hi there!", options); 
     } 
    }); 
    } 

    notification.onclick = function(){ 
    console.log('clicked notification'); 
    openApp(); 
    } 
} 
+0

請使用回答發佈答案,不要在問題中編輯它。在同一頁面上:不要使用答案作爲評論,說你已經在你的問題中回答:)。聽起來令人困惑,但基本上只是將問題用於問題以及答案和答案,而且你會很好 – Nanne

回答

0

帕科,

你也許可以利用以下的技術

// Log visibility of the app 
var logVisibility = document.querySelector("#log-visibility"), 
    logVisibilityDisplay = document.querySelector("#log-visibility-display"); 
if (logVisibility && logVisibilityDisplay) { 
    logVisibility.onclick = function() { 
     logVisibilityDisplay.style.display = "block"; 
     logVisibilityDisplay.innerHTML = "I have focus!<br>" 
     document.addEventListener("visibilitychange", function() { 
      if (document.hidden) { 
       console.log("Firefox OS Boilerplate App is hidden"); 
       logVisibilityDisplay.innerHTML += "Now I'm in the background<br>"; 
      } 
      else { 
       console.log("Firefox OS Boilerplate App has focus"); 
       logVisibilityDisplay.innerHTML += "I have focus!<br>"; 
      } 
     }); 
    }; 
} 

與請求鎖屏組合像

// Keep screen on 
var lock = null; 
var keepscreen = document.querySelector("#keep-screen-on"); 
if (keepscreen) { 
    keepscreen.onclick = function() { 
     if (!lock) { 
      lock = window.navigator.requestWakeLock('screen'); 
      keepscreen.innerHTML = "Remove the lock"; 
     } 
     else { 
      lock.unlock(); 
      lock = null; 
      keepscreen.innerHTML = "Keep screen on"; 
     } 
    }; 
} 

我在這裏的想法是:

  1. 添加您的報警
  2. 當你的函數,將觸發報警被調用,你試試請求鎖在屏幕上(設備不能用這種方式鎖定)。
  3. 在您的鬧鈴關閉後(用戶互動將其關閉),您再次釋放屏幕鎖定。

我不是100%確定這會起作用,但這正是我現在能想到的。

編輯

歐凱,我已搜索周圍多一點,你可能想使用的,而不是篩網鎖激活鎖定。欲瞭解更多信息,請查看Screen WebAPI at mdn

相關問題