diff --git a/Sample-App/app/src/main/AndroidManifest.xml b/Sample-App/app/src/main/AndroidManifest.xml index 0c405e5..1a677ce 100644 --- a/Sample-App/app/src/main/AndroidManifest.xml +++ b/Sample-App/app/src/main/AndroidManifest.xml @@ -14,6 +14,7 @@ + = 33 (TIRAMISU) + 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. + Log.d(LOG_TAG, "Notification permission granted"); + Toast.makeText(this, "Notification permission granted", Toast.LENGTH_SHORT).show(); + } else if (shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS)) { + Log.d(LOG_TAG, "Notification Permission: Not granted"); + showNotificationPermissionRationale(); + } else { + // Directly ask for the permission + Log.d(LOG_TAG, "Requesting notification permission"); + requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS); + } + } else { + Log.d(LOG_TAG, "Notification permission granted"); + Toast.makeText(this, "Notification permission granted", Toast.LENGTH_SHORT).show(); + } + } + + private final ActivityResultLauncher requestPermissionLauncher = + registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> { + if (isGranted) { + // FCM SDK (and your app) can post notifications. + Log.d(LOG_TAG, "Notification permission granted"); + Toast.makeText(this, "Notification permission granted", Toast.LENGTH_SHORT).show(); + } else { + if (Build.VERSION.SDK_INT >= 33) { + if (shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS)) { + showNotificationPermissionRationale(); + } else { + Log.d(LOG_TAG, "Grant notification permission from settings"); + Toast.makeText(this, "Grant notification permission from settings", Toast.LENGTH_SHORT).show(); + } + } + } + }); + + + private void showNotificationPermissionRationale() { + new AlertDialog.Builder(this) + .setTitle("Grant notification permission") + .setMessage("Notification permission is required to show notifications") + .setPositiveButton("Ok", (dialog, which) -> { + if (Build.VERSION.SDK_INT >= 33) { + requestPermissionLauncher.launch(android.Manifest.permission.POST_NOTIFICATIONS); + } + }) + .setNegativeButton("Cancel", null) + .show(); + } }