-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathHomeScreen.tsx
327 lines (295 loc) Β· 9.8 KB
/
HomeScreen.tsx
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* Copyright Airship and Contributors */
/**
* Sample React Native App
*
* HomeScreen: Contains elements for enabling push, displaying the channel ID, and for setting named user and tags.
*/
import React, { useState, useEffect, useCallback } from 'react';
import uuid from 'react-native-uuid';
import {
View,
Text,
Image,
KeyboardAvoidingView,
Platform,
TouchableOpacity,
Button,
} from 'react-native';
import Airship, {
EventType,
AirshipEmbeddedView,
} from '@ua/react-native-airship';
import styles from '../Styles';
import NamedUserManagerCell from './Home Elements/NamedUserManagerCell';
import TagManagerCell from './Home Elements/TagManagerCell';
import ChannelCell from './Home Elements/ChannelCell';
const EnablePushCell: React.FC<{
notificationsEnabled: boolean;
handleNotificationsEnabled: (value: boolean) => void;
}> = ({ notificationsEnabled, handleNotificationsEnabled }) => (
<TouchableOpacity
style={[
styles.enablePushButtonContainer,
// eslint-disable-next-line react-native/no-inline-styles
{ backgroundColor: notificationsEnabled ? '#6CA15F' : '#E0E0E0' },
]}
onPress={() => handleNotificationsEnabled(!notificationsEnabled)}
>
<Text style={styles.enablePushRowText}>
{notificationsEnabled ? 'Push Enabled' : 'Push Disabled'}
</Text>
</TouchableOpacity>
);
export default function HomeScreen() {
const [channelId, setChannelId] = useState(null);
const [namedUser, setNamedUser] = useState<string | undefined>(undefined);
const [namedUserText, setNamedUserText] = useState('');
const [tags, setTags] = useState<string[]>([]);
const [tagText, setTagText] = useState('');
const [notificationsEnabled, setNotificationsEnabled] = useState(false);
const [isEmbeddedReady, setEmbeddedReady] = useState(false);
const refreshTags = useCallback(async () => {
const fetchedTags = await Airship.channel.getTags();
setTags(fetchedTags);
}, []);
const refreshNamedUser = useCallback(async () => {
const fetchedNamedUser = await Airship.contact.getNamedUserId();
setNamedUser(fetchedNamedUser);
}, []);
const handleNamedUserSet = useCallback(async () => {
await Airship.contact.identify(namedUserText);
await refreshNamedUser();
setNamedUserText(''); // Clear named user text once set
}, [namedUserText, refreshNamedUser]);
const handleTagAdd = useCallback(async () => {
await Airship.channel.addTag(tagText);
await refreshTags();
setTagText('');
}, [tagText, refreshTags]);
const handleTagRemove = useCallback(
async (text: string) => {
await Airship.channel.removeTag(text);
await refreshTags();
},
[refreshTags]
);
const handleNotificationsEnabled = useCallback(async (enabled: boolean) => {
if (enabled) {
await Airship.push.enableUserNotifications({
fallback: 'systemSettings',
});
} else {
Airship.push.setUserNotificationsEnabled(false);
}
}, []);
useEffect(() => {
setEmbeddedReady(Airship.inApp.isEmbeddedReady('test'));
Airship.channel.getChannelId().then((id) => {
if (id) {
setChannelId(id);
}
});
Airship.push
.getNotificationStatus()
.then((status) => setNotificationsEnabled(status.isUserOptedIn));
const fetchTags = async () => {
const fetchedTags = await Airship.channel.getTags();
setTags(fetchedTags);
};
fetchTags();
const fetchNamedUser = async () => {
const fetchedNamedUser = await Airship.contact.getNamedUserId();
setNamedUser(fetchedNamedUser);
};
fetchNamedUser();
let channelListener = Airship.addListener(
EventType.ChannelCreated,
(event) => {
setChannelId(event.channelId);
}
);
let embeddedListener = Airship.inApp.addEmbeddedReadyListener(
'test',
(isReady) => {
setEmbeddedReady(isReady);
}
);
let optInListener = Airship.addListener(
EventType.PushNotificationStatusChangedStatus,
(event) => {
console.log('Event', event);
setNotificationsEnabled(event.status.isUserOptedIn);
}
);
return () => {
channelListener.remove();
embeddedListener.remove();
optInListener.remove();
};
}, []);
return (
<KeyboardAvoidingView
style={{ flex: 1 }}
behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
keyboardVerticalOffset={Platform.OS === 'ios' ? 200 : 0}
>
<View style={{ flex: 1, flexShrink: 0, padding: 20 }}>
{isEmbeddedReady ? (
<View style={{ flex: 1 }}>
<AirshipEmbeddedView embeddedId="test" style={{ flex: 1 }} />
</View>
) : (
<View
style={{
flex: 1,
justifyContent: 'center',
alignItems: 'center',
}}
>
<Image
style={styles.backgroundIcon}
source={require('./../img/airship-mark.png')}
/>
</View>
)}
<View style={{ flexDirection: 'column' }}>
{channelId ? (
<>
<View style={[styles.roundedView, { marginBottom: 8 }]} />
<EnablePushCell
notificationsEnabled={notificationsEnabled}
handleNotificationsEnabled={handleNotificationsEnabled}
/>
<View style={[styles.roundedView, { marginBottom: 8 }]}>
<ChannelCell channelId={channelId} />
</View>
<View style={[styles.roundedView, { marginBottom: 8 }]}>
<NamedUserManagerCell
namedUserText={namedUserText}
handleNamedUserSet={handleNamedUserSet}
handleUpdateNamedUserText={setNamedUserText}
namedUser={namedUser}
/>
</View>
<View style={styles.roundedView}>
<TagManagerCell
tagText={tagText}
tags={tags}
handleTagAdd={handleTagAdd}
handleTagRemove={handleTagRemove}
handleUpdateTagText={setTagText}
/>
</View>
</>
) : (
<View style={styles.warningView}>
<Text style={styles.warningTitleText}>Channel Unavailble</Text>
<Text style={styles.warningBodyText}>
Have you added the takeOff call with the correct app key and
secret?
</Text>
</View>
)}
</View>
<View style={[styles.roundedView, { marginVertical: 8, padding: 8}]}>
{Platform.OS === 'ios' ? (
<Text style={{ fontWeight: 'bold', marginStart: 8 }}>
Live Activities
</Text>
) : (
<Text style={{ fontWeight: 'bold', marginStart: 8 }}>
Live Updates
</Text>
)}
<Button
onPress={async () => {
if (Platform.OS === 'ios') {
Airship.iOS.liveActivityManager.start({
attributesType: 'ExampleWidgetsAttributes',
content: {
state: {
emoji: 'π',
},
relevanceScore: 0.0,
},
attributes: {
name: uuid.v4(),
},
});
} else {
Airship.android.liveUpdateManager.start({
type: 'Example',
name: uuid.v4(),
content: {
emoji: 'π',
},
});
}
}}
title="Start New"
color="#841584"
/>
<Button
onPress={async () => {
if (Platform.OS === 'ios') {
const activities =
await Airship.iOS.liveActivityManager.listAll();
activities.forEach((element) => {
Airship.iOS.liveActivityManager.end({
activityId: element.id,
dismissalPolicy: {
type: "immediate"
}
});
});
} else {
const activities =
await Airship.android.liveUpdateManager.listAll();
activities.forEach((element) => {
Airship.android.liveUpdateManager.end({
name: element.name,
});
});
}
}}
title="End All"
color="#841584"
/>
<Button
onPress={async () => {
if (Platform.OS === 'ios') {
const activities =
await Airship.iOS.liveActivityManager.listAll();
activities.forEach((element) => {
Airship.iOS.liveActivityManager.update({
activityId: element.id,
content: {
state: {
emoji: element.content.state.emoji + 'π',
},
relevanceScore: 0.0,
},
});
});
} else {
const activities =
await Airship.android.liveUpdateManager.listAll();
activities.forEach((element) => {
Airship.android.liveUpdateManager.update({
name: element.name,
content: {
emoji: element.content.emoji + 'π',
},
});
});
}
}}
title="Update All"
color="#841584"
/>
</View>
<View style={{ flexGrow: 0 }} />
</View>
</KeyboardAvoidingView>
);
}