[英]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.