Skip to content

Commit ce1dfb7

Browse files
committed
feat: demonstrate asking for post notification permission
1 parent 878f0e1 commit ce1dfb7

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-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
}

0 commit comments

Comments
 (0)