Skip to content

Instantly share code, notes, and snippets.

@maxcodes
Last active January 21, 2018 17:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxcodes/6d4657b4253e72e2be6fd391416f6cf3 to your computer and use it in GitHub Desktop.
Save maxcodes/6d4657b4253e72e2be6fd391416f6cf3 to your computer and use it in GitHub Desktop.
OneSignal
import { compose, setPropTypes, withHandlers, lifecycle, getContext, withState } from 'recompose'
const HomeScene = ({ }) => (
<View>
// omitted
</View>
)
export default compose(
setPropTypes({
stale: PropTypes.bool,
}),
getContext({
environment: PropTypes.object.isRequired,
navigator: PropTypes.object.isRequired,
}),
withHandlers({
navigateFromParams: ({ navigator }) => (params) => {
if (params) {
const scene = params['scene'] || params['object']
const id = params['ID']
if(scene === 'Quote') {
const bookID = id || params['bookID']
navigator.push(getQuotesRoute(bookID))
} else if (scene === 'Book') {
const bookID = id || params['bookID']
navigator.push(getBookRoute(bookID))
} else if (scene === 'Shelf') {
const shelfID = id || params['shelfID']
navigator.push(getLibraryRoute(shelfID))
} else if (scene === 'TopicQuotes') {
const topicSlug = id || params['topicSlug']
navigator.push(getTopicQuotesRoute(topicSlug))
} else if (scene === 'BookClub') {
navigator.push(getBookClubRoute(id))
} else if (scene === 'ProgressComments') {
navigator.push(getProgressCommentsScene(id))
} else if (scene === 'ProgressLikes') {
navigator.push(getProgressLikesScene(id))
}
}
},
}),
withHandlers({
onOpened: ({ navigator, navigateFromParams }) => (openResult) => {
console.log("onOpened triggered") // note: this gets called only if app is already in background
navigateFromParams(openResult.notification.payload.additionalData)
},
}),
lifecycle({
componentWillMount () {
const { onOpened, navigateFromParams } = this.props
// setup OneSignal for push notifications
OneSignal.addEventListener('ids', (device) => AsyncStorage.setItem('@DeviceID:key', device.userId))
OneSignal.addEventListener('opened', onOpened)
branch.subscribe(({ params }) => {
navigateFromParams(params)
})
},
componentWillUnmount () {
OneSignal.removeEventListener('opened', onOpened)
},
}),
)(HomeScene)
class HomeScene extends React.Component {
static propTypes = {
environment: PropTypes.bool,
}
static childContextTypes = {
environment: PropTypes.instanceOf(Environment).isRequired,
navigator: PropTypes.object.isRequired,
}
getChildContext () {
const { environment, navigator } = this.props
return { environment, navigator }
}
navigateFromParams (params) {
if (!params) {
return
}
const scene = params['scene'] || params['object']
const id = params['ID']
const { navigator } = this.props
if(scene === 'Quote') {
const bookID = id || params['bookID']
navigator.push(getQuotesRoute(bookID))
} else if (scene === 'Book') {
const bookID = id || params['bookID']
navigator.push(getBookRoute(bookID))
} else if (scene === 'Shelf') {
const shelfID = id || params['shelfID']
navigator.push(getLibraryRoute(shelfID))
} else if (scene === 'TopicQuotes') {
const topicSlug = id || params['topicSlug']
navigator.push(getTopicQuotesRoute(topicSlug))
} else if (scene === 'BookClub') {
navigator.push(getBookClubRoute(id))
} else if (scene === 'ProgressComments') {
navigator.push(getProgressCommentsScene(id))
} else if (scene === 'ProgressLikes') {
navigator.push(getProgressLikesScene(id))
}
}
onOpened (openResult) {
console.log("onOpened triggered") // note: this gets called only if app is already in background
this.navigateFromParams(openResult.notification.payload.additionalData)
}
componentWillMount () {
// setup OneSignal for push notifications
OneSignal.addEventListener('ids', (device) => AsyncStorage.setItem('@DeviceID:key', device.userId))
OneSignal.addEventListener('opened', this.onOpened)
branch.subscribe(({ params }) => {
this.navigateFromParams(params)
})
}
componentWillUnmount () {
OneSignal.removeEventListener('opened', onOpened)
}
render () {
// omitted
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment