jamesisaac / react-native-background-task

Periodic background tasks for React Native apps, cross-platform (iOS and Android), which run even when the app is closed.
MIT License
1.1k stars 110 forks source link

How can I call a function inside background task? #33

Closed kivancguncavdi closed 6 years ago

kivancguncavdi commented 6 years ago

Hi all! I'm new in React Native and I'm working on building a forecast application. Right now, I can fetch some data from a forecast API when the app is in background. It works fine. On the other hand, I'm also using react-native-push-notification in the app. All I want to do is to create a local notification whenever a new data fetched in the background and set the notficiation's message property as the fetched data. To do this, I'm calling PushNotification.configure and PushNotification.localNotification functions inside BackgroundTask.define but it's not working. I need to make a connection between these two but I have no idea how to do.

Can anybody help me or give me some advices?

kivancguncavdi commented 6 years ago

I just solved the problem by using the following code right above the component in index.js file:

const clearFunc = () =>{
  PushNotification.cancelAllLocalNotifications();
}

const notif = (msg) => {
    PushNotification.localNotification({
    //title: notification['gcm.notification.title'],
    message: msg// (required)
    //subText: notification['gcm.notification.subText'],
    //tag: notification['gcm.notification.tag'],
    //group: notification['gcm.notification.group'],
  });
}

BackgroundTask.define(async () => {
  clearFunc();
  notif("Hello World!");
  BackgroundTask.finish();
});