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 111 forks source link

Alert or Toast does not work in background task #54

Open VenkataPakalapati opened 6 years ago

VenkataPakalapati commented 6 years ago

The background task is running but not displaying the alert or toast notifications

BackgroundTask.define(async () => { ToastAndroid.show('Task started !', ToastAndroid.SHORT); Alert.alert('Restricted', 'Hello from background task'); console.log("running background task");

console.log("completed background task --- "+new Date());

ToastAndroid.show('Task completed!', ToastAndroid.SHORT);

BackgroundTask.finish();

})

rishiankush commented 5 years ago

@VenkataPakalapati I am facing the same issue. did you find any solution to this issue?

nicatasa commented 5 years ago

Hi. BackgroundTask only working background and when app is closed . Alert , Toast, Settimeout, setinterval etc. is working only app is foreground . They are not running in background.

Forexample you may be use Pushnotifications or alarmanager or another modules

`BackgroundTask.define(() => { PushNotification.localNotification({ id: '159', bigText: "My big text that will be shown when notification is expanded", // (optional) default: "message" prop vibrate: true, // (optional) default: true vibration: 300, // vibration length in milliseconds, ignored if vibrate=false, default: 1000 priority: "high", // (optional) set notification priority, default: high visibility: "private", // (optional) set notification visibility, default: private importance: "high", // (optional) set notification importance, default: high title: "My Notification Title", // (optional) message: "My Notification Message", // (required) playSound: false, // (optional) default: true soundName: 'default', // (optional) Sound to play when the notification is shown. Value of 'default' plays the default sound. It can be set to a custom sound such as 'android.resource://com.xyz/raw/my_sound'. It will look for the 'my_sound' audio file in 'res/raw' directory and play it. default: 'default' (default sound is played)

}); PushNotification.cancelLocalNotifications({ id: '159' }); BackgroundTask.finish(); });`

Note: PushNotification is module first install it with yarn or npm install