Expo has Notification API for sending push notifications. However, how does sending push notifications work when app is in background? Can they be triggered from server or scheduled in advance?
1
votes
2 Answers
9
votes
Sending push notifications is done from your server so you are totally free in sending them whenever you want and schedule jobs to take care of it.
However you won't be able to handle the receiving of the notification on the client if the app is backgrounded and the user has not selected the notification. According to expo documentation:
You cannot handle push notifications in the background. This is a work in progress.
But if the app is in foreground or the user has selected the notification, then a notification will be passed to the listener in your app.
-2
votes
We can handle push notifications in the background.
For this we have to dismission notification if AppState is 'active' and origin === 'received'.
Here is the full code.
import React from 'react';
import { Text, View, Vibration, AppState } from 'react-native';
import { Notifications } from 'expo';
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
notification: {}
}
}
componentDidMount() {
this._notificationSubscription = Notifications.addListener(
this._handleNotification
);
}
_handleNotification = notification => {
if (AppState.currentState == 'active' && notification.origin === 'received') {
Notifications.dismissNotificationAsync(notification.notificationId);
} else {
Vibration.vibrate()
this.setState({ notification: notification });
}
};
render() {
return (
<View><Text>Push Notifications Sample</Text></View>
);
}
}