我尝试使用expo(react native(调度本地通知,但发生了一些非常奇怪的事情。
这是我写的代码:
const localNotification = {
title: 'test',
body: 'test body',
ios: {
sound: true
}
};
let t = new Date();
t.setSeconds(t.getSeconds() + 10);
const schedulingOptions = {
time: t, // (date or number) — A Date object representing when to fire the notification or a number in Unix epoch time. Example: (new Date()).getTime() + 1000 is one second from now.
repeat: 'minute'
};
Notifications.scheduleLocalNotificationAsync(localNotification, schedulingOptions);
然后,每当应用程序处于后台时,我都会收到同样的通知。
我删除了那段代码,改为使用Notifications.presentLocalNotificationAsync()
,我希望之前的通知停止出现,因为Notifications.scheduleLocalNotificationAsync()
从未在整个应用程序中的任何地方调用过。
在安卓系统上,我重新启动了手机,通知不再出现,但在iOS上,即使在重新启动并重新安装expo后,我仍会收到相同的预定通知。
我试着给Notifications.cancelAllScheduledNotificationsAsync()
打电话,但没有成功。
当您的代码名为scheduleLocalNotificationAsync()
时,它向操作系统注册了一个重复通知-仅删除代码是不够的。
我认为Notifications.cancelAllScheduledNotificationsAsync()
就是您想要的:
https://docs.expo.io/versions/latest/sdk/notifications/#notificationscancelallschedulednotificationsasync