You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Expo React Native如何在指定日期仅发送一次通知?

解决方案

在Expo Notifications中,你可以直接使用date字段来指定单次触发的具体日期,这是官方支持但你可能没留意到的参数,完全能满足你的需求。

实现步骤:

  • 计算当前时间加上两个月后的Date对象(setMonth方法会自动处理月份溢出,比如11月加2个月会自动转到下一年的1月)
  • 在trigger中传入这个date对象,并设置repeats: false确保只触发一次

完整代码示例:

async function scheduleTwoMonthReminder() {
  // 获取当前时间
  const now = new Date();
  // 计算两个月后的日期
  const twoMonthsLater = new Date(now);
  twoMonthsLater.setMonth(twoMonthsLater.getMonth() + 2);

  await Notifications.scheduleNotificationAsync({
    content: {
      title: "好久不见!👋",
      body: "已经两个月没登录应用啦,快来看看吧",
      data: { type: "login_reminder" },
    },
    trigger: {
      date: twoMonthsLater, // 指定具体触发日期
      repeats: false, // 禁用重复触发
    },
  });
}

关键说明:

  • 使用date参数无需手动换算秒数,由系统精准处理时间,避免了秒数计算误差和重复触发问题
  • 每次用户登录时调用该函数,即可重新安排一个两个月后的单次提醒;如果需要避免重复创建通知,可以先调用Notifications.getAllScheduledNotificationsAsync()查询已有通知,取消旧的未触发提醒后再创建新的

内容的提问来源于stack exchange,提问作者user6377312

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 22:30:44