【问题标题】:setting LocalNotifications to only fire in waking hours将本地通知设置为仅在清醒时间触发
【发布时间】:2015-04-05 22:18:43
【问题描述】:

我正在使用 Teleriks 验证的 LocalNotification 插件向运行我的应用程序的设备发送本地通知。我允许他们在通知之间选择他们喜欢的时间间隔(1-2-3 或 4 小时),但我希望这些通知仅在醒着的时间触发(这样他们就不会在中间唤醒用户晚上的 - 有这个设置吗?到目前为止,通知代码是:

 app.notify({
              id : 4,
              title : 'Remember',
              message : '.. until you cancel all notifications',
              repeat : 60*this.get('notification_interval'),
              autoCancel : true,
              date : new Date(new Date().getTime() + 60*60*1000*this.get('notification_interval'))
                        },function(){alert('you will recieve a message every '+this.get('notification_interval')+'. hour');});

编辑

根据 AtanuCSE 的优秀评论,我编辑了代码,现在看起来像:

app.setupnotificationinterval(interval)
    {
         //set hour to 10 for starting hour 
        var hour = 10;

        //set a notificationDate time.
        var notificationDate = new Date().getTime();
        //manipulate the dateobject to fit, 10:00:00
        notificationDate.setMinutes(0);notificationDate.setSeconds(0);
        //loop as long as the notification hour interval doesnt exceed 12 hours (from 10-22).
        while(hour < 22)
        {
           notificationDate.setHours(hour);

           this.notify({
                 id : hour,
              title : 'Husk at registrere energi',
            message : 'Det er tid til at du skal registrere din energi',
             repeat : 'daily',
         autoCancel : true,
               date : notificationDate
        },function(){alert('alarm sat til kl. ' + notificationDate.getHour());});
            hour = hour + interval;
        }

    }

【问题讨论】:

  • 我不知道答案,但我有一个想法。您可以设置多个闹钟。就像间隔 4 小时一样,您可以在上午 10 点、下午 2 点、下午 6 点、晚上 10 点等设置闹钟,每天重复。这样可以避免睡眠时间。
  • 当然 - 谢谢 :)

标签: javascript cordova notifications localnotification telerik-appbuilder


【解决方案1】:
app.setupnotificationinterval(interval)
    {
         //set hour to 10 for starting hour 
        var hour = 10;

        //set a notificationDate time.
        var notificationDate = new Date().getTime();
        //manipulate the dateobject to fit, 10:00:00
        notificationDate.setMinutes(0);notificationDate.setSeconds(0);
        //loop as long as the notification hour interval doesnt exceed 12 hours (from 10-22).
        while(hour < 22)
        {
           notificationDate.setHours(hour);

           this.notify({
                 id : hour,
              title : 'Husk at registrere energi',
            message : 'Det er tid til at du skal registrere din energi',
             repeat : 'daily',
         autoCancel : true,
               date : notificationDate
        },function(){alert('alarm sat til kl. ' + notificationDate.getHour());});
            hour = hour + interval;
        }

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多