|
| 1 | +/** |
| 2 | + * Sample React Native App |
| 3 | + * https://github.com/facebook/react-native |
| 4 | + * @flow |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | +
|
| 9 | + This example assumes there's a parent Component that returns the redux Provider |
| 10 | + wrapping its children with the same store as being imported here being passed |
| 11 | + as the store prop. |
| 12 | +
|
| 13 | +**/ |
| 14 | + |
| 15 | +import React, { Component } from "react"; |
| 16 | +import { connect } from "react-redux"; |
| 17 | +import { AppRegistry, AsyncStorage, StyleSheet } from "react-native"; |
| 18 | +import { persistStore } from "redux-persist"; // [email protected] -- the API for ^5.x.x and rehydrating/purging the store is slightly different |
| 19 | +import OneSignal from "react-native-onesignal"; |
| 20 | +import App from "./src/App"; |
| 21 | +import { store } from "./configureStore.js"; // Whereever you're creating and exporting your store from |
| 22 | + |
| 23 | +// Action creator -- likely to be in the directory with the rest of your project's action creators |
| 24 | +// placed here for the sake of example |
| 25 | +const setInitialNotification = openResult => ({ |
| 26 | + type: T.INITIAL_NOTIFICATION, |
| 27 | + payload: openResult.notification.payload.additionalData |
| 28 | +}); |
| 29 | + |
| 30 | +// function to initially add onOpened listener to |
| 31 | +// handles initial tap of push notification from an unopened AppState |
| 32 | +const handleOnOpened = openResult => { |
| 33 | + console.log(`** INITIAL OPEN RESULT FROM APP.JS **`, openResult); |
| 34 | + console.log("Message: ", openResult.notification.payload.body); |
| 35 | + console.log("Data: ", openResult.notification.payload.additionalData); |
| 36 | + console.log("isActive: ", openResult.notification.isAppInFocus); |
| 37 | + console.log("openResult: ", openResult); |
| 38 | + |
| 39 | + store.dispatch(setInitialNotification(openResult)); |
| 40 | +}; |
| 41 | + |
| 42 | +OneSignal.configure({}); |
| 43 | +OneSignal.addEventListener("opened", handleOnOpened); |
| 44 | + |
| 45 | +class UnconnectedRNOneSignal extends Component { |
| 46 | + state = { |
| 47 | + rehydrated: false |
| 48 | + }; |
| 49 | + |
| 50 | + componentWillMount() { |
| 51 | + OneSignal.addEventListener("received", this.onReceived); |
| 52 | + OneSignal.addEventListener("registered", this.onRegistered); |
| 53 | + OneSignal.addEventListener("ids", this.onIds); |
| 54 | + |
| 55 | + this.rehydrateStore(); |
| 56 | + } |
| 57 | + |
| 58 | + componentWillUnmount() { |
| 59 | + OneSignal.removeEventListener("received", this.onReceived); |
| 60 | + OneSignal.removeEventListener("opened", this.onOpened); |
| 61 | + OneSignal.removeEventListener("registered", this.onRegistered); |
| 62 | + OneSignal.removeEventListener("ids", this.onIds); |
| 63 | + } |
| 64 | + |
| 65 | + rehydrateStore = () => { |
| 66 | + persistStore( |
| 67 | + store, |
| 68 | + { |
| 69 | + // blacklist: ["logs", "statusMessages", "env"], |
| 70 | + storage: AsyncStorage, |
| 71 | + debounce: 50 |
| 72 | + }, |
| 73 | + () => { |
| 74 | + /* |
| 75 | + After rehydrating: |
| 76 | + - remove the 'opened' listener from the handleOnOpened function |
| 77 | + - add the 'opened' listener to this.onOpened |
| 78 | + */ |
| 79 | + |
| 80 | + this.setState({ rehydrated: true }, () => { |
| 81 | + OneSignal.removeEventListener("opened", handleOnOpened); // removing |
| 82 | + OneSignal.addEventListener("opened", this.onOpened); |
| 83 | + }); |
| 84 | + } |
| 85 | + ); |
| 86 | + }; |
| 87 | + |
| 88 | + onReceived = notification => { |
| 89 | + console.log("Notification received: ", notification); |
| 90 | + }; |
| 91 | + |
| 92 | + onOpened = openResult => { |
| 93 | + console.log("Message: ", openResult.notification.payload.body); |
| 94 | + console.log("Data: ", openResult.notification.payload.additionalData); |
| 95 | + console.log("isActive: ", openResult.notification.isAppInFocus); |
| 96 | + console.log("openResult: ", openResult); |
| 97 | + }; |
| 98 | + |
| 99 | + onRegistered = notifData => { |
| 100 | + console.log( |
| 101 | + "Device had been registered for push notifications!", |
| 102 | + notifData |
| 103 | + ); |
| 104 | + }; |
| 105 | + |
| 106 | + onIds = device => { |
| 107 | + console.log("Device info: ", device); |
| 108 | + }; |
| 109 | + |
| 110 | + render() { |
| 111 | + const { rehydrated } = this.state; |
| 112 | + |
| 113 | + return ( |
| 114 | + <App rehydrated={rehydrated} /> // Again, this entire component is being wrapped in redux Provider |
| 115 | + ); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +const styles = StyleSheet.create({ |
| 120 | + container: { |
| 121 | + flex: 1, |
| 122 | + justifyContent: "center", |
| 123 | + alignItems: "center", |
| 124 | + backgroundColor: "#F5FCFF" |
| 125 | + }, |
| 126 | + welcome: { |
| 127 | + fontSize: 20, |
| 128 | + textAlign: "center", |
| 129 | + margin: 10 |
| 130 | + }, |
| 131 | + instructions: { |
| 132 | + textAlign: "center", |
| 133 | + color: "#333333", |
| 134 | + marginBottom: 5 |
| 135 | + } |
| 136 | +}); |
| 137 | + |
| 138 | +const mapStateToProps = state => { |
| 139 | + const { notifications } = state; // probably have a notifications reducer |
| 140 | + |
| 141 | + return { |
| 142 | + notifications |
| 143 | + // any other pieces of reducer states you may need |
| 144 | + }; |
| 145 | +}; |
| 146 | + |
| 147 | +const actionCreatorObj = { |
| 148 | + // whichever action creators responsible for push notifications in your app |
| 149 | + // and anything else relevant for this Component |
| 150 | +}; |
| 151 | + |
| 152 | +const RNOneSignal = connect(mapStateToProps)(UnconnectedRNOneSignal); |
| 153 | + |
| 154 | +export default RNOneSignal; |
| 155 | + |
| 156 | +// AppRegistry.registerComponent("RNOneSignal", () => RNOneSignal); // this is going to happen in the file rending the Provider component and its children |
0 commit comments