简体   繁体   中英

Enable/Disable firebase push notifications in react native app

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.

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();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM