Skip to content

Instantly share code, notes, and snippets.

@rob-mcgrail
Last active October 5, 2017 20:56
Show Gist options
  • Save rob-mcgrail/c41bc23db67b9d1d00552a8958e31b4e to your computer and use it in GitHub Desktop.
Save rob-mcgrail/c41bc23db67b9d1d00552a8958e31b4e to your computer and use it in GitHub Desktop.
Pushwoosh Redux RN integration example.
import React from 'react';
import { Provider } from 'react-redux';
import { Router, AndroidBackButton } from 'react-router-native';
import { StyleProvider } from '@shoutem/theme';
/* Local components */
import StoredSession from 'src/components/connected/StoredSession';
import NotifiableDevice from 'src/components/connected/NotifiableDevice';
import Routes from 'src/routes';
import history from 'src/history';
import storeConfig from 'src/redux/stores';
import theme from 'src/theme';
const store = storeConfig();
const App = () => (
<StyleProvider style={theme}>
<Provider store={store}>
<NotifiableDevice>
<StoredSession>
<Router history={history}>
<AndroidBackButton>
<Routes />
</AndroidBackButton>
</Router>
</StoredSession>
</NotifiableDevice>
</Provider>
</StyleProvider>
);
export default App;
// Requests
import { registerPushwooshToken } from 'src/requests/services/pushwoosh';
// Types
export const types = {
DEVICE_TOKEN: 'app/auth/devices-token'
};
export function registerDeviceToken() {
return async (dispatch) => {
try {
const value = await registerPushwooshToken();
dispatch({
type: types.DEVICE_TOKEN,
payload: value
});
} catch (e) {
console.warn(e);
}
};
}
/* Reducer defaults */
export const defaultState = {
device_token: null
};
/* Define reducer */
export default function (state = defaultState, action) {
switch (action.type) {
case types.DEVICE_TOKEN:
return { ...state, device_token: action.payload };
default:
return state;
}
}
import React, { Component } from 'react';
import PropTypes from 'prop-types';
const Loading = (Wrapped, action) => class extends Component {
static propTypes = {
action: PropTypes.func
}
static defaultProps = {
action: false
}
constructor(props) {
super(props);
this.state = {
loaded: false,
action: action || this.props.action
};
}
async componentDidMount() {
await this.state.action();
this.setState({ loaded: true });
}
render() {
if (this.state.loaded) {
return <Wrapped {...this.props} />;
}
return null; // Add an optional loader here
}
};
export default Loading;
import { connect } from 'react-redux';
import Container from 'src/components/pure/layout/Container';
import Loading from 'src/components/utility/Loading';
import { registerDeviceToken } from 'src/redux/modules/auth';
const NotifiableDevice = connect(
null, { action: registerDeviceToken }
)(Loading(Container));
export default NotifiableDevice;
import Pushwoosh from 'pushwoosh-react-native-plugin';
import config from 'src/config';
Pushwoosh.init({
pw_appid: config.pushwoosh.appId,
project_number: config.pushwoosh.projectNumber
});
export function registerPushwooshToken() {
return new Promise((resolve, reject) => {
Pushwoosh.register(resolve, reject);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment