将LocalNotifications设置为仅在醒着的时间点火

时间:2015-04-05 22:18:43

标签: javascript cordova notifications localnotification telerik-appbuilder

我正在使用teleriks验证LocalNotification插件,以便向运行我的应用的设备发送本地通知。我允许他们在通知之间选择他们喜欢的间隔(1-2-3或4小时),但我希望这些通知只在醒着的时候开火(所以他们不会唤醒用户)在nigth的中间 - 是否有这样的设置?到目前为止,通知代码是:

 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;
        }

    }

1 个答案:

答案 0 :(得分:0)

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;
        }

    }