简体   繁体   English

Phonegap:本周每周日重播本地通知?

[英]Phonegap: local notification repeat every Sunday of the week?

I'm trying to implement the Phonegap local notification in my project. 我正在尝试在我的项目中实现Phonegap本地通知。

I'm using this plugin: 我正在使用这个插件:

de.appplant.cordova.plugin.local-notification-custom

I have installed the plugin and tested it and it works fine. 我已经安装了插件并对其进行了测试,并且工作正常。

I tested it with this code and it works fine: 我使用此代码测试它,它工作正常:

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

The above notification will run every minute and work fine. 以上通知将每分钟运行并且正常工作。

Now, I need to set a local notification that will only run on every Sunday and every week . 现在,我需要设置一个仅在每个Sundayweek运行的本地通知。

I came across something like this but when tested it, it does nothing: 我遇到过这样的事情但是在测试它时它什么也没做:

cordova.plugins.notification.local.schedule({
    id: 1,
    title: "Test...",
    text: "Test...",
    sound: null,
    every: 'week',
    at: sunday_16_pm
});

I don't even know if at: sunday_16_pm is correct or not! 我甚至不知道是否at: sunday_16_pm是否正确!

Could someone please advice on this issue? 有人可以就这个问题提出建议吗?

Thanks in advance. 提前致谢。

EDIT: 编辑:

After searching for hours and finding nothing, I just came across this documentation: 搜索了几个小时后什么都没发现,我刚看到这个文档:

https://github.com/katzer/cordova-plugin-local-notifications/wiki/04.-Scheduling https://github.com/katzer/cordova-plugin-local-notifications/wiki/04.-Scheduling

They have a sample code that says: 他们有一个示例代码说:

Schedule Repeatedly 重复安排

cordova.plugins.notification.local.schedule({
    text: "Delayed Notification",
    firstAt: monday,
    every: "day",
    icon: "file://img/logo.png"
}, callback);

But what is monday ?!? monday是什么?!? is that a variable? 那是一个变量吗? And if so, how do you create that variable? 如果是这样,你如何创建该变量?

I don't understand why people write documentation as if no one else would want to read/understand them!! 我不明白为什么人们写文档就好像没有人想要阅读/理解它们一样!

Another edit: 另一个编辑:

I found this which explains exactly what i'm trying to do but I'm not using ionic and never have. 我发现这可以解释我正在尝试做什么,但我没有使用离子,从来没有。 So I don't understand the code that is provided there at all! 所以我根本不理解那里提供的代码!

https://www.joshmorony.com/getting-familiar-with-local-notifications-in-ionic-2/ https://www.joshmorony.com/getting-familiar-with-local-notifications-in-ionic-2/

I don't know about those variables sunday_16_pm or monday either, but you can use your own variable with firstAt . 我不知道那些变量sunday_16_pmmonday ,但你可以使用自己的变量firstAt

First of all you have to find the timestamp for sunday_16_pm to tell this plugin that the repeating should start on sunday afternoon. 首先,你必须找到sunday_16_pm的时间戳告诉这个插件重复应该在周日下午开始。

In order to find this timestamp (that I suppose this should be done dynamically), I wrote the function getDayMillDiff to calculate the time-difference between now and sunday. 为了找到这个时间戳(我想这应该动态完成),我写了函数getDayMillDiff来计算现在和星期日之间的时差。 Afterwards this difference is used to obtain the desired sunday_16_pm . 之后,这个差异用于获得所需的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
});

One more example: 还有一个例子:

To test getDayMillDiff for other days than sunday, you can simply pass the string "monday" onto it (please use always a name listed within the variable days in getDayMillDiff ): 要在星期日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
});

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM