-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (75 loc) · 2.43 KB
/
index.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
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* @format
*/
import {AppRegistry, Text, TextInput} from 'react-native';
import App from './App';
import {name as appName} from './app.json';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { DEV, API_URL_DEVELOPMENT, API_URL_PRODUCTION } from '@env'
import io from 'socket.io-client'
import notifee, { AndroidImportance } from '@notifee/react-native'
if (Text.defaultProps == null) {
Text.defaultProps = Text.defaultProps || {};
Text.defaultProps.allowFontScaling = false;
}
if (TextInput.defaultProps == null) {
TextInput.defaultProps = {};
TextInput.defaultProps.allowFontScaling = false;
TextInput.defaultProps.autoComplete = "off";
}
const socket = io(`${ DEV === 'yes' ? API_URL_DEVELOPMENT : API_URL_PRODUCTION }`)
socket.on('connect', () => {
console.log('Connected to socket.io server')
})
socket.on('automated_notification', async (data) => {
console.log('Received automated notification:', data)
// Request permissions (required for iOS)
await notifee.requestPermission()
// Create a channel (required for Android)
const channelId = await notifee.createChannel({
id: data.id,
name: 'FarmFriend Push Notification',
importance: AndroidImportance.HIGH,
})
// Display a notification
await notifee.displayNotification({
title: data.title,
body: data.message,
android: {
smallIcon: 'ic_launcher',
channelId,
// pressAction is needed if you want the notification to open the app when pressed
pressAction: {
id: 'default',
}
},
})
})
socket.on('new_notification', async (data) => {
console.log('Received new notification:', data)
// Request permissions (required for iOS)
await notifee.requestPermission()
// Create a channel (required for Android)
const channelId = await notifee.createChannel({
id: data.id,
name: 'FarmFriend Push Notification',
importance: AndroidImportance.HIGH,
})
const account_type = await AsyncStorage.getItem('ACCOUNT_TYPE')
if (data.notification_to === account_type) {
// Display a notification
await notifee.displayNotification({
title: data.title,
body: data.message,
android: {
smallIcon: 'ic_launcher',
channelId,
// pressAction is needed if you want the notification to open the app when pressed
pressAction: {
id: 'default',
}
},
})
}
})
AppRegistry.registerComponent(appName, () => App);