@@ -2,6 +2,7 @@ package com.google.firebase.example.messaging.kotlin
22
33import android.Manifest
44import android.content.pm.PackageManager
5+ import android.os.Build
56import android.os.Bundle
67import android.util.Log
78import android.widget.Toast
@@ -109,22 +110,22 @@ class MainActivity : AppCompatActivity() {
109110 }
110111 }
111112
112- // [START_EXCLUDE]
113- @RequiresApi(33 )
114- // [END_EXCLUDE]
115113 private fun askNotificationPermission () {
116- if (ContextCompat .checkSelfPermission(this , Manifest .permission.POST_NOTIFICATIONS ) ==
117- PackageManager .PERMISSION_GRANTED
118- ) {
119- // FCM SDK (and your app) can post notifications.
120- } else if (shouldShowRequestPermissionRationale(Manifest .permission.POST_NOTIFICATIONS )) {
121- // TODO: display an educational UI explaining to the user the features that will be enabled
122- // by them granting the POST_NOTIFICATION permission. This UI should provide the user
123- // "OK" and "No thanks" buttons. If the user selects "OK," directly request the permission.
124- // If the user selects "No thanks," allow the user to continue without notifications.
125- } else {
126- // Directly ask for the permission
127- requestPermissionLauncher.launch(Manifest .permission.POST_NOTIFICATIONS )
114+ // This is only necessary for API level >= 33 (TIRAMISU)
115+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ) {
116+ if (ContextCompat .checkSelfPermission(this , Manifest .permission.POST_NOTIFICATIONS ) ==
117+ PackageManager .PERMISSION_GRANTED
118+ ) {
119+ // FCM SDK (and your app) can post notifications.
120+ } else if (shouldShowRequestPermissionRationale(Manifest .permission.POST_NOTIFICATIONS )) {
121+ // TODO: display an educational UI explaining to the user the features that will be enabled
122+ // by them granting the POST_NOTIFICATION permission. This UI should provide the user
123+ // "OK" and "No thanks" buttons. If the user selects "OK," directly request the permission.
124+ // If the user selects "No thanks," allow the user to continue without notifications.
125+ } else {
126+ // Directly ask for the permission
127+ requestPermissionLauncher.launch(Manifest .permission.POST_NOTIFICATIONS )
128+ }
128129 }
129130 }
130131 // [END ask_post_notifications]
0 commit comments