> ## Documentation Index
> Fetch the complete documentation index at: https://docs.notifly.tech/llms.txt
> Use this file to discover all available pages before exploring further.

# プッシュ通知の許可をリクエストする

> アプリ内でプッシュ通知の許可を求める処理を実装します。

通知の表示にユーザーの許可が必要なプラットフォームでは、アプリで許可をリクエストする処理を実装します。アプリの利用の流れに合わせて、リクエストを表示するタイミングを決めます。

> [tab] Android
>
> Android 13（APIレベル33、`TIRAMISU`）以降では、実行時に通知の許可を求めます。アプリにリクエスト処理を追加してください。事前設定は[通知の実行時権限](https://developer.android.com/develop/ui/views/notifications/notification-permission)を参照してください。
>
> このKotlinの例には`compileSdk` 33以降が必要です。Android 13以降で権限をリクエストするには、最終的にマージされた`AndroidManifest.xml`に`android.permission.POST_NOTIFICATIONS`を宣言する必要があります。FCMなどの依存関係ですでに宣言されている場合は、重複して追加する必要はありません。
>
> ```kotlin theme={null}
> // 通知の許可をリクエストする
> private fun askNotificationPermission() {
>     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU &&
>         ContextCompat.checkSelfPermission(
>             this,
>             Manifest.permission.POST_NOTIFICATIONS
>         ) != PackageManager.PERMISSION_GRANTED
>     ) {
>         ActivityCompat.requestPermissions(
>             this,
>             arrayOf(Manifest.permission.POST_NOTIFICATIONS),
>             101
>         )
>     }
> }
> ```

> [tab] iOS
>
> `UNUserNotificationCenter`で通知の許可をリクエストします。リクエストするタイミング、画面、案内文はアプリに合わせて設定します。
>
> ```swift theme={null}
> UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in
>     if let error = error {
>         print("Failed to request authorization: \(error)")
>         return
>     } else {
>         guard granted else {
>             print("Notification permission denied")
>             return
>         }
>         DispatchQueue.main.async {
>             UIApplication.shared.registerForRemoteNotifications()
>         }
>     }
> }
> ```

> [tab] Flutter
>
> `firebase_messaging` を使うアプリの例です。
>
> ```dart theme={null}
> import 'package:firebase_messaging/firebase_messaging.dart';
>
> FirebaseMessaging messaging = FirebaseMessaging.instance;
> NotificationSettings settings = await messaging.requestPermission(
>   alert: true,
>   badge: true,
>   sound: true,
> );
> ```

> [tab] React Native
>
> iOSでは、以下の例のようにReact Native Firebaseで通知の許可を求めます。Android 13以降では、`PermissionsAndroid`で実行時権限の`POST_NOTIFICATIONS`をリクエストします。Androidタブで説明した最終マニフェストの権限宣言も確認してください。詳しくは[React Native Firebaseの通知権限](https://rnfirebase.io/messaging/usage#android---requesting-permissions)を参照してください。
>
> ```javascript theme={null}
> import messaging from '@react-native-firebase/messaging';
>
> await messaging().requestPermission();
> ```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.