Skip to content

Commit 7127256

Browse files
authored
Merge pull request #354 from firebase/kroikie-request-notification-permission
feat: demonstrate asking for post notification permission
2 parents 878f0e1 + 51e6f8a commit 7127256

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
lines changed

messaging/app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'com.android.application'
22
apply plugin: 'kotlin-android'
33

44
android {
5-
compileSdkVersion 32
5+
compileSdkVersion 33
66

77
defaultConfig {
88
applicationId "com.google.firebase.example.messaging"

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package com.google.firebase.example.messaging;
22

3+
import android.Manifest;
34
import android.annotation.SuppressLint;
5+
import android.content.pm.PackageManager;
6+
import android.os.Build;
47
import android.os.Bundle;
58

69
import androidx.annotation.NonNull;
10+
import androidx.annotation.RequiresApi;
711
import androidx.appcompat.app.AppCompatActivity;
12+
import androidx.core.content.ContextCompat;
13+
814
import android.util.Log;
915
import android.widget.Toast;
1016

@@ -19,6 +25,7 @@
1925
public class MainActivity extends AppCompatActivity {
2026

2127
private static final String TAG = "MainActivity";
28+
private static final int NOTIFICATION_REQUEST_CODE = 1234;
2229

2330
@Override
2431
protected void onCreate(Bundle savedInstanceState) {
@@ -102,4 +109,41 @@ public void onComplete(@NonNull Task<String> task) {
102109
});
103110
// [END log_reg_token]
104111
}
112+
113+
@RequiresApi(33)
114+
// [START ask_post_notifications]
115+
private void askNotificationPermission() {
116+
if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) ==
117+
PackageManager.PERMISSION_GRANTED) {
118+
// FCM SDK (and your app) can post notifications.
119+
} else if (shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS)) {
120+
// TODO: display an educational UI explaining to the user the features that will be enabled
121+
// by them granting the POST_NOTIFICATION permission. This UI should provide the user
122+
// "OK" and "No thanks" buttons. If the user selects "OK," directly request the permission.
123+
// If the user selects "No thanks," allow the user to continue without notifications.
124+
} else {
125+
// Directly ask for the permission
126+
requestPermissions(new String[] { Manifest.permission.POST_NOTIFICATIONS }, NOTIFICATION_REQUEST_CODE);
127+
}
128+
}
129+
// [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]
105149
}

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package com.google.firebase.example.messaging.kotlin
22

3+
import android.Manifest
4+
import android.content.pm.PackageManager
35
import android.os.Bundle
46
import android.util.Log
57
import android.widget.Toast
8+
import androidx.annotation.RequiresApi
69
import androidx.appcompat.app.AppCompatActivity
10+
import androidx.core.content.ContextCompat
11+
import com.google.firebase.example.messaging.MainActivity
712
import com.google.firebase.ktx.Firebase
813
import com.google.firebase.messaging.ktx.messaging
914
import com.google.firebase.messaging.ktx.remoteMessage
@@ -13,6 +18,7 @@ class MainActivity : AppCompatActivity() {
1318

1419
companion object {
1520
private const val TAG = "MainActivity"
21+
private const val NOTIFICATION_REQUEST_CODE = 1234
1622
}
1723

1824
override fun onCreate(savedInstanceState: Bundle?) {
@@ -90,4 +96,40 @@ class MainActivity : AppCompatActivity() {
9096
// [END log_reg_token]
9197
}
9298

99+
@RequiresApi(33)
100+
// [START ask_post_notifications]
101+
private fun askNotificationPermission() {
102+
if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) ==
103+
PackageManager.PERMISSION_GRANTED
104+
) {
105+
// FCM SDK (and your app) can post notifications.
106+
} else if (shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS)) {
107+
// TODO: display an educational UI explaining to the user the features that will be enabled
108+
// by them granting the POST_NOTIFICATION permission. This UI should provide the user
109+
// "OK" and "No thanks" buttons. If the user selects "OK," directly request the permission.
110+
// If the user selects "No thanks," allow the user to continue without notifications.
111+
} else {
112+
// Directly ask for the permission
113+
requestPermissions(arrayOf(Manifest.permission.POST_NOTIFICATIONS), NOTIFICATION_REQUEST_CODE)
114+
}
115+
}
116+
// [END ask_post_notifications]
117+
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+
93135
}

0 commit comments

Comments
 (0)