Created
November 13, 2017 18:34
-
-
Save arindam89/abbbd35ff196e979f3286764487ace53 to your computer and use it in GitHub Desktop.
Using Pushy 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 EStyleSheet from 'react-native-extended-stylesheet'; | |
import React, { Component } from 'react'; | |
import { Provider, connect } from 'react-redux'; | |
import { addNavigationHelpers } from 'react-navigation'; | |
import { View } from 'react-native'; | |
import Pushy from 'pushy-react-native'; | |
import store from './state/store'; | |
import AllIsWellAppNavigator from './config/routes'; | |
import Alert from './components/Alert/Alert'; | |
/** | |
* Global style variables are all defined here. | |
*/ | |
EStyleSheet.build({ | |
$primaryBlue: '#4F6D7A', | |
$white: '#FFFFFF', | |
$lightGray: '#F0F0F0', | |
$border: '#979797', | |
$inputText: '#797979', | |
}); | |
const AllIsWellApp = ({ dispatch, nav }) => ( | |
<AllIsWellAppNavigator | |
navigation={addNavigationHelpers({ | |
dispatch, | |
state: nav, | |
})} | |
/> | |
); | |
const mapStateToProps = state => ({ | |
nav: state.nav, | |
}); | |
const AllIsWellAppWithNavigation = connect(mapStateToProps)(AllIsWellApp); | |
// Handle push notifications | |
Pushy.setNotificationListener(async (data) => { | |
// Print notification payload data | |
console.log(`Received notification: ${JSON.stringify(data)}`); | |
// Notification title | |
const notificationTitle = 'Pushy'; | |
// Attempt to extract the "message" property from the payload: {"message":"Hello World!"} | |
const notificationText = data.message || 'Test notification'; | |
// Display basic system notification | |
Pushy.notify(notificationTitle, notificationText); | |
}); | |
export default class AllIsWellAppWrapped extends Component { | |
componentDidMount() { | |
// Start the Pushy service | |
Pushy.listen(); | |
// Register the device for push notifications | |
Pushy.register() | |
.then(async (deviceToken) => { | |
// Print device token to console | |
console.log(`Pushy device token: ${deviceToken}`); | |
// Send the token to your backend server via an HTTP GET request | |
// await fetch('https://your.api.hostname/register/device?token=' + deviceToken); | |
// Succeeded, do something to alert the user | |
}) | |
.catch((err) => { | |
// Handle registration errors | |
console.error(err); | |
}); | |
} | |
render() { | |
return ( | |
<Provider store={store}> | |
<View style={{ width: '100%', height: '100%' }}> | |
<AllIsWellAppWithNavigation /> | |
<Alert /> | |
</View> | |
</Provider> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment