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

时间:2017-12-14 16:15:29

标签: android ios cordova phonegap-build

我试图在我的项目中实施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/

1 个答案:

答案 0 :(得分:2)

我也不知道这些变量sunday_16_pmmonday,但您可以将自己的变量与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除了星期日以外的其他日期,您只需将字符串"monday"传递给它(请始终使用days中变量getDayMillDiff中列出的名称):

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

希望它有所帮助。

相关问题