invertase / notifee

⚛️ A feature rich notifications library for React Native.
https://notifee.app
Apache License 2.0
1.88k stars 228 forks source link

Configuring repeating notifications with a custom interval #1051

Closed jeespu closed 4 months ago

jeespu commented 5 months ago

I need a functionality for an app I'm working on, where the user can set a reminder for time X and it can be configured to repeat every N {timeUnit}, so for example every 3 hours. I've been testing and trying stuff for a few days now without any luck to get it reliably working.

So far I've managed to get the events to trigger when I initially create a timestamp notification, and add a onBackgroundEvent handler that will schedule a new trigger notification. In the event handler, I filter out "DELIVERED" events and once the notification is delivered, I schedule another one.

My problems are, the event handler seems to run multiple times and always triggers events 3, 7, 3, 7, 3, 7 (3 being DELIVERED and 7 being TRIGGER_NOTIFICATION_CREATED or something). In addition to this, the newly scheduled "follow-up notification" always seems to schedule a new notification as it should, but it never fires, even when the app is in the foreground.

Using the interval trigger alone is not sufficient for the app because it doesn't allow setting a custom start time, but neither is using a timestamp trigger's RepeatFrequency, because it doesn't allow custom intervals. The app already had a functionality like this that uses react-native-push-notification, but the library is sadly very outdated and we'd like to replace it with a newer one that is maintained.

notifee.onBackgroundEvent(handleEvent);

async function handleEvent(e: Event) {
  if (e.type !== EventType.DELIVERED) {
    return;
  }
  const notificationParams = e.detail.notification?.data as NotificationParams;

  if (notificationParams.repeatInterval) {
    const {repeatInterval, title, date} = notificationParams;

    // Create an interval notification that repeats with a custom interval
    let nextNotificationDate = date;

    if (nextNotificationDate < Date.now()) {
      do {
        nextNotificationDate += repeatInterval.every * repeatInterval.unit;
      } while (nextNotificationDate < Date.now());
    }

    await notifee.createTriggerNotification(
      {
        title,
        data: {...notificationParams, date: nextNotificationDate},
      },
      {
        timestamp: nextNotificationDate,
        type: TriggerType.TIMESTAMP,
      },
    );
  }
}

@helenaford any insight on this? Also, does iOS send "DELIVERED" events yet? Is there any reliable workaround for the case I described?

EDIT: I noticed I had forgot to set channelId for the new notification and the notification now popped. I also added a 2 second timeout for the event handler and the "looping" stopped. Hacky, but works. The question remains: will this work for iOS?

github-actions[bot] commented 4 months ago

Hello 👋, to help manage issues we automatically close stale issues.

This issue has been automatically marked as stale because it has not had activity for quite some time.Has this issue been fixed, or does it still require attention?

This issue will be closed in 15 days if no further activity occurs.

Thank you for your contributions.