Skip to content

Commit e0a93fa

Browse files
authored
refactor(fcm): let the system manage the POST_NOTIFICATIONS Request Code (#357)
1 parent 7127256 commit e0a93fa

File tree

2 files changed

+32
-40
lines changed

2 files changed

+32
-40
lines changed

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

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import android.os.Build;
77
import android.os.Bundle;
88

9+
import androidx.activity.result.ActivityResultLauncher;
10+
import androidx.activity.result.contract.ActivityResultContracts;
911
import androidx.annotation.NonNull;
1012
import androidx.annotation.RequiresApi;
1113
import androidx.appcompat.app.AppCompatActivity;
@@ -110,8 +112,20 @@ public void onComplete(@NonNull Task<String> task) {
110112
// [END log_reg_token]
111113
}
112114

113-
@RequiresApi(33)
114115
// [START ask_post_notifications]
116+
// Declare the launcher at the top of your Activity/Fragment:
117+
private final ActivityResultLauncher<String> requestPermissionLauncher =
118+
registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
119+
if (isGranted) {
120+
// FCM SDK (and your app) can post notifications.
121+
} else {
122+
// TODO: Inform user that that your app will not show notifications.
123+
}
124+
});
125+
126+
// [START_EXCLUDE]
127+
@RequiresApi(33)
128+
// [END_EXCLUDE]
115129
private void askNotificationPermission() {
116130
if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) ==
117131
PackageManager.PERMISSION_GRANTED) {
@@ -123,27 +137,8 @@ private void askNotificationPermission() {
123137
// If the user selects "No thanks," allow the user to continue without notifications.
124138
} else {
125139
// Directly ask for the permission
126-
requestPermissions(new String[] { Manifest.permission.POST_NOTIFICATIONS }, NOTIFICATION_REQUEST_CODE);
140+
requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS);
127141
}
128142
}
129143
// [END ask_post_notifications]
130-
131-
// [START handle_ask_post_notifications_request]
132-
@Override
133-
public void onRequestPermissionsResult(int requestCode, String[] permissions,
134-
int[] grantResults) {
135-
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
136-
switch (requestCode) {
137-
case NOTIFICATION_REQUEST_CODE:
138-
// If request is cancelled, the result arrays are empty.
139-
if (grantResults.length > 0 &&
140-
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
141-
// FCM SDK (and your app) can post notifications.
142-
} else {
143-
// TODO: Inform user that that your app will not show notifications.
144-
}
145-
return;
146-
}
147-
}
148-
// [END handle_ask_post_notifications_request]
149144
}

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

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.content.pm.PackageManager
55
import android.os.Bundle
66
import android.util.Log
77
import android.widget.Toast
8+
import androidx.activity.result.contract.ActivityResultContracts
89
import androidx.annotation.RequiresApi
910
import androidx.appcompat.app.AppCompatActivity
1011
import androidx.core.content.ContextCompat
@@ -96,8 +97,21 @@ class MainActivity : AppCompatActivity() {
9697
// [END log_reg_token]
9798
}
9899

99-
@RequiresApi(33)
100100
// [START ask_post_notifications]
101+
// Declare the launcher at the top of your Activity/Fragment:
102+
private val requestPermissionLauncher = registerForActivityResult(
103+
ActivityResultContracts.RequestPermission()
104+
) { isGranted: Boolean ->
105+
if (isGranted) {
106+
// FCM SDK (and your app) can post notifications.
107+
} else {
108+
// TODO: Inform user that that your app will not show notifications.
109+
}
110+
}
111+
112+
// [START_EXCLUDE]
113+
@RequiresApi(33)
114+
// [END_EXCLUDE]
101115
private fun askNotificationPermission() {
102116
if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) ==
103117
PackageManager.PERMISSION_GRANTED
@@ -110,26 +124,9 @@ class MainActivity : AppCompatActivity() {
110124
// If the user selects "No thanks," allow the user to continue without notifications.
111125
} else {
112126
// Directly ask for the permission
113-
requestPermissions(arrayOf(Manifest.permission.POST_NOTIFICATIONS), NOTIFICATION_REQUEST_CODE)
127+
requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
114128
}
115129
}
116130
// [END ask_post_notifications]
117131

118-
// [START handle_ask_post_notifications_request]
119-
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
120-
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
121-
when (requestCode) {
122-
NOTIFICATION_REQUEST_CODE -> {
123-
// If request is cancelled, the result arrays are empty.
124-
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
125-
// FCM SDK (and your app) can post notifications.
126-
} else {
127-
// TODO: Inform user that that your app will not show notifications.
128-
}
129-
return
130-
}
131-
}
132-
}
133-
// [END handle_ask_post_notifications_request]
134-
135132
}

0 commit comments

Comments
 (0)