Please provide the following:
- SDK Version: v35.0.0
- Platforms(Android/iOS/web/all): iOS and Android
Hello, could someone help me around TaskManager. I want to schedule background fetch to update notifications since api that I’m using currently does not support push notifications and I need to find a workaround.
Error that I get is:
Task ‘setNotifications’ is not defined. You must define a task using TaskManager.defineTask before registering.
- node_modules/expo-background-fetch/build/BackgroundFetch.js:33:14 in registerTaskAsync$
- node_modules/regenerator-runtime/runtime.js:45:39 in tryCatch
- … 9 more stack frames from framework internals
import React from 'react';
import * as Sentry from 'sentry-expo';
import { Notifications } from 'expo';
import * as Permissions from 'expo-permissions';
import * as SecureStore from 'expo-secure-store';
import * as BackgroundFetch from 'expo-background-fetch';
import * as TaskManager from 'expo-task-manager';
import { LocaleConfig } from 'react-native-calendars';
import axios from 'axios';
import getEnvVars from './environment';
import AppScreen from './src/screens/AppScreen';
const TASK_NAME = 'setNotifications';
TaskManager.defineTask(TASK_NAME, async () => {
const currentClinic = await SecureStore.getItemAsync('currentClinic').then(response => JSON.parse(response));
const userToken = await currentClinic.auth;
const notification = await axios.post(`${apiUrl}/connects/notification`, `token=${userToken}`)
.then((response) => {
if (response.data.error_code === 9003) {
console.log(response);
} else if (response.data.data.notifications !== null) {
return response.data.data;
}
console.log(response);
return response;
})
.catch((error) => {
console.log(error);
});
if (notification !== null) {
console.log(notification);
const notificationId = Notifications.scheduleLocalNotificationAsync(
{
title: "I'm Scheduled",
body: 'Wow, I can show up even when app is closed',
},
{
repeat: false,
time: new Date().getTime() + 10000,
},
);
console.log(notificationId);
}
return notification ? BackgroundFetch.Result.NewData : BackgroundFetch.Result.NoData;
});
TaskManager.defineTask(TASK_NAME, async () => {
console.log('radi ili ne');
});
const { apiUrl } = getEnvVars();
Sentry.init({
dsn: '<SOMELINK>',
enableInExpoDevelopment: true,
debug: true,
});
export default class App extends React.Component {
componentDidMount() {
initializeBackgroundFetch();
}
render() {
return (
<ErrorBoundary>
<AppScreen />
</ErrorBoundary>
);
}
}
async function initializeBackgroundFetch() {
try {
await Permissions.askAsync(Permissions.NOTIFICATIONS);
await BackgroundFetch.registerTaskAsync(TASK_NAME, {
minimumInterval: 15,
});
const isRegistered = await TaskManager.isTaskRegisteredAsync(TASK_NAME);
console.log(isRegistered);
if (isRegistered) {
console.log(isRegistered);
}
} catch (error) {
console.log(error);
}
}
class ErrorBoundary extends React.Component {
componentDidCatch(error, errorInfo) {
Sentry.captureException(error, {
additionalInfo: errorInfo,
});
}
render() {
const { children } = this.props;
return children;
}
}