离子电容器|请求系统通知访问(通知或任何其他)

问题描述 投票:0回答:3

如何使用离子电容在Android上弹出系统“请求通知访问”窗口?

PushNotifications.requestPermissions()
的文档说“android总是收到通知”,这是错误的。某些品牌(例如三星)或用户可以默认禁用它们。 :

**
 * Request permission to receive push notifications.
 *
 * On Android it doesn't prompt for permission because you can always
 * receive push notifications.
 *
 * On iOS, the first time you use the function, it will prompt the user
 * for push notification permission and return granted or denied based
 * on the user selection. On following calls it will get the current status of
 * the permission without prompting again.
 *
 * @since 1.0.0
 */
requestPermissions(): Promise<PermissionStatus>;

还将其添加到 AndroidManifest.xml 但什么也没有。

<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
ionic-framework ionic4 capacitor
3个回答
2
投票

<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
Android 13 新增,仅可在 Android 13 设备中请求,之前的 Android 版本不会提示。

Capacitor 4 并不正式支持该权限,它会在您创建通知通道时请求该权限,但在某些设备中,直到下次应用程序启动时才会提示。

如果你想请求许可,你必须使用 Capacitor 5,目前是 alpha 版本,也是

@capacitor/push-notifications
的版本 5,目前也是 alpha 版本。您可以使用
next
npm 标签安装它们,即
npm install @capacitor/push-notifications@next


0
投票

对于 Android API 级别 33+,请求推送通知权限将无法以传统方式工作。可以通过其之上的android层来实现。在 MainActivity.java 文件中进行以下更改,以提示用户获得通知权限。

com.app 包;

import android.Manifest;
import android.os.Bundle;
import android.os.Build;
import android.os.Handler;

import androidx.core.content.ContextCompat;

import android.content.pm.PackageManager;

import androidx.core.app.ActivityCompat;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;

import com.getcapacitor.BridgeActivity;

public class MainActivity extends BridgeActivity {

  public static final int DOWNLOAD_REQUEST_CODE = 1024;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    CheckNotifications();
  }

  public void CheckNotifications() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {

      if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) ==
        PackageManager.PERMISSION_GRANTED) {
        // FCM SDK (and your app) can post notifications.
      } else if (shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS)) {
        // TODO: display an educational UI explaining to the user the features that will be enabled
        //       by them granting the POST_NOTIFICATION permission. This UI should provide the user
        //       "OK" and "No thanks" buttons. If the user selects "OK," directly request the permission.
        //       If the user selects "No thanks," allow the user to continue without notifications.
      } else {
        // Directly ask for the permission
        requestPermission.launch(Manifest.permission.POST_NOTIFICATIONS);
      }
    }
  }

  private ActivityResultLauncher<String> requestPermission =
    registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
      if (isGranted) {
        // Permission is granted. Continue the action or workflow in your
        // app.
      } else {
        // Explain to the user that the feature is unavailable because the
        // features requires a permission that the user has denied. At the
        // same time, respect the user's decision. Don't link to system
        // settings in an effort to convince the user to change their
        // decision.
      }
    });

}

-1
投票

文档显示和示例:

const registerNotifications = async () => {
 let permStatus = await PushNotifications.checkPermissions();

 if (permStatus.receive === 'prompt') {
  permStatus = await PushNotifications.requestPermissions();
 }

 if (permStatus.receive !== 'granted') {
  throw new Error('User denied permissions!');
 }

 await PushNotifications.register();
}

https://capacitorjs.com/docs/apis/push-notifications

© www.soinside.com 2019 - 2024. All rights reserved.