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

React Native Expo应用中如何设置本地通知的自定义启动时间与自定义重复间隔

Configure Custom Initial Trigger Date + Repeat Interval for Expo Notifications

Great question! You absolutely can set both a custom first notification trigger date and a custom repeat interval in Expo Notifications. The trick is combining the date property (for your initial delay) with the seconds and repeats properties (for your repeat schedule) in the trigger configuration. This works reliably on both Android and iOS.

Here's the modified code to achieve your example (15-day initial delay, 27-day repeat):

import * as Notifications from "expo-notifications";

/**
 * Generates a notification config with custom initial delay and repeat interval
 * @param {number} initialDelayDays - Days until the first notification fires
 * @param {number} repeatIntervalDays - Days between repeated notifications
 */
export default function notificationsConfig(initialDelayDays, repeatIntervalDays) {
  // Calculate the exact date for the first notification
  const firstTriggerDate = new Date();
  firstTriggerDate.setDate(firstTriggerDate.getDate() + initialDelayDays);

  // Convert the repeat interval from days to seconds
  const repeatIntervalSeconds = repeatIntervalDays * 24 * 60 * 60;

  const config = {
    content: {
      title: "Reminder",
      body: "Your custom reminder message here.",
      sound: false,
      priority: Notifications.AndroidNotificationPriority.HIGH,
      color: "blue",
    },
    trigger: {
      date: firstTriggerDate, // First notification fires at this specific date
      seconds: repeatIntervalSeconds, // Repeat every X seconds after the first trigger
      repeats: true,
    },
  };
  return config;
}

Key Details to Note:

  • date property: This sets the precise timestamp for the first notification to trigger. In the example, we add your initial delay (15 days) to the current date.
  • seconds + repeats: true: The seconds value defines how often the notification repeats after the first trigger. For your 27-day interval, we convert days to seconds to get an exact custom interval.
  • Platform Compatibility: Expo handles the platform-specific scheduling logic behind the scenes, so this setup works for both Android and iOS.

Quick Additional Tips:

  • On Android 12+, you'll need to request the Notifications.Permissions.ANDROID.SCHEDULE_EXACT_ALARM permission to use exact scheduled notifications.
  • For iOS, using seconds for custom intervals (like 27 days) is better than predefined intervals (like Notifications.IntervalDay) since your repeat isn't a standard unit.
  • Always make sure your app has been granted notification permissions before scheduling any notifications.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 08:52:34