TobiasBuchholz / Plugin.Firebase

Wrapper around the native Android and iOS Firebase Xamarin SDKs
MIT License
211 stars 49 forks source link

[Question] gets "Couldn't retrieve FCM token Error" #162

Closed JoacimWall closed 10 months ago

JoacimWall commented 1 year ago

Hi

Now "CheckIfValidAsync" just show the dialog and move on when you later use GetTokenAsync you get error
"Couldn't retrieve FCM token Error".

it's works if you answer yes on the notification dialog but crash if you have answer no. and call GetTokenAsync().

Is there support in the nuget to check if the user has accept?

I have looked throw you sample and I can't find that you are doning any checks.

//Joacim

TobiasBuchholz commented 1 year ago

Sorry, the sample project is not up-to-date in regards to the android target api level 33. At the moment the plugin doesn't provide any functionality to check whether the new notification permission is granted or not. There is the CheckIfValidAsync() method, but it only checks whether the GooglePlayServices are available. I will think about adding this check to the plugin but you can do it already by yourself as well with something like this:

// code to check if the permission is granted
if(Build.VERSION.SdkInt >= BuildVersionCodes.Tiramisu && ContextCompat.CheckSelfPermission(_context, Manifest.Permission.PostNotifications) == Permission.Granted) {
    // get the token or whatever
}

// code to request the permission
ActivityCompat.RequestPermissions(_activity, new[] { Manifest.Permission.PostNotifications }, 0);

You will also need to add the following line to your AndroidManifest.xml:

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

Take a look at the official android documentation for more information.