React Native - Schedule Local Notifications

Asked

Viewed 899 times

5

I’m using the library React Native Push Notifications to schedule notifications, but I’m facing a problem I don’t know if it’s possible to resolve with this lib.

I have to schedule a notification that repeats every week on specific days of the week.

On github this code is made available to schedule notifications:

PushNotification.localNotificationSchedule({
  //... You can use all the options from localNotifications
  message: "My Notification Message", // (required)
  date: new Date(Date.now() + (60 * 1000)) // in 60 secs
});

And this section tells you how to repeat notifications:

Repeating Notifications (optional): Specify repeatType and Optionally repeatTime while Scheduling the local notification. Check the local notification example above. Property repeatType could be one of week, day, hour, minute, time. If specified as time, it should be accompanied by one more Parameter repeatTime which should the number of milliseconds between each interval.

But I don’t know how to use this to repeat only on specific days of the week, as only on Tuesday and Wednesday.

Someone would know how to do that?

  • I have an idea but I do not know if it works and I have no way to test... In function PushNotification.configure has the property onNotification which is a function that receives the notification, I think it is possible to create a new one in that callback according to the current date, ie you schedule a notification, when it is created, is scheduled the next and so on. But it’s just an idea I don’t know if it’ll work

1 answer

6


Creates a notification for each day:

// toda terca a partir de hoje, essa hora
d = new Date();
d.setDate(d.getDate() + (2 + 7 - d.getDay()) % 7);
PushNotification.localNotificationSchedule({
  //... You can use all the options from localNotifications
  message: "My Notification Message", // (required)
  date: d,
  repeatType: 'week',  
});

// toda quarta a partir de hoje, essa hora
d2 = new Date();
d2.setDate(d2.getDate() + (3 + 7 - d2.getDay()) % 7);
PushNotification.localNotificationSchedule({
  //... You can use all the options from localNotifications
  message: "My Notification Message", // (required)
  date: d2,
  repeatType: 'week',  
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.