【问题标题】:My JavaScript code stop running when the mobile screen is locked?当手机屏幕被锁定时,我的 JavaScript 代码停止运行?
【发布时间】:2018-03-28 21:12:37
【问题描述】:

这是我的代码:

firebase.initializeApp(config);
if(!window.Notification){
  alert('Not supported');
}else{
  Notification.requestPermission().then(function(p){
    if(p=='denied'){
      alert('You denied to show notification');
    }else if(p=='granted'){
      alert('You allowed to show notification');
    }
  });
}
var database = firebase.database().ref("sensor/Motion");
database.on('child_added', function() {

  $("#Mt").val("Motion detected");
  $("#dateM").val( moment().format('LLL') );
  if(Notification.permission!=='default'){
    var notification = new Notification('Alert', {
      icon: 'alert-icon-red.png',
      body: "Motion detected!",
    });

  }else{
    alert('Please allow the notification first');
  }

在我的桌面浏览器中一切正常,但在我的 Android 设备上,当屏幕锁定时,它可以工作几分钟,然后就无法工作。我没有收到任何通知。

【问题讨论】:

    标签: javascript jquery firebase firebase-realtime-database


    【解决方案1】:

    听起来您的移动浏览器在屏幕锁定时正在停止任务,这可能会延长电池寿命。总的来说,这听起来是件好事。

    如果您想在用户未使用设备时提醒他们发生的事件,请考虑使用 Firebase 云消息传递来发送和处理这些消息。这可确保它使用的通信渠道在用户未积极使用应用时更有可能处于活跃状态。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-12
      • 1970-01-01
      • 2019-04-15
      相关资源
      最近更新 更多