首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >世博会通知( '...Notifications.presentLocalNotificationAsync...')] TaskManager ) [TypeError:未定义“不是一个函数”(靠近TypeError)

世博会通知( '...Notifications.presentLocalNotificationAsync...')] TaskManager ) [TypeError:未定义“不是一个函数”(靠近TypeError)
EN

Stack Overflow用户
提问于 2021-07-09 12:00:55
回答 1查看 737关注 0票数 0

当任务被调用时,我试图了解如何发送通知,但是我得到了错误,我不明白为什么以及如何修复这个错误。

这是一个错误:

'...Notifications.presentLocalNotificationAsync...') TaskManager:

TaskManager:任务“背景-提取”失败:,TypeError: undefined不是一个函数(接近TaskManager)

以下是代码:

代码语言:javascript
复制
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;

我将感谢任何我能得到的帮助和信息。

EN

回答 1

Stack Overflow用户

发布于 2021-07-09 14:08:33

答案在文档https://github.com/expo/fyi/blob/master/presenting-notifications-deprecated.md

presentLocalNotificationAsync已经被否决了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68316418

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档