@@ -194,10 +194,34 @@ private void askNotificationPermission() {
194
194
Log .d (TAG , "Notification permission granted" );
195
195
} else {
196
196
Log .e (TAG , "Notification permission missing" );
197
- // Directly ask for the permission
198
- ActivityCompat .requestPermissions (
199
- this , new String [] {Manifest .permission .POST_NOTIFICATIONS }, 101 );
197
+
198
+ // Check if we should show an explanation (rationale)
199
+ if (ActivityCompat .shouldShowRequestPermissionRationale (this , Manifest .permission .POST_NOTIFICATIONS )) {
200
+ // If the permission was denied, show a Snackbar directing the user to settings
201
+ showPermissionSnackbar ();
202
+ } else {
203
+ // Directly ask for the permission
204
+ ActivityCompat .requestPermissions (
205
+ this , new String []{Manifest .permission .POST_NOTIFICATIONS }, 101 );
206
+ }
200
207
}
201
208
}
202
209
}
210
+
211
+ private void showPermissionSnackbar () {
212
+ // Create a Snackbar to inform the user and guide them to app settings
213
+ Snackbar snackbar = Snackbar .make (findViewById (android .R .id .content ),
214
+ "Notification permission is necessary please enable it in settings." ,
215
+ Snackbar .LENGTH_LONG )
216
+ .setAction ("Settings" , new View .OnClickListener () {
217
+ @ Override
218
+ public void onClick (View v ) {
219
+ // Open the app settings to allow the user to manually enable permissions
220
+ Intent intent = new Intent (Settings .ACTION_APPLICATION_DETAILS_SETTINGS );
221
+ intent .setData (Uri .fromParts ("package" , getPackageName (), null ));
222
+ startActivity (intent );
223
+ }
224
+ });
225
+ snackbar .show ();
226
+ }
203
227
}
0 commit comments