我试图了解在调用任务时如何发送通知,但我遇到了错误,我不明白为什么以及如何修复此问题。
这就是错误:
taskmanager:任务“后台提取”失败:,[typeerror:undefined不是一个函数(靠近“…notifications.presentlocalnotificationasync…”)
代码如下:
import React, { useEffect, useState } from 'react';
import { StyleSheet, Text, View, Button, StatusBar } from 'react-native';
import * as BackgroundFetch from 'expo-background-fetch';
import * as TaskManager from 'expo-task-manager';
import * as Notifications from 'expo-notifications';
const BACKGROUND_FETCH_TASK = 'background-fetch';
// 1. Define the task by providing a name and the function that should be executed
// Note: This needs to be called in the global scope (e.g outside of your React components)
TaskManager.defineTask(BACKGROUND_FETCH_TASK, async () => {
const now = Date.now();
console.log(`Got background fetch call at date: ${new Date(now).toISOString()}`);
await Notifications.presentLocalNotificationAsync({
title: 'This is crazy',
body: 'Your mind will blow after reading this',
});
// Be sure to return the successful result type!
return BackgroundFetch.Result.NewData;
});
// 2. Register the task at some point in your app by providing the same name, and some configuration options for how the background fetch should behave
// Note: This does NOT need to be in the global scope and CAN be used in your React components!
async function registerBackgroundFetchAsync() {
return BackgroundFetch.registerTaskAsync(BACKGROUND_FETCH_TASK, {
minimumInterval: 1, // 15 minutes
stopOnTerminate: false, // android only,
startOnBoot: true, // android only
delay: 1000,
periodic: true,
});
}
// 3. (Optional) Unregister tasks by specifying the task name
// This will cancel any future background fetch calls that match the given name
// Note: This does NOT need to be in the global scope and CAN be used in your React components!
async function unregisterBackgroundFetchAsync() {
return BackgroundFetch.unregisterTaskAsync(BACKGROUND_FETCH_TASK);
}
const BackgroundFetchScreen = () => {
const [isRegistered, setIsRegistered] = useState(false);
const [status, setStatus] = useState(null);
useEffect(() => {
checkStatusAsync();
}, []);
const checkStatusAsync = async () => {
const status = await BackgroundFetch.getStatusAsync();
const isRegistered = await TaskManager.isTaskRegisteredAsync(BACKGROUND_FETCH_TASK);
setStatus(status);
setIsRegistered(isRegistered);
};
const toggleFetchTask = async () => {
if (isRegistered) {
await unregisterBackgroundFetchAsync();
} else {
await registerBackgroundFetchAsync();
}
checkStatusAsync();
};
return (
<>
<View style={styles.textContainer}>
<Text>
Background fetch status:{' '}
<Text >{status ? BackgroundFetch.Status[status] : null}</Text>
</Text>
<Text>
Background fetch task name:{' '}
<Text >
{isRegistered ? BACKGROUND_FETCH_TASK : 'Not registered yet!'}
</Text>
</Text>
</View>
<View style={styles.textContainer}></View>
<Button
title={isRegistered ? 'Unregister BackgroundFetch task' : 'Register BackgroundFetch task'}
onPress={toggleFetchTask}
/>
</>
);
}
const styles = StyleSheet.create({
textContainer: {
flex: 1,
marginTop: StatusBar.currentHeight || 0,
},
});
export default BackgroundFetchScreen;
我将感谢任何帮助和信息,我可以得到。
1条答案
按热度按时间7fhtutme1#
答案在文件中https://github.com/expo/fyi/blob/master/presenting-notifications-deprecated.md
presentlocalnotificationasync已被弃用。