Reputation: 590
I have a react app with settings screens which allows to disable all types of notifications. How to achieve this wasn't exactly clear in react native documentation. I can ask for user permission when he signup but how do I disable this permission from settings screen so he will not receive any notifications but user can enable them again in future.
I can do this using topics but that's pretty limited. I want to target using audience from firebase console. Any help appreciated.
Upvotes: 10
Views: 9704
Reputation: 1876
You can do something like this. you need to check platform and do the configuration respectively.
For Android to check notification status you need react-native-permissions
library.
import {checkNotifications} from 'react-native-permissions';
Depending on the notification
you can enable and disable the button.
async checkNotificationStatus(){
if(Platform.OS === 'ios'){
const authStatus = await messaging().hasPermission();
if(authStatus === messaging.AuthorizationStatus.AUTHORIZED) {
this.setState({
notification: true
})
}else{
this.setState({
notification: false
})
}
}else{
checkNotifications().then(({status}) => {
if(status === 'granted'){
this.setState({
notification: true
})
}else{
this.setState({
notification: false
})
}
});
}
}
to Enable and Disable when button click. you can simply navigate the user to System settings
async toggleNotification(){
if(Platform.OS === 'ios'){
const authStatus = await messaging().hasPermission();
if (authStatus === messaging.AuthorizationStatus.NOT_DETERMINED) {
const authorizationStatus = await messaging().requestPermission();
if (authorizationStatus === messaging.AuthorizationStatus.AUTHORIZED) {
this.setState({
notification: true
})
}
} else{
Linking.openURL('app-settings://')
}
}else{
Linking.openSettings();
}
}
Upvotes: 8