From 01b4094f7e95c7cdff1d0c602d18bdadd74fe8b2 Mon Sep 17 00:00:00 2001 From: spoorthipujariadobe Date: Mon, 13 Mar 2023 13:05:16 -0700 Subject: [PATCH 1/2] add runtime notification permissions for Android 13 --- Sample-App/app/src/main/AndroidManifest.xml | 1 + .../mobile/sampleapp/MenuActivity.java | 64 +++++++++++++++++++ 2 files changed, 65 insertions(+) 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 { + 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(); + } } From 70c5d5aed36f95d9108162ea83660671b9d25c02 Mon Sep 17 00:00:00 2001 From: spoorthipujariadobe Date: Mon, 13 Mar 2023 13:36:39 -0700 Subject: [PATCH 2/2] formatting fixes --- .../java/com/adobe/marketing/mobile/sampleapp/MenuActivity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sample-App/app/src/main/java/com/adobe/marketing/mobile/sampleapp/MenuActivity.java b/Sample-App/app/src/main/java/com/adobe/marketing/mobile/sampleapp/MenuActivity.java index f475c67..c395752 100644 --- a/Sample-App/app/src/main/java/com/adobe/marketing/mobile/sampleapp/MenuActivity.java +++ b/Sample-App/app/src/main/java/com/adobe/marketing/mobile/sampleapp/MenuActivity.java @@ -84,7 +84,6 @@ public void onClick(View v) { } } - private void askNotificationPermission() { // This is only necessary for API level >= 33 (TIRAMISU) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { @@ -118,6 +117,7 @@ private void askNotificationPermission() { 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(); } }