|
1 | | -import * as TaskManager from "expo-task-manager"; |
2 | | -import * as BackgroundFetch from "expo-background-fetch"; |
| 1 | +import * as TaskManager from 'expo-task-manager'; |
| 2 | +import * as BackgroundFetch from 'expo-background-fetch'; |
3 | 3 |
|
4 | | -const BACKGROUND_FETCH_TASK = "background-fetch-task"; |
| 4 | +const BACKGROUND_FETCH_TASK = 'background-fetch-task'; |
5 | 5 |
|
6 | 6 | // Define the task |
7 | 7 | TaskManager.defineTask(BACKGROUND_FETCH_TASK, async () => { |
8 | | - try { |
9 | | - console.log("Background fetch task started"); |
10 | | - // Perform your API request |
11 | | - const response = await fetch( |
12 | | - "https://new.codebuilder.org/api/reddit/posts" |
13 | | - ); |
14 | | - const data = await response.json(); |
| 8 | + try { |
| 9 | + console.log('----------------------- Background fetch task started ---------------------'); |
| 10 | + // Perform your API request |
| 11 | + const response = await fetch('https://new.codebuilder.org/api/jobs'); |
| 12 | + const contentType = response.headers.get('content-type') || ''; |
15 | 13 |
|
16 | | - // Handle the fetched data |
17 | | - console.log("Fetched data:", data); |
| 14 | + if (!response.ok) { |
| 15 | + const text = await response.text(); |
| 16 | + console.error('Background fetch HTTP error', response.status, text.slice(0, 200)); |
| 17 | + return BackgroundFetch.BackgroundFetchResult.Failed; |
| 18 | + } |
18 | 19 |
|
19 | | - return BackgroundFetch.BackgroundFetchResult.NewData; // Task succeeded |
20 | | - } catch (error) { |
21 | | - console.error("Background fetch failed:", error); |
22 | | - return BackgroundFetch.BackgroundFetchResult.Failed; // Task failed |
23 | | - } |
| 20 | + let data: unknown; |
| 21 | + if (contentType.includes('application/json')) { |
| 22 | + data = await response.json(); |
| 23 | + } else { |
| 24 | + const text = await response.text(); |
| 25 | + console.warn('Background fetch received non-JSON response:', text.slice(0, 200)); |
| 26 | + data = text; |
| 27 | + } |
| 28 | + |
| 29 | + // Handle the fetched data |
| 30 | + console.log('Fetched data:', data); |
| 31 | + |
| 32 | + return BackgroundFetch.BackgroundFetchResult.NewData; // Task succeeded |
| 33 | + } catch (error) { |
| 34 | + console.error('Background fetch failed:', error); |
| 35 | + return BackgroundFetch.BackgroundFetchResult.Failed; // Task failed |
| 36 | + } |
24 | 37 | }); |
25 | 38 |
|
26 | 39 | // Register the task |
27 | 40 | export async function registerBackgroundFetch() { |
28 | | - const status = await BackgroundFetch.getStatusAsync(); |
29 | | - if (status === BackgroundFetch.BackgroundFetchStatus.Available) { |
30 | | - await BackgroundFetch.registerTaskAsync(BACKGROUND_FETCH_TASK, { |
31 | | - minimumInterval: 60, // Fetch interval in seconds (not guaranteed to be exact) |
32 | | - stopOnTerminate: false, // Continue task when app is closed |
33 | | - startOnBoot: true, // Start task when device is rebooted |
34 | | - }); |
35 | | - console.log("Background fetch task registered"); |
36 | | - } else { |
37 | | - console.error("Background fetch is not available"); |
38 | | - } |
| 41 | + const status = await BackgroundFetch.getStatusAsync(); |
| 42 | + if (status === BackgroundFetch.BackgroundFetchStatus.Available) { |
| 43 | + await BackgroundFetch.registerTaskAsync(BACKGROUND_FETCH_TASK, { |
| 44 | + minimumInterval: 60, // Fetch interval in seconds (not guaranteed to be exact) |
| 45 | + stopOnTerminate: false, // Continue task when app is closed |
| 46 | + startOnBoot: true, // Start task when device is rebooted |
| 47 | + }); |
| 48 | + console.log('Background fetch task registered'); |
| 49 | + } else { |
| 50 | + console.error('Background fetch is not available'); |
| 51 | + } |
39 | 52 | } |
0 commit comments