Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import { Notifications } from 'expo';
import { AppState, Platform } from 'react-native';
import { Component } from 'react';
const isIos = Platform.OS === 'ios';
export class NotificationHandler extends Component {
state = {
appState: AppState.current
};
handleNotification = async (notification = {}) => {
console.log('incoming push messsage', notification);
const { origin, data = {} } = notification;
const isAppActive = this.state.appState === 'active';
//ios does not show push notification when app in foreground
if (isIos && isAppActive) {
return //do something appropriate as the app is in foreground
}
if (origin === 'selected') {
//user pressed the notification, do something based on that.
}
};
handleAppState = nextAppState => {
this.setState({ appState: nextAppState });
};
async componentWillUnmount () {
(await this._notificationSubscription) &&
this._notificationSubscription.remove();
AppState.removeEventListener('change', this.handleAppState);
}
async componentDidMount () {
this._notificationSubscription = await Notifications.addListener(
this.handleNotification
);
AppState.addEventListener('change', this.handleAppState);
}
render () {
return null;
}
}
export default NotificationHandler;
@jozef5198

This comment has been minimized.

Copy link

@jozef5198 jozef5198 commented Jan 28, 2020

Thanks man!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.