我尝试使用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()
在整个应用程序的任何地方都不会被调用。
在 Android 上,我重新启动了手机,通知不再出现,但在 iOS 上,即使在重新启动并重新安装 expo 后,我仍然收到相同的预定通知。
我尝试打电话给
Notifications.cancelAllScheduledNotificationsAsync()
,但没有成功。
当您的代码调用
scheduleLocalNotificationAsync()
时,它会向操作系统注册一个重复通知 - 删除代码是不够的。
我认为
Notifications.cancelAllScheduledNotificationsAsync()
就是您正在寻找的:
从“expo-notifications”导入 * 作为通知; 进口 { 取消所有预定通知异步, } 来自“博览会通知”;
取消所有通知();