微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

背景提取在独立应用程序中不起作用

如何解决背景提取在独立应用程序中不起作用

我已经解决了在 Expo-GO 中完美运行的后台功能,但是在传输 apk 时,它在独立应用程序中根本不工作

功能正在执行,假设按照之前的预定时间在特定时间向用户发送本地通知

如果有人可以的话,需要一些帮助

import React,{ useEffect,useState } from 'react';
import {
    View,Text,StyleSheet
} from 'react-native';
import * as BackgroundFetch from 'expo-background-fetch';
import * as TaskManager from 'expo-task-manager';
import * as Notifications from 'expo-notifications';
import { data } from '../helpers/timeData';




const ALARM_TASK_FETCH = 'alarm_task_fetch';

TaskManager.defineTask(ALARM_TASK_FETCH,() => {
    Notifications.cancelAllSchedulednotificationsAsync()
    Notifications.scheduleNotificationAsync({
        content: {
          title: data.timeTitle
        },trigger: {
          seconds: data.seconds,repeats: false,},});

      console.log('Done')
    return BackgroundFetch.Result.NewData;
})


const HomeScreen = props => {


    console.log(data.nextSlot)
    async function registerBackgroundFetchAsync() {
        return BackgroundFetch.registerTaskAsync(ALARM_TASK_FETCH,{
          minimumInterval: 1,// 15 minutes
          stopOnTerminate: false,// android only,startOnBoot: true,// android only
        });
      }

    useEffect(() => {
       registerBackgroundFetchAsync()
    },[])

    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(ALARM_TASK_FETCH);
      setStatus(status);
      setIsRegistered(isRegistered);
      console.log(status,isRegistered)
    };

    return <View style = {styles.container}>
        <Text style = {styles.text}>
            Welcome to Home Screen
        </Text>
        <Text style = {styles.data}>
            Next alarm in <Text style = {{color : 'red',fontStyle : 'italic'}}>
            {data.nextSlot}
            </Text>
        </Text>
        <Text style = {styles.data}>
        with title of <Text style = {{color : 'red',fontStyle : 'italic'}}>
        {data.timeTitle}
            </Text> 
        </Text>
    </View>
}


const styles = StyleSheet.create({
    container : {
        flex : 1,backgroundColor : '#e8e8e8'
    },text : {
        textAlign : 'center',color : 'navy',fontSize : 22,fontStyle : 'italic'
    },data : {
        color : 'navy',textAlign : 'center',marginTop : 15
    }
});


export default HomeScreen;

这是我的 app.json 文件

{
  "expo": {
    "name": "FinalZakker","slug": "FinalZakker","version": "1.0.0","orientation": "portrait","icon": "./assets/icon.png","splash": {
      "image": "./assets/splash.png","resizeMode": "contain","backgroundColor": "#ffffff"
    },"plugins": [
      [
        "expo-notifications",{
          "icon": "./local/path/to/myNotificationIcon.png","color": "#ffffff","sounds": [
            "./local/path/to/mySound.wav","./local/path/to/myOtherSound.wav"
          ],"mode": "production"
        }
      ]
    ],"updates": {
      "fallbackToCacheTimeout": 0
    },"assetBundlePatterns": [
      "**/*"
    ],"ios": {
      "supportsTablet": true,"infoPlist": {
        "uibackgroundmodes": [
          "location","fetch","remote-notification",{
            "content-available": 1
          }
        ]
      }
    },"android": {
      "adaptiveIcon": {
        "foregroundImage": "./assets/adaptive-icon.png","backgroundColor": "#FFFFFF"
      },"permissions": [
        "CAMERA","ACCESS_FINE_LOCATION","ACCESS_BACKGROUND_LOCATION"
      ],"package": "com.hatim85f.FinalZakker"
    },"web": {
      "favicon": "./assets/favicon.png"
    }
  }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。