Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Background Fetch not post data on my server #170

Open
codingwithashu opened this issue Oct 21, 2024 · 0 comments
Open

Background Fetch not post data on my server #170

codingwithashu opened this issue Oct 21, 2024 · 0 comments

Comments

@codingwithashu
Copy link

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.');
      }
    });
  }
  

-->

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant