Last active
April 20, 2017 04:45
-
-
Save mstifflin/ac47c285fa6825e9ba3f4e9e39901f73 to your computer and use it in GitHub Desktop.
Create Local Android Push Notifications in React Native
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import { AppState, View, Button, Text, StyleSheet } from 'react-native'; | |
import PushNotification from 'react-native-push-notification'; | |
import PushController from './PushController.js'; //The push controller created earlier | |
export default class HomeScreen extends Component { | |
constructor(props) { | |
super(props); | |
this.state = {}; | |
this.handleAppStateChange = this.handleAppStateChange.bind(this); | |
this.sendNotification = this.sendNotification.bind(this); | |
}; | |
componentDidMount() { | |
AppState.addEventListener('change', this.handleAppStateChange); | |
}; | |
componentWillUnmount() { | |
AppState.removeEventListener('change', this.handleAppStateChange); | |
}; | |
// This will notify the user in 3 seconds after sending the app to the | |
// background (like after pressing the home button or switching apps) | |
handleAppStateChange(appState) { | |
if (appState === 'background') { | |
// Schedule a notification | |
PushNotification.localNotificationSchedule({ | |
message: 'Scheduled delay notification message', // (required) | |
date: new Date(Date.now() + (3 * 1000)) // in 3 secs | |
}); | |
} | |
}; | |
sendNotification() { | |
PushNotification.localNotification({ | |
message: 'You pushed the notification button!' | |
}); | |
}; | |
render() { | |
return ( | |
<View> | |
<Button title='Press here for a notification' | |
onPress={this.sendNotification} /> | |
<PushController /> | |
</View> | |
); | |
}; | |
}; | |
const styles = StyleSheet.create({ | |
container: { | |
flex: 1, | |
justifyContent: 'center', | |
alignItems: 'center' | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment