skip to Main Content

I am building a react native Android/IOS app using Expo and I need to have a scheduled notification come in and run some code regardless of whether the app is in the foreground or background.

I currently have this background task defined with Expo:

TaskManager.defineTask(BACKGROUND_NOTIFICATION_TASK, ({data, error, executionInfo}) => {
    console.log('Received a notification in the background!');
    Notifications.scheduleNotificationAsync({
        content: {
            title: "HOORAY",
            body: 'HOORAY',
            data: {type: 'pre'}
        },
        trigger: {
            seconds: 10,
        },
    });
});

Notifications.registerTaskAsync(BACKGROUND_NOTIFICATION_TASK);

Which is all done in the global scope as per the docs

I have tried to use the Notifications.scheduleNotificationAsync (shown below) and tried sending a push notification from the Firebase dashboard and I receive them fine but the background notification task doesn’t run regardless of whether the app is in the foreground or background.

Notifications.scheduleNotificationAsync({
  content: {
    title: "Example title",
    body: 'Example body',
    data: {test: 'true'}
  },
  trigger: {
    seconds: 10,
  },
});

It’s also worth noting that the task is being registered, TaskManager.getRegisteredTasksAsync() gives me this [{"options": {}, "taskName": "BACKGROUND-NOTIFICATION-TASK", "taskType": "remote-notification"}]

From what I understand from the docs the background notification task I have set in TaskManager should run when a notification comes in, regardless of whether the app is in the foreground or background as per the docs

I am running an Expo (version 51.0.10) development build on a real Android device (Pixel 7 Pro).

Where am I going wrong?

2

Answers


  1. According to the issue below, the background task only works for data-only notifications.

    https://github.com/expo/expo/issues/29622

    Login or Signup to reply.
  2. When I run the same code, it generates this error below even with the updated dependencies, directly correct, reinstallation of the node_modules files

    cannot read property 'scheduleNotificationAsync' of undefined
    

    My code

    import * as TaskManager from "expo-task-manager";
    import Notifications from "expo-notifications"; 
    
    const BACKGROUND_SYNC_TASK = "background-sync-task";
    const NOTIFICATION_CATEGORY_IDENTIFIER = "notification-sync-task";
    
    TaskManager.defineTask(BACKGROUND_SYNC_TASK, async () => {
        try {
            await Notifications.scheduleNotificationAsync({
                content: {
                    title: "Tarefa em segundo plano",
                    body: "Sua tarefa em segundo plano está rodando!",
                    sound: "default",
                },
                trigger: {
                    seconds: 1,
                    channelId: NOTIFICATION_CATEGORY_IDENTIFIER
                }
            });
    
            await new Promise(resolve => setTimeout(resolve, 1000 * 10)); // 10 segundos
    
            return BackgroundFetch.BackgroundFetchResult.NewData;
        } catch (error) {
            console.error("Error:", error);
    
            return BackgroundFetch.BackgroundFetchResult.Failed;
        }
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search