cordova-plugin-background-fetch icon indicating copy to clipboard operation
cordova-plugin-background-fetch copied to clipboard

Background Fetch not post data on my server

Open codingwithashu opened this issue 4 months ago • 0 comments

Your Environment

  • Plugin version: 7.2.4
  • Platform: Android
  • OS version:
  • Device manufacturer / model: Moto g24
  • Cordova version (cordova -v): 12.0.0
  • Cordova platform version (cordova platform ls):
  • Plugin config

Expected Behavior

Background Fetch not post data on my server

Context

Posting data to my server

async onDeviceReady(): Promise<void> {
  const URL = `https://webhook.site/88ffb4bc-a20f-49a5-b5a0-8b984848daec`;

  const onEvent = async (taskId: string): Promise<void> => {
    console.log('[BackgroundFetch] event received: ', taskId);

    try {
      // Perform the background network check (you might want to handle the result)
      await this.networkInfoService.checkLocationStatus(URL, objData);
      BackgroundFetch.finish(taskId);
    } catch (error) {
      console.error(`[BackgroundFetch] Error during background task: ${error}`);
      BackgroundFetch.finish(taskId); 
    }
  };

  // Timeout callback is executed when the task has exceeded the allowed running time
  const onTimeout = async (taskId: string): Promise<void> => {
    console.log('[BackgroundFetch] TIMEOUT: ', taskId);
    // Ensure the task is marked as finished
    BackgroundFetch.finish(taskId);
  };

  // Configure the BackgroundFetch plugin
  try {
    const status = await BackgroundFetch.configure(
      {
        minimumFetchInterval: 15, // Minimum interval in minutes
        startOnBoot: true,        // Start background fetch tasks when device boots
        stopOnTerminate: false    // Continue fetch tasks even when the app is terminated
      },
      onEvent,
      onTimeout
    );
    // Show the configuration status message
    this.apiService.ShowMessage(`[BackgroundFetch] configured successfully, status: ${status}`);
  } catch (error) {
    // Handle errors during configuration
    this.apiService.ShowMessage(`[BackgroundFetch] configuration failed: ${error}`);
  }
}


 async checkLocationStatus(URL: string, objESSEmployee: ESSEmployee) {
    this.platform.ready().then(() => {
      if (this.platform.is('android')) {
        cordova.plugins.LocationPermissionPlugin.checkStatus(
          async (result: any) => {

            const EventName = 'Mob-Status';
            const state = JSON.stringify(result);
            const jsonData = {
              EmployeeId: objESSEmployee.EmployeeId,
              CloudAccountName: objESSEmployee.ApiAccountName,
              EventName: EventName,
              EventValue: state,
              UpdatedOn: new Date().toISOString() 
            };


            try {
              const response = await fetch(URL, {
                method: 'POST',
                headers: {
                  'Content-Type': 'application/json',
                },
                body: JSON.stringify(jsonData)
              });

              const data = await response.json();
              console.log('Data posted successfully:', data);
            } catch (error) {
              console.error('Error posting data:', error);
            } 
          },
          async (error: any) => {
            console.log(error);
          }
        );
      } else {
        console.log('Not Supported', 'This feature is only available on Android devices.');
      }
    });
  }
  

-->

codingwithashu avatar Oct 21 '24 17:10 codingwithashu