Search code examples
androidiosreact-nativemobileexpo

Do scheduled notifications survive the reintallation of the mobile app?


I'm making a calendar app in react native with expo. I was thinking about using Notifications.scheduleNotification to pop up a notification when the time of the events in the calendar come.

When the user resets the phone, at least in android, the notifications survive, and they receive the previously scheduled notification anyway. This prevents me from needing to put a BackgroundFetch to reschedule possibly lost notifications. Instead, I schedule them when creating the event.

But I thought: What about reinstalling? Will I lose the scheduled notifications on reinstall?

What's the case in android? And in iOS?


Solution

    • the scheduled notifications are stored within the app's data, and when the app is uninstalled, all its data is removed.

    • You can use a combination of local storage and background fetching. When the app is installed or launched, you can check the local storage for any previously scheduled notifications. If there are any, you can reschedule them using Notifications.scheduleNotification.

       import * as Notifications from 'expo-notifications';
       import AsyncStorage from '@react-native-async-storage/async-storage';
      
       async function loadScheduledNotifications() {
        const storedNotifications = await AsyncStorage.getItem('scheduledNotifications');
        if (storedNotifications) {
          const notifications = JSON.parse(storedNotifications);
          notifications.forEach(notification => {
            Notifications.scheduleNotification(notification);
          });
         }
       }
      
       async function saveScheduledNotification(notification) {
         const storedNotifications = await AsyncStorage.getItem('scheduledNotifications');
         let notifications = [];
         if (storedNotifications) {
           notifications = JSON.parse(storedNotifications);
         }
         notifications.push(notification);
         await AsyncStorage.setItem('scheduledNotifications', JSON.stringify(notifications));
       }
      
       // Usage
       Notifications.scheduleNotification(notification);
       saveScheduledNotification(notification);
      
    • We're using AsyncStorage to store and retrieve the scheduled notifications. When a notification is scheduled, we save it to AsyncStorage. When the app is launched, we load any previously scheduled notifications and reschedule them.