如何在Capacitor 4项目中使用Android权限

xytpbqjk  于 2022-11-03  发布在  Android
关注(0)|答案(1)|浏览(368)

我正在尝试将推送通知与FCM一起使用,但我不知道如何在Android版本中实现权限,以便在用户希望允许通知不在其设备上时显示消息对话框
service.ts

async registerFCM() {

await PushNotifications.addListener('pushNotificationReceived', notification => {
    console.log('Push notification received: ', notification);
  });

  await PushNotifications.addListener('pushNotificationActionPerformed', notification => {
    console.log('Push notification action performed', notification.actionId, notification.inputValue);
  });

  PushNotifications.requestPermissions().then(async result => {
    if (result.receive === 'granted') {
        if (Capacitor.getPlatform() === 'android') {
            await PushNotifications.register()
            PushNotifications.addListener('registration', async ({ value }) => {
                console.log(value);

                this.submitFCM(value);
            });
          }
          else{
        // Register with Apple / Google to receive push via APNS/FCM
        await PushNotifications.register();

        // Get FCM token instead the APN one returned by Capacitor
        FCM.getToken()
            .then((r) => {
                console.log(`Token: ${r.token}`)

                this.submitFCM(r.token);
            })
            .catch((err) => console.log(err));
        }
    } else {
        // Show some error
    }
});

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android">

 <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
     android:usesCleartextTraffic="true">

    <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
    <meta-data android:name="com.facebook.sdk.ClientToken" android:value="@string/facebook_client_token"/>

    <activity
    android:Permissions
        android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode"
        android:name="com.myproject.mobile.MainActivity"
        android:label="@string/title_activity_main"
        android:theme="@style/AppTheme.NoActionBarLaunch"
        android:launchMode="singleTask"
        android:exported="true">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>

    <service
        android:name="com.google.firebase.messaging.FirebaseMessagingService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

    <provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="${applicationId}.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths"></meta-data>
    </provider>
   </application>

<!-- Permissions -->

   <uses-permission android:name="android.permission.INTERNET" />
   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>

我可以在〉〈uses-permission中添加它们吗?我如何为推送通知和上传视频和音频文件添加它们?
我尝试使用权限,但无法使用。我希望使用Android的权限对话框
因为我有PushNotifications.requestPermissions().then(async result =〉{它应该能正确地完成这项工作?但它没有

bwleehnv

bwleehnv1#

通知权限为<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
它只适用于Android 13设备,在此之前,版本通知只是工作,没有提示.
Capacitor插件尚未更新以提示该权限,这在以SDK 33为目标时是必需的(Capacitor目前以SDK 32为目标)。但是,如果您在Android 13设备上进行测试,则如果您以SDK 32为目标,则应用将在应用启动时提示该权限。

相关问题