如果手机处于阻塞状态,则警报会丢失

时间:2014-02-21 11:21:36

标签: notifications alarm firefox-os

我正在为firefox OS开发一个应用程序,我遇到了警报问题。如果手机被阻挡,警报不会触发。

当手机处于阻止状态时会出现问题,因为通知桌面不会触发。但是当你解锁手机时,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();
  }
}

1 个答案:

答案 0 :(得分: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. 关闭闹钟后(用户交互关闭),再次释放屏幕锁定。
  4. 我不是百分百肯定这会奏效,但这就是我现在能想到的。

    修改

    哦,我已经搜索了一下,你可能想用wakeLock代替sreen lock。有关详细信息,请查看Screen WebAPI at mdn