cph-cachet / flutter-plugins

A collection of Flutter plugins developed by CACHET
551 stars 681 forks source link

[9.0.0] still have a permission problem on API34 of Android #888

Closed tomo689 closed 9 months ago

tomo689 commented 9 months ago

Device / Emulator and OS ・Pixel 7Pro API 34 ・Pixel 6a API 30

Describe the bug I got the below error on the Pixel 7Pro API 34 emulator. (I didn't have any problems on the Pixel 6a API 30 emulator.)

Error Content

I/flutter (21826): >> trying to get permissions for [STEPS] with permissions [0]
I/FLUTTER_HEALTH(21826): Access Denied (to Health Connect)!
I/flutter (21826): >> isAuthorized: false
I/flutter (21826): Authorization not granted - error in authorization

pubspec.yaml

health: ^9.0.0

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.___">
    <uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.BODY_SENSORS"/>

I am sure that user permission is correctly set up in AndroidManifest.xml

GiannosP14 commented 9 months ago

I have the same issue on API 34 and on physical device (Galaxy A33). Any suggestions?

harshitmahapatra commented 9 months ago

Hi, I had the exact same issue as you on a physical Samsung Galaxy S21 (Android 14). I looked at the mainfest in the example app.

Turns out I was missing the activity-alias.

        <activity-alias
            android:name="ViewPermissionUsageActivity"
            android:exported="true"
            android:targetActivity=".MainActivity"
            android:permission="android.permission.START_VIEW_PERMISSION_USAGE">
            <intent-filter>
                <action android:name="android.intent.action.VIEW_PERMISSION_USAGE" />
                <category android:name="android.intent.category.HEALTH_PERMISSIONS" />
            </intent-filter>
        </activity-alias>

To be precise, you need to do the following:

  1. Change FlutterActivity to FlutterFragmentActivity in android/app/src/main/kotlin//MainActivity.kt
  2. Add the query to check if health connect is installed or not in android/app/src/main/AndroidManifest.xml (outside of the application tag)
      <!-- Check whether Health Connect is installed or not -->
      <queries>
      <package android:name="com.google.android.apps.healthdata" />
      <intent>
          <action android:name="androidx.health.ACTION_SHOW_PERMISSIONS_RATIONALE" />
      </intent>
      </queries> 
  3. Add the permissions for the data types you use in the AndroidManifest.xml, activity recognition is always needed (outside of the application tag).
    <uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>
    <uses-permission android:name="android.permission.health.READ_STEPS"/>
  4. Add the intent to show permissions (inside the application tag) in the AndroidManifest.xml.
              <!-- Intention to show Permissions screen for Health Connect API -->
            <intent-filter>
                <action android:name="androidx.health.ACTION_SHOW_PERMISSIONS_RATIONALE" />
            </intent-filter>
  5. Add the activity-alias (inside the application tag) in the AndroidManifest.xml.
          <activity-alias
            android:name="ViewPermissionUsageActivity"
            android:exported="true"
            android:targetActivity=".MainActivity"
            android:permission="android.permission.START_VIEW_PERMISSION_USAGE">
            <intent-filter>
                <action android:name="android.intent.action.VIEW_PERMISSION_USAGE" />
                <category android:name="android.intent.category.HEALTH_PERMISSIONS" />
            </intent-filter>
        </activity-alias>

Hopefully this helps! I would recommend comparing your manifest with the example app's manifest and trying again.

majhamza commented 7 months ago

Hi, I had the exact same issue as you on a physical Samsung Galaxy S21 (Android 14). I looked at the mainfest in the example app.

Turns out I was missing the activity-alias.

        <activity-alias
            android:name="ViewPermissionUsageActivity"
            android:exported="true"
            android:targetActivity=".MainActivity"
            android:permission="android.permission.START_VIEW_PERMISSION_USAGE">
            <intent-filter>
                <action android:name="android.intent.action.VIEW_PERMISSION_USAGE" />
                <category android:name="android.intent.category.HEALTH_PERMISSIONS" />
            </intent-filter>
        </activity-alias>

To be precise, you need to do the following:

1. Change FlutterActivity to FlutterFragmentActivity in android/app/src/main/kotlin/<your_app_package>/MainActivity.kt

2. Add the query to check if health connect is installed or not in android/app/src/main/AndroidManifest.xml (outside of the application tag)
    <!-- Check whether Health Connect is installed or not -->
    <queries>
    <package android:name="com.google.android.apps.healthdata" />
    <intent>
        <action android:name="androidx.health.ACTION_SHOW_PERMISSIONS_RATIONALE" />
    </intent>
    </queries> 
3. Add the permissions for the data types you use  in the AndroidManifest.xml, activity recognition is always needed (outside of the application tag).
  <uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>
  <uses-permission android:name="android.permission.health.READ_STEPS"/>
5. Add the intent to show permissions (inside the application tag) in the AndroidManifest.xml.
            <!-- Intention to show Permissions screen for Health Connect API -->
          <intent-filter>
              <action android:name="androidx.health.ACTION_SHOW_PERMISSIONS_RATIONALE" />
          </intent-filter>
6. Add the activity-alias (outside the application tag) in the AndroidManifest.xml.
        <activity-alias
          android:name="ViewPermissionUsageActivity"
          android:exported="true"
          android:targetActivity=".MainActivity"
          android:permission="android.permission.START_VIEW_PERMISSION_USAGE">
          <intent-filter>
              <action android:name="android.intent.action.VIEW_PERMISSION_USAGE" />
              <category android:name="android.intent.category.HEALTH_PERMISSIONS" />
          </intent-filter>
      </activity-alias>

Hopefully this helps! I would recommend comparing your manifest with the example app's manifest and trying again.

I had the same issue, and this solution worked for me. Just a small clarification, though: the activity-alias should be copied inside the tag, not outside

harshitmahapatra commented 7 months ago

Hi, I had the exact same issue as you on a physical Samsung Galaxy S21 (Android 14). I looked at the mainfest in the example app. Turns out I was missing the activity-alias.

        <activity-alias
            android:name="ViewPermissionUsageActivity"
            android:exported="true"
            android:targetActivity=".MainActivity"
            android:permission="android.permission.START_VIEW_PERMISSION_USAGE">
            <intent-filter>
                <action android:name="android.intent.action.VIEW_PERMISSION_USAGE" />
                <category android:name="android.intent.category.HEALTH_PERMISSIONS" />
            </intent-filter>
        </activity-alias>

To be precise, you need to do the following:

1. Change FlutterActivity to FlutterFragmentActivity in android/app/src/main/kotlin/<your_app_package>/MainActivity.kt

2. Add the query to check if health connect is installed or not in android/app/src/main/AndroidManifest.xml (outside of the application tag)
    <!-- Check whether Health Connect is installed or not -->
    <queries>
    <package android:name="com.google.android.apps.healthdata" />
    <intent>
        <action android:name="androidx.health.ACTION_SHOW_PERMISSIONS_RATIONALE" />
    </intent>
    </queries> 
3. Add the permissions for the data types you use  in the AndroidManifest.xml, activity recognition is always needed (outside of the application tag).
  <uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>
  <uses-permission android:name="android.permission.health.READ_STEPS"/>
5. Add the intent to show permissions (inside the application tag) in the AndroidManifest.xml.
            <!-- Intention to show Permissions screen for Health Connect API -->
          <intent-filter>
              <action android:name="androidx.health.ACTION_SHOW_PERMISSIONS_RATIONALE" />
          </intent-filter>
6. Add the activity-alias (outside the application tag) in the AndroidManifest.xml.
        <activity-alias
          android:name="ViewPermissionUsageActivity"
          android:exported="true"
          android:targetActivity=".MainActivity"
          android:permission="android.permission.START_VIEW_PERMISSION_USAGE">
          <intent-filter>
              <action android:name="android.intent.action.VIEW_PERMISSION_USAGE" />
              <category android:name="android.intent.category.HEALTH_PERMISSIONS" />
          </intent-filter>
      </activity-alias>

Hopefully this helps! I would recommend comparing your manifest with the example app's manifest and trying again.

I had the same issue, and this solution worked for me. Just a small clarification, though: the activity-alias should be copied inside the tag, not outside

Thanks! Have updated the comment.