简体   繁体   中英

Phonegap: local notification repeat every Sunday of the week?

I'm trying to implement the Phonegap local notification in my project.

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 .

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!

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

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 ?!? 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/

I don't know about those variables sunday_16_pm or monday either, but you can use your own variable with 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.

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. Afterwards this difference is used to obtain the desired 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 ):

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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