[英]Phonegap: local notification repeat every Sunday of the week?
我正在尝试在我的项目中实现Phonegap本地通知。
我正在使用这个插件:
de.appplant.cordova.plugin.local-notification-custom
我已经安装了插件并对其进行了测试,并且工作正常。
我使用此代码测试它,它工作正常:
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
运行的本地通知。
我遇到过这样的事情但是在测试它时它什么也没做:
cordova.plugins.notification.local.schedule({
id: 1,
title: "Test...",
text: "Test...",
sound: null,
every: 'week',
at: sunday_16_pm
});
我甚至不知道是否at: sunday_16_pm
是否正确!
有人可以就这个问题提出建议吗?
提前致谢。
编辑:
搜索了几个小时后什么都没发现,我刚看到这个文档:
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
是什么?!? 那是一个变量吗? 如果是这样,你如何创建该变量?
我不明白为什么人们写文档就好像没有人想要阅读/理解它们一样!
另一个编辑:
我发现这可以解释我正在尝试做什么,但我没有使用离子,从来没有。 所以我根本不理解那里提供的代码!
https://www.joshmorony.com/getting-familiar-with-local-notifications-in-ionic-2/
我不知道那些变量sunday_16_pm
或monday
,但你可以使用自己的变量firstAt
。
首先,你必须找到sunday_16_pm
的时间戳告诉这个插件重复应该在周日下午开始。
为了找到这个时间戳(我想这应该动态完成),我写了函数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
});
还有一个例子:
要在星期日getDayMillDiff
的其他日子测试getDayMillDiff
,您只需将字符串"monday"
传递给它(请始终使用getDayMillDiff
变量days
列出的名称):
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
});
希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.