@@ -23,6 +23,7 @@ class MainActivity : AppCompatActivity(), AlertDialogFragment.Callback {
2323
2424 private val notificationPostman = NotificationPostman ()
2525
26+ // ランタイムパーミッションの結果.
2627 private val requestPermissionLauncher =
2728 registerForActivityResult(ActivityResultContracts .RequestPermission ()) {
2829 Snackbar .make(
@@ -77,21 +78,30 @@ class MainActivity : AppCompatActivity(), AlertDialogFragment.Callback {
7778 if (Build .VERSION .SDK_INT < Build .VERSION_CODES .TIRAMISU ) {
7879 throw InternalError (" Conflicted OS version." )
7980 }
81+ // 権限が必要な理由を提示したので、ランタイムパーミッションをリクエストする.
8082 logI(" Launch the request of the permission." )
8183 requestPermissionLauncher.launch(Manifest .permission.POST_NOTIFICATIONS )
8284 }
8385
86+ /* *
87+ * 通知権限が許可されているか確認する.
88+ * https://developer.android.com/training/permissions/requesting?hl=ja
89+ */
8490 private fun checkNotificationPermission (): Boolean {
91+ // バージョン確認. Android13 未満は不要.
8592 if (Build .VERSION .SDK_INT < Build .VERSION_CODES .TIRAMISU ) {
8693 logI(" No needed the permission." )
8794 return true
8895 }
96+ // 権限が許可されているか確認.
8997 if (ContextCompat .checkSelfPermission(
9098 this , Manifest .permission.POST_NOTIFICATIONS )
9199 == PackageManager .PERMISSION_GRANTED ) {
92100 logI(" The permission is granted already." )
93101 return true
94102 }
103+ // 権限を必要とする理由を提示する必要があるか確認.
104+ // 必要がある場合は、アプリ側がその理由を明示的に説明する.
95105 if (shouldShowRequestPermissionRationale(Manifest .permission.POST_NOTIFICATIONS )) {
96106 logI(" App should show the request of permission rationale." )
97107 AlertDialogFragment .newInstance(
@@ -101,6 +111,7 @@ class MainActivity : AppCompatActivity(), AlertDialogFragment.Callback {
101111 ).show(supportFragmentManager, null )
102112 return false
103113 }
114+ // 権限をリクエストする.
104115 logI(" Launch the request of the permission." )
105116 requestPermissionLauncher.launch(Manifest .permission.POST_NOTIFICATIONS )
106117 return false
0 commit comments