在离子v1中每天发送特定时间的通知

时间:2017-09-15 11:24:20

标签: cordova ionic-framework android-notifications localnotification

我希望每天晚上8点在离子v1发送通知。我已经引用了以下代码,但它给出了异常。这里应该使用哪种日期时间格式。我从来没有找到完整的代码。请帮帮我。

https://github.com/katzer/cordova-plugin-local-notifications/wiki/11.-Samples

 document.addEventListener('deviceready', function () {
// Schedule notification for tomorrow to remember about the meeting
cordova.plugins.notification.local.schedule({
    id: 10,
    title: "Meeting in 15 minutes!",
    text: "Jour fixe Produktionsbesprechung",
    at: monday_9_am,
    data: { meetingId:"#123FG8" }
});

// Join BBM Meeting when user has clicked on the notification 
cordova.plugins.notification.local.on("click", function (notification) {
    if (notification.id == 10) {
        joinMeeting(notification.data.meetingId);
    }
});

// Notification has reached its trigger time (Tomorrow at 8:45 AM)
cordova.plugins.notification.local.on("trigger", function (notification) {
    if (notification.id != 10)
        return;

    // After 10 minutes update notification's title 
    setTimeout(function () {
        cordova.plugins.notification.local.update({
            id: 10,
            title: "Meeting in 5 minutes!"
        });
    }, 600000);
});
 }, false);

/**********Exception***************/
ionic.bundle.js:26794 ReferenceError: monday_9_am is not defined
at Channel.<anonymous> (controllers.js:4672)
at Channel.subscribe (cordova.js:775)
at document.addEventListener (cordova.js:133)
at HTMLDocument.document.addEventListener (cordova.js:1703)
at controllers.js:4636
at Scope.$emit (ionic.bundle.js:30645)
at Object.emit (ionic.bundle.js:58086)
at transitionComplete (ionic.bundle.js:58032)
at HTMLElement.completeOnTransitionEnd (ionic.bundle.js:58012)
at defaultHandlerWrapper (ionic.bundle.js:16787)

1 个答案:

答案 0 :(得分:0)

来自here

cordova.plugins.notification.local.schedule({
    id: 1,
    title: "Message Title",
    message: "Message Text",
    firstAt: date, // firstAt and at properties must be an IETF-compliant RFC 2822 timestamp
    every: "week", // this also could be minutes i.e. 25 (int)
    sound: "file://sounds/reminder.mp3",
    icon: "http://icons.com/?cal_id=1",
    data: { meetingId:"123#fg8" }
});

具体做法是:

// firstAt and at properties must be an IETF-compliant RFC 2822 timestamp

我相信你可以创建一个JS Date对象,然后运行.toUTCString()来生成一个兼容的时间戳。