android – React Native handle IOS permissions for FCM messaging-ThrowExceptions

Exception or error:

I integrated FCM messaging to my React Native app.

In my App component i tried to grant permissions by asking the user if he accept to get notifications or not .

My question is, am i using to right way to grant permissions?

I use async componentDidMount to implement the function requestPermission :

import firebase from '@react-native-firebase/app'
import messaging from '@react-native-firebase/messaging'

async componentDidMount () {
 // ......
    const granted = messaging().requestPermission()
    if (granted) {
  console.log('User granted messaging permissions!')
  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
  firebase
    .messaging()
    .getToken()
    .then(fcmToken => {
      if (fcmToken) {
        // user has a device token
        console.log('fcm')
        console.log(fcmToken)
      } else {
        // user doesn't have a device token yet
        console.log('error')
      }
    })
  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
} else {
  console.log('User declined messaging permissions :(')
}
}

I get the FCM generated token, and i can send message using Postman to the device using this token. How can i be sure that the permissions are always granted and i can get the token from every device ? 
How to solve:

when u ask permission if the use declines or cancels the request u can ask the user that the app needs that permission and redirect him to the apps settings page,,,
u can check weather permision is granted every time app is launched and if not redirect them to settings page, since ios asks permisions only once if user declines it that permision wont be asked again.. here is an example code for checking and requesting permisions

 firebase
      .messaging()
      .hasPermission()
      .then(enabled => {       
        if (enabled) {
          setNotification();
          onNotification();
        } else {
          firebase
            .messaging()
            .requestPermission()
            .then()
            .catch(err => {
              if (!isAndroid) {
                Linking.canOpenURL('app-settings:')
                  .then(supported => {
                    Linking.openURL('app-settings:');
                  })
                  .catch(error => {});
              }
            });
       }
     })
.catch();

Leave a Reply

Your email address will not be published. Required fields are marked *