Skip to content

Commit 7fcf599

Browse files
authored
check API Level before requesting notifications permission (#369)
1 parent 86356ac commit 7fcf599

File tree

2 files changed

+30
-29
lines changed

2 files changed

+30
-29
lines changed

messaging/app/src/main/java/com/google/firebase/example/messaging/MainActivity.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -123,21 +123,21 @@ public void onComplete(@NonNull Task<String> task) {
123123
}
124124
});
125125

126-
// [START_EXCLUDE]
127-
@RequiresApi(33)
128-
// [END_EXCLUDE]
129126
private void askNotificationPermission() {
130-
if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) ==
131-
PackageManager.PERMISSION_GRANTED) {
132-
// FCM SDK (and your app) can post notifications.
133-
} else if (shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS)) {
134-
// TODO: display an educational UI explaining to the user the features that will be enabled
135-
// by them granting the POST_NOTIFICATION permission. This UI should provide the user
136-
// "OK" and "No thanks" buttons. If the user selects "OK," directly request the permission.
137-
// If the user selects "No thanks," allow the user to continue without notifications.
138-
} else {
139-
// Directly ask for the permission
140-
requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS);
127+
// This is only necessary for API level >= 33 (TIRAMISU)
128+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
129+
if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) ==
130+
PackageManager.PERMISSION_GRANTED) {
131+
// FCM SDK (and your app) can post notifications.
132+
} else if (shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS)) {
133+
// TODO: display an educational UI explaining to the user the features that will be enabled
134+
// by them granting the POST_NOTIFICATION permission. This UI should provide the user
135+
// "OK" and "No thanks" buttons. If the user selects "OK," directly request the permission.
136+
// If the user selects "No thanks," allow the user to continue without notifications.
137+
} else {
138+
// Directly ask for the permission
139+
requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS);
140+
}
141141
}
142142
}
143143
// [END ask_post_notifications]

messaging/app/src/main/java/com/google/firebase/example/messaging/kotlin/MainActivity.kt

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.google.firebase.example.messaging.kotlin
22

33
import android.Manifest
44
import android.content.pm.PackageManager
5+
import android.os.Build
56
import android.os.Bundle
67
import android.util.Log
78
import 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

Comments
 (0)