Skip to content
This repository was archived by the owner on Oct 2, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Sample-App/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

<application
android:name=".MainApp"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package com.adobe.marketing.mobile.sampleapp;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MenuActivity extends AppCompatActivity implements View.OnClickListener {

Expand Down Expand Up @@ -46,6 +54,8 @@ protected void onCreate(Bundle savedInstanceState) {
btnEdgeIdentity.setOnClickListener(this);
btnConsent.setOnClickListener(this);
btnMessaging.setOnClickListener(this);

askNotificationPermission();
}

private void openMainActivity(int tab) {
Expand Down Expand Up @@ -73,4 +83,58 @@ public void onClick(View v) {
break;
}
}

private void askNotificationPermission() {
// This is only necessary for API level >= 33 (TIRAMISU)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it make sense to move this check to line 57 instead?

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<String> 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();
}
}