Short answer:
Need to use some resource outside the lib used. I couldn’t find a way to use only lib.
Long response (accumulated):
One possibility is to use another lib [1], but this solution has very limited options: the range should be configured with a 'String' option with few options provided.
The best will be to use the lib you have, but connect the start time to the external clock as [2] or [3].
The method [2] uses a C-object solution to give the time to display the message, the method [3] has an external lib to do the same.
The important parts of the solution [3] that I consider most useful of the three options:
import BackgroundFetch from 'react-native-background-fetch';
// ao iniciar o programa inicia as notificações
PushNotification.configure(....);
// definindo o tempo a execução.
BackgroundFetch.configure(
{
minimumFetchInterval: 15, // minutos para esperar cada ciclo
},
async taskId => {
....
PushNotification.localNotification({
title: 'Título',
message: `Mensagem longa.`,
playSound: true,
soundName: 'default',
});
// parar a execução.
BackgroundFetch.finish(taskId);
With this method you can set any range for notification.
[1] https://github.com/react-native-community/push-notification-ios
[2] https://stackoverflow.com/questions/37155782/configure-repeatinterval-for-local-notifications-in-ios-in-react-native
[3] https://medium.com/@alihaghani/background-tasks-and-local-push-Notifications-with-React-Native-d63fc7fff9b
correction:
You can use this lib to set times for events and send the local notification as above.
This should make repeatable calls on IOS and Android:
BackgroundTimer.runBackgroundTimer(() => {
// notificação local
},
timeMillis); // calcule aqui o tempo em milissegundos
(one day: 24 * 60 * 60 * 1000 = 86400000 msec)
One more option:
You can trigger the event with the "every day" setting and have a variable saved in your phone’s local memory that tells you how many days ago you sent the last notification. So it can be every 2 or 3 days. Any amount of days actually.
Use of "localstorage" in React Native (in index.js
):
import PushNotification from 'react-native-push-notification';
import SyncStorage from 'sync-storage';
...
// inicializar
SyncStorage.set('quantidadeDias', 0);
...
const QUANTIDADE_DIAS_NECESSARIA = 1; // ou 2,3,4...
class NotificationHandler {
onNotification(notification) {
if (Platform.OS === 'ios') {
const diaAgora = SyncStorage.get('quantidadeDias') + 1;
if (diaAgora == QUANTIDADE_DIAS_NECESSARIA) {
SyncStorage.set('quantidadeDias', 0);
// padrão
notification.finish(PushNotificationIOS.FetchResult.NoData);
} else {
SyncStorage.set('quantidadeDias', diaAgora);
// não toque na notificação
}
}
}
The syntax of the code in Notificationhandler has changed over time, see [4] and [5].
Resources:
[4] https://github.com/zo0r/react-native-push-notification/issues/701
[5] https://github.com/zo0r/react-native-push-notification/issues/1453
After the notification is triggered, it is shown independent of calling notification.Finish or not
– Vynstus
and Backgroundtimer solution also does not work?
– mico