Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 504
quazar Avatar asked Apr 22 '20 16:04

quazar


People also ask

How do I turn off push notifications in React Native?

Setting the id when calling PushNotification. localNotification() to the same value as the previous notification will overwrite the notification.


1 Answers

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();
        }
    }
like image 136
Im Batman Avatar answered Oct 16 '22 23:10

Im Batman