React native React Native-Expo-localschedule通知一次抛出多个通知,而不是Android上的单个通知
我正在尝试创建每小时一次具有通知功能的水提醒应用程序。 我已经用scheduleLocalNotificationAsync实现了通知。我的实现在IOS中可以正常工作,但在Android中,当我打开应用程序时,应用程序会同时发送12个通知,而不是在预定时间逐个发送React native React Native-Expo-localschedule通知一次抛出多个通知,而不是Android上的单个通知,react-native,notifications,react-native-android,expo,React Native,Notifications,React Native Android,Expo,我正在尝试创建每小时一次具有通知功能的水提醒应用程序。 我已经用scheduleLocalNotificationAsync实现了通知。我的实现在IOS中可以正常工作,但在Android中,当我打开应用程序时,应用程序会同时发送12个通知,而不是在预定时间逐个发送 componentDidMount = () => { this._sendNotifications(); }; _sendNotifications = async () => { // Not to cre
componentDidMount = () => {
this._sendNotifications();
};
_sendNotifications = async () => {
// Not to create duplicated notifications first cancel all notifications
await Notifications.cancelAllScheduledNotificationsAsync();
// beginning of notification part
const localnotification = {
title: 'Water Reminder',
body: "Don't forget to drink water!",
android: {
sound: true,
},
ios: {
sound: true,
},
};
// get the current date
let currentDate = Date.now();
currentDate = new Date(currentDate);
// get the day, month and year from current date to create time to schedule
let year = currentDate.getFullYear();
let month = currentDate.getMonth();
let date = currentDate.getDate();
// then create unix epoch number for eact date with number from notification section
// then we call notification function with each timestamp (not1)
if (this.state.switchStatus === false) {
await Notifications.cancelAllScheduledNotificationsAsync();
} else {
// Notification for nine
if (this.state.otherSwitchStatus.nine === true) {
let not0 = new Date(year, month, date, 9);
not0 = Date.parse(not0);
const schedulingOptions0 = { time: not0, repeat: 'day' };
// call the function to send notification at 9:00
await Notifications.scheduleLocalNotificationAsync(localnotification, schedulingOptions0);
}
// Notification for ten
if (this.state.otherSwitchStatus.ten === true) {
let not1 = new Date(year, month, date, 10);
not1 = Date.parse(not1);
const schedulingOptions1 = { time: not1, repeat: 'day' };
// call the function to send notification at 10:00
await Notifications.scheduleLocalNotificationAsync(localnotification, schedulingOptions1);
}
}
我想我了解代码的情况 正如您在我的代码中看到的,独立于一天中的当前时间,我调用scheduleLocalNotificationAsync 15次(从09:00到23:00,使用daily repeat选项)。但是,当我在12:40打开通知屏幕时,我立即收到4个通知(09:00、10:00、11:00和12:00各一个) 我猜expo会立即调用通知函数4次。ios的情况并非如此,但安卓的情况正是如此。我想它需要在世博会一侧固定 在此之前,我通过检查当前时间并将其与计划时间进行比较来解决问题,如果时间已过,我将其安排在明天
let hour = currentDate.getHours();
// I changed the code below
// let not0 = new Date(year, month, date, 9)
// to this
let not0 = new Date(year, month, hour > 9 ? date + 1 : date, 9);
我还创建了一个问题中心。