本地通知在Android设备中连续触发一分钟

时间:2019-03-27 14:14:32

标签: javascript ionic-framework ionic2 ionic3 ionic-native

我正在使用插件在ionic 2应用程序中处理本地通知-

ionic cordova plugin add cordova-plugin-local-notification
npm install @ionic-native/local-notifications

该通知在ios设备上运行得非常好,但是对于android来说,则显示出意外的行为。

异常行为- 在设定的时间当天重复通知。

当前行为- 通知持续一分钟。

我的要求是在每周的特定日期重复由用户设置的本地通知。通知也应在用户设置的同一天和同一时间重复。

这是我的代码段-

makeLocalNotificationOn(mainDayNotifyObj, index) {
    let currentDate = new Date();
    let currentDay = currentDate.getDay(); // Sunday = 0, Monday = 1, etc.

    let indexOfDay;

    for (let i = 0; i < this.remainderDaysArray.length; i++) {
        if (mainDayNotifyObj.dayName == this.remainderDaysArray[i]) {
            indexOfDay = i;
            break;
        }
    }

    let setTime = mainDayNotifyObj.time.split(":");
    let hours = parseInt(setTime[0]);
    let minutes = parseInt(setTime[1]);


    let firstNotificationTime = new Date();
    let dayDifference = indexOfDay - currentDay;

    if (dayDifference < 0) {
        dayDifference = dayDifference + 7; // for cases where the day is in the following week
    }

    firstNotificationTime.setHours(firstNotificationTime.getHours() + (24 * (dayDifference)));
    firstNotificationTime.setHours(hours);
    firstNotificationTime.setMinutes(minutes);

    let weekDay = firstNotificationTime.getDay();

       let remainderObj = {
            id: index,
            title: 'Hello',
            text: 'Its time',
            foreground: true,
            trigger: {every: {weekday: weekDay, hour: hours, minute: minutes},firstAt: firstNotificationTime}
        };

    this.notificationArray.push(remainderObj);

    this.platform.ready().then(() => {
        this.localNotifications.schedule(this.notificationArray);

    });

}

任何帮助将不胜感激。 谢谢

2 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,我所做的就是修改MatchTrigger.java来比较秒数,因此它仅触发第二个00

因此搜索

if (cal.get(Calendar.MINUTE) < now.get(Calendar.MINUTE)) {
            switch (unit) {
                case MINUTE:
                    addToDate(cal, now, Calendar.MINUTE, 1);
                    break;
                case HOUR:
                    addToDate(cal, now, Calendar.HOUR_OF_DAY, 1);
                    break;
                case DAY:
                case WEEK:
                    addToDate(cal, now, Calendar.DAY_OF_YEAR, 1);
                    break;
                case MONTH:
                    addToDate(cal, now, Calendar.MONTH, 1);
                    break;
                case YEAR:
                    addToDate(cal, now, Calendar.YEAR, 1);
                    break;
            }
        }

,然后将结束的} 替换为

} else
        if (cal.get(Calendar.SECOND) < now.get(Calendar.SECOND)) {
            switch (unit) {
                case MINUTE:
                    addToDate(cal, now, Calendar.MINUTE, 1);
                    break;
                case HOUR:
                    addToDate(cal, now, Calendar.HOUR_OF_DAY, 1);
                    break;
                case DAY:
                case WEEK:
                    addToDate(cal, now, Calendar.DAY_OF_YEAR, 1);
                    break;
                case MONTH:
                    addToDate(cal, now, Calendar.MONTH, 1);
                    break;
                case YEAR:
                    addToDate(cal, now, Calendar.YEAR, 1);
                    break;
            }
        }

答案 1 :(得分:0)

@valencianok非常感谢您的回复。

我通过修改触发器对象的值解决了我的问题。

我用来触发通知的其余对象有一些更改。

这是代码段-

remainderObj = {
            id: index,
            title: 'Hello',
            text: 'Its time',
            foreground: true,
            trigger{every{weekday:weekDay,hour:hours,minute:minutes},firstAt: firstNotificationTime,count:1}
        }

count 属性完成了工作,count = 1仅在Android设备中触发一次通知。