我正在使用插件在 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);
});
}
任何帮助将不胜感激。谢谢
https://github.com/katzer/cordova-plugin-local-notifications/issues/1533#issuecomment-514521588
如果有人仍然找到这篇文章,该错误仍然存在,但幸运的是,它是由原作者修复和提交的,因为他没有创建一个新版本,基本上你必须删除插件并安装回购的负责人,只需按照我提供的链接中的步骤操作。
我遇到了同样的问题,我所做的是修改 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;
}
}
@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 设备中仅触发一次通知。