@@ -194,10 +194,34 @@ private void askNotificationPermission() {
194194        Log .d (TAG , "Notification permission granted" );
195195      } else  {
196196        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+           // Directly ask for the permission 
201+           ActivityCompat .requestPermissions (
202+                   this , new  String []{Manifest .permission .POST_NOTIFICATIONS }, 101 );
203+         } else  {
204+           // If the permission was denied twice or permanently, show a Snackbar directing the user to settings 
205+           showPermissionSnackbar ();
206+         }
200207      }
201208    }
202209  }
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+   }
203227}
0 commit comments