Local Push Notification iOS

Asked

Viewed 635 times

5

I’m using the lib React-Native-push-notification to schedule notifications in an app,.

For Android I am able to create, but the same does not work for iOS. I wonder if you have any alternative to develop this for iOS.

Code that works on Android:

PushNotification.localNotificationSchedule({
      title: name,
      message: message,
      date: new Date(),
      repeatType: 'time', // (Apenas Android)
      repeatTime: moment.duration(2, 'days').asMilliseconds() // (Apenas Android) Repete a cada 2 dias
    });

3 answers

0

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

  • and Backgroundtimer solution also does not work?

0

There is a separate version for iOS

To be able to use it according to the official documentation

You need to use this pacote:

yarn add @react-native-community/push-notification-ios

So, envés de utilizar PushNotification, you will use: PushNotificationIOS

To know how to work better with PushNotificationIOS, access their official documentation on github

The code would look like this:

PushNotificationIOS.scheduleLocalNotification({
      title: name,
      message: message,
      date: new Date(),
      repeatInterval: new Date(Date.now() + 172800 * 1000) // isso faz ele ter um tempo de repetição de dois em dois dias, cada dia tem 86400 segundos, dois dias tem 172800, mude esse valor para a quantidade de dias que você precisaria
    });

(OBS: for iOS, envés de localNotificationSchedule, is scheduleLocalNotification)

Specific link on the scheduleLocalNotification() for iOS

  • I tried that, but there are no repeatType and repeatTime properties in this iOS lib. The repeating property that the doc specifies is this only: repeatInterval?: 'minute' | 'hour' | 'day' | 'week' | 'Month' | 'year';

  • when you put repeatInterval: 'day', what happens?

  • repeat once every day (24 hours), the problem is repeat every 2, 3, 4 days

  • Okay, I’ll see to that now!

  • I gave a better study on the subject and managed to get this result, it worked? I edited the answer putting it.

  • Gave this error: repeatInterval should be one of: ( day, hour, minute, Month, week, year )"

Show 2 more comments

-3

There is a separate version for iOS

To get you to use it according to official documentation

You need to use this package:

Yarn add @React-Native-community/push-notification-Ios So, after using Pushnotification, you will use: Pushnotificationios

To learn how to work better with Pushnotificationios, access their official documentation on github

  • Yes, but the iOS lib has limitations to repeat the notification for a certain time interval, which is the case with this question, if there is any alternative to implement this interval repetition

  • I can understand why I keep giving negative vote for everything that does not work for Oce ? just leave no vote and vote up on what is right

  • Your answer seems to be a copy of one of the answers already presented to that question. I believe that must be why they gave you down vote. I won’t give it to you, but I’m clarifying what it might be. Hugs!

Browser other questions tagged

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