首页 > 解决方案 > 取消 iOS 上的所有展会本地预定通知

问题描述

我尝试使用 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()没有运气。

标签: iosreact-nativenotificationsuilocalnotificationexpo

解决方案


当您的代码调用scheduleLocalNotificationAsync()它时,它会向操作系统注册一个定期通知 - 删除代码是不够的。

我认为Notifications.cancelAllScheduledNotificationsAsync()这是您正在寻找的:

https://docs.expo.io/versions/latest/sdk/notifications/#notificationscancellallschedulednotificationsasync


推荐阅读