Skip to content

Instantly share code, notes, and snippets.

@arnaudambro
Last active December 13, 2020 21:29
Show Gist options
  • Save arnaudambro/6c4b22b7a4f3639aae4f025a70ae0109 to your computer and use it in GitHub Desktop.
Save arnaudambro/6c4b22b7a4f3639aae4f025a70ae0109 to your computer and use it in GitHub Desktop.
Set-up request notifications routes
import React from 'react';
import {SafeAreaView, StyleSheet, View, Text, StatusBar, TouchableOpacity, Platform} from 'react-native';
import Notifications from './NotificationService'
class App extends React.Component {
constructor(props) {
super(props);
Notifications.init(this.handleRegister);
}
handleRegister = async deviceToken => {
this.userId = DeviceInfo.getUniqueId();
this.registered = await fetch(`http://192.168.178.12:7777/save-token/${this.userId}/${deviceToken}`, {
method: 'POST',
}).then(res => res.ok);
};
componentDidMount() {
Notifications.listen(notification => console.log('do what you need to do', notification);
}
componentWillUnmount() {
Notifications.delete();
}
handleNotificationPressWithDelay = delay => async () => {
const params = [this.userId, delay];
await fetch(`http://{YOUR_IP}:7777/test-send/${params.filter(p => p).join('/')}`)
.then(res => console.log({res}))
.catch(error => console.log({error}));
};
render() {
return (
<>
<StatusBar barStyle="dark-content" />
<SafeAreaView style={styles.body}>
<TouchableOpacity
accessibilityRole={'button'}
onPress={this.handleNotificationPressWithDelay(10000)}
style={styles.linkContainer}>
<Text style={styles.link}>Notification in 10 seconds</Text>
</TouchableOpacity>
<View style={styles.separator} />
<TouchableOpacity
accessibilityRole={'button'}
onPress={this.handleNotificationPressWithDelay(0)}
style={styles.linkContainer}>
<Text style={styles.link}>Notification now</Text>
</TouchableOpacity>
</SafeAreaView>
</>
);
}
}
// styles in https://github.com/arnaudambro/push-notifications-test/blob/master/front/App.js
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment