-1

I have a react native app and as a part of it a user can set up to be regularly notified (the notifications are just local...not pushed from the server).

The thing is, The user will choose the time and days of the week of the notifications and then I can set it up for a week or so but ideally I would want to run a cron job or something weekly to set up the notifications for that week...what would be the best way to do this?

Edit to add: The notifications will be sent at regular intervals but the content of the notifications will change over time.

Ideally I am looking for something that can run some code on the users phone every sunday and set the notifications without them having to open the app. Is there a nice way to do something like this?

I am using expo and if I can avoid ejecting for this then that would also be good.

Matthew Jonat
  • 187
  • 17

1 Answers1

1

You should have a look at the expo documentation

Here is a very simple exemple of how to handle a weekly notification triggered every sunday:

import * as Notifications from 'expo-notifications';

async function scheduleAndCancel() {
  const identifier = await Notifications.scheduleNotificationAsync({
    content: {
      title: 'Hello, it’s Sunday',
    },
    trigger: { weekday: 1, repeats: true },
  });

  // save identifier somewhere in AsyncStorage or something else to be able to cancel it.
}
devpolo
  • 2,487
  • 3
  • 12
  • 28
  • I will add this to my original question but I forgot to mention that while the timing of the notification will stay the same, the content will change every week so I will need to physically re-set it every week I think – Matthew Jonat Mar 21 '23 at 10:34
  • Sorry dude...it was not intentional. I upvoted your answer as it was helpful. – Matthew Jonat Mar 21 '23 at 10:43
  • Can you share more context "The notifications will be sent at regular intervals but the content of the notifications will change over time."? – devpolo Mar 21 '23 at 10:45
  • It's kind of like a word of the day in a notification. The user will choose the time and days that they want to receive the word of the day but the word of the day is going to be different every day... – Matthew Jonat Mar 21 '23 at 19:11