Phonegap:每周星期天重复本地通知?
我试图在我的项目中实现Phonegap本地通知。
我正在使用这个插件:
de.appplant.cordova.plugin.local-notification-custom
我已经安装了插件,并testing它,它工作正常。
我testing了这个代码,它工作正常:
cordova.plugins.notification.local.schedule({ id : 1, title : 'I will bother you every minute', text : '.. until you cancel all notifications', sound : null, every : 'minute', autoClear : false, at : new Date(new Date().getTime() + 10*1000) });
上述通知将每分钟运行并正常工作。
现在,我需要设置一个只能在每个Sunday
和week
运行的本地通知。
我遇到过这样的事情,但是在testing时,它什么都不做:
cordova.plugins.notification.local.schedule({ id: 1, title: "Test...", text: "Test...", sound: null, every: 'week', at: sunday_16_pm });
我甚至不知道at: sunday_16_pm
是否正确!
有人能请这个问题的build议吗?
提前致谢。
编辑:
找了几个小时找不到,我刚刚遇到这个文档:
https://github.com/katzer/cordova-plugin-local-notifications/wiki/04.-Scheduling
他们有一个示例代码说:
计划重复
cordova.plugins.notification.local.schedule({ text: "Delayed Notification", firstAt: monday, every: "day", icon: "file://img/logo.png" }, callback);
但是monday
呢?! 是一个variables? 如果是的话,你如何创build这个variables?
我不明白为什么人们写文档就好像别人不想读/理解它们一样!
另一个编辑:
我发现这正是我想要做的事情,但是我并没有使用离子,从来没有。 所以我不明白那里提供的代码!
我不知道这些variablessunday_16_pm
或monday
,但你可以使用firstAt
自己的variables。
首先,你必须findsunday_16_pm
的时间戳,告诉这个插件,重复应该在星期天下午开始。
为了find这个时间戳(我认为这应该是dynamic的),我写了函数getDayMillDiff
来计算现在和星期日之间的时间差。 之后,这个差异被用来获得所需的sunday_16_pm
。
function getDayMillDiff(refday){ var days = { monday: 1, tuesday: 2, wednesday: 3, thursday: 4, friday: 5, saturday: 6, sunday: 0 }; if(!days.hasOwnProperty(refday))throw new Error(refday+" is not listed in "+JSON.stringify(days)); var curr = new Date(); var triggerDay = days[refday]; var dayMillDiff=0; var dayInMill = 1000*60*60*24; // add a day as long as refday(sunday for instance) is not reached while(curr.getDay()!=triggerDay){ dayMillDiff += dayInMill; curr = new Date(curr.getTime()+dayInMill); } return dayMillDiff; } var today = new Date(); // how many days are between current day (thursday for instance) to sunday, add this difference to this sunday variable var sunday = today.getTime() + getDayMillDiff("sunday"); // convert timestamp to Date so that hours can be adjusted var sunday_16_pm = new Date(sunday); sunday_16_pm.setHours(16,0,0); // now we can use sunday_16_pm to schedule a notification showing at this date and every past week cordova.plugins.notification.local.schedule({ id: 1, title: "Test...", text: "Test...", every: 'week', firstAt: sunday_16_pm });
再举一个例子:
要testinggetDayMillDiff
的其他日子而不是星期天,可以简单地将string"monday"
传递给它(请在getDayMillDiff
使用variablesdays
中列出的名称):
var today = new Date(); var monday = today.getTime() + getDayMillDiff("monday"); var monday_10_am = new Date(monday); monday_10_am.setHours(10,0,0); cordova.plugins.notification.local.schedule({ id: 1, title: "Test...", text: "Test...", every: 'week', firstAt: monday_10_am });
希望能帮助到你。