vikeri / react-native-background-job

Schedule background jobs in React Native that run your JavaScript when your app is in the background/killed.
MIT License
747 stars 109 forks source link

Job not triggered #169

Open luizgallas opened 4 years ago

luizgallas commented 4 years ago

What am I doing wrong? This is not inside a component. I want to push a notification everytime the interval set by the user is reached.

export default function scheduleRoutine(routine) {
  const message = routine.message === '' ? 'Lembrete automático' : routine.message;

  BackgroundJob.register({
    jobKey: routine.cod.toString(),
    job: () => {
      if (validateSchedule(routine)) {
        PushNotification.localNotification({
          title: `Sua rotina: ${routine.name}`,
          message: message,
          vibration: 900,
          invokeApp: true,
          date: new Date(Date.now() + routine.interval * 60000),
          id: JSON.stringify(routine.cod),
          userInfo: {id: JSON.stringify(routine.cod)},
        });
        cancelNotification(routine);
      };
    }
  });
  console.log('Registered Job: ', routine.cod);

  BackgroundJob.schedule({
    jobKey: routine.cod.toString(),
    period: routine.interval * 60000,
    exact: true,
    allowWhileIdle: true,
  });
}
prajna-h commented 4 years ago

Yeah, job is not triggering. I have done all the required setup.

import BackgroundJob from "react-native-background-job";

const regularJobKey = "regularJobKey";

BackgroundJob.register({
    jobKey: regularJobKey,
    job: () => console.log(`Background Job fired!. Key = ${regularJobKey}`)
});

export default class Test extends Component {
   state={
      loading: false
   }

   componentDidMount(){
      BackgroundJob.schedule({
            jobKey: regularJobKey,
            notificationTitle: "Notification title",
            notificationText: "Notification text",
            period: 15000
        });
   }

   render(){
       return(<></>);
   }
}
atosh502 commented 3 years ago

Can you try adding a foreground service permission to the AndroidManifest.xml? https://developer.android.com/guide/components/foreground-services#request-foreground-service-permissions

parmarkamlesh commented 3 years ago

@prajna-h Check period key of object min interval should be 15min

  var backgroundSchedule = {
        jobKey: "myJob",
        exact:true,
        allowWhileIdle :true,
        notificationText :"test",
        notificationTitle :'titel',
        period:900000

    }

    BackgroundJob.schedule(backgroundSchedule)
        .then(() => console.log("Success"))
        .catch(err => console.err(err));