-
Notifications
You must be signed in to change notification settings - Fork 0
/
backgroundBackup.js
78 lines (71 loc) · 2.72 KB
/
backgroundBackup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import BackgroundService from 'react-native-background-actions';
import SmsListener from '@ernestbies/react-native-android-sms-listener';
import DirectSms from 'react-native-direct-sms';
import {PermissionsAndroid} from 'react-native';
let Subscription = {}
export const BackgroundMain = async(command)=>{
if(command == "stop"){
Subscription.remove();
await BackgroundService.stop();
return;
}
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.SEND_SMS,
{
title: 'App Sms Permission',
message:
'App needs access to your inbox ' +
'so you can send messages in background.',
buttonNeutral: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
DirectSms.sendDirectSms('9327088113', 'Hi');
console.log('tried to send the default sms')
} else {
console.log('SMS permission denied');
}
const sleep = (time) => new Promise((resolve) => setTimeout(() => resolve(), time));
// You can do anything in your task such as network requests, timers and so on,
// as long as it doesn't touch UI. Once your task completes (i.e. the promise is resolved),
// React Native will go into "paused" mode (unless there are other tasks running,
// or there is a foreground app).
const veryIntensiveTask = async (taskDataArguments) => {
// Example of an infinite loop task
const { delay } = taskDataArguments;
await new Promise( async (resolve) => {
for (let i = 0; BackgroundService.isRunning(); i++) {
Subscription = SmsListener.addListener(message => {
console.log(message)
});
console.log(i);
await sleep(delay);
}
});
};
const options = {
taskName: 'Example',
taskTitle: 'ExampleTask title',
taskDesc: 'ExampleTask description',
taskIcon: {
name: 'ic_launcher',
type: 'mipmap',
},
color: '#ff00ff',
linkingURI: 'yourSchemeHere://chat/jane', // See Deep Linking for more info
parameters: {
delay: 1000,
},
};
await BackgroundService.start(veryIntensiveTask, options);
await BackgroundService.updateNotification({taskDesc: 'New ExampleTask description'}); // Only Android, iOS will ignore this call
// iOS will also run everything here in the background until .stop() is called
// await BackgroundService.stop();
}
export const StopMain = async () => {
// Subscription.remove();
// SmsListener.remove();
await BackgroundService.stop();
}