I have a requirement what I need to enable/disable the "Show notification" programmatically of App Info. I googled it for so much time but couldn't get proper solution. Is this possible in android to turn ON/OFF "Show notification" programmatically?. Thanks in advance.

            Asked
            
        
        
            Active
            
        
            Viewed 1.8k times
        
    4
            
            
        
        emrcftci
        
- 3,355
 - 3
 - 21
 - 35
 
        Vinod Pattanshetti
        
- 2,465
 - 3
 - 22
 - 36
 
- 
                    1do you mean enable/disable notifications only from your app or from the whole device? – OShiffer May 15 '17 at 06:53
 - 
                    1yes @OShiffer your right. – Vinod Pattanshetti May 15 '17 at 06:57
 - 
                    @VinodPattanshetti but what do you want? Only your apps notification or from the whole device? – Bruno Bieri May 15 '17 at 06:57
 - 
                    only app notifications @BrunoBieri. Like this http://stackoverflow.com/questions/17667406/programmatically-disabling-enabling-notifications. – Vinod Pattanshetti May 15 '17 at 06:58
 - 
                    @VinodPattanshetti have you followed the answers of this question? This one can help you: http://stackoverflow.com/a/9632076/1306012 – Bruno Bieri May 15 '17 at 07:02
 - 
                    http://stackoverflow.com/questions/9367196/detect-a-new-android-notification/9632076#9632076 This is different what I expected. – Vinod Pattanshetti May 15 '17 at 07:05
 - 
                    what service you are using to show notifications? alarm manager? – Pavel Poley May 15 '17 at 07:08
 - 
                    @PavelPoley see my requirement is to just How to TURN ON / OFF the show notification programmatically that's all, Nothing else I want. – Vinod Pattanshetti May 15 '17 at 07:10
 
4 Answers
5
            Actually, there is not way to turn on/off programatically notifications, the only way we can do this using following code.
public class CustomToast {
  private static final String CHECK_OP_NO_THROW = "checkOpNoThrow";
  private static final String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";
  public static void makeText(Context mContext, String message, int time) {
    if (isNotificationEnabled(mContext)) {
      //Show Toast message 
      Toast.makeText(mContext, message, time).show();
    } else {
      // Or show own custom alert dialog
      showCustomAlertDialog(mContext, message);
    }
  }
  private static boolean isNotificationEnabled(Context mContext) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
      AppOpsManager mAppOps = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
      ApplicationInfo appInfo = mContext.getApplicationInfo();
      String pkg = mContext.getApplicationContext().getPackageName();
      int uid = appInfo.uid;
      Class appOpsClass;
      try {
        appOpsClass = Class.forName(AppOpsManager.class.getName());
        Method checkOpNoThrowMethod =
            appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE, String.class);
        Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);
        int value = (int) opPostNotificationValue.get(Integer.class);
        return ((int) checkOpNoThrowMethod.invoke(mAppOps, value, uid,
            pkg) == AppOpsManager.MODE_ALLOWED);
      } catch (ClassNotFoundException | NoSuchMethodException | NoSuchFieldException
          | InvocationTargetException | IllegalAccessException ex) {
        Utils.logExceptionCrashLytics(ex);
      }
      return false;
    } else {
      return false;
    }
  }
  private static void showCustomAlertDialog(Context mContext, String message) {
      if (!(mContext instanceof Activity && ((Activity)mContext).isFinishing())) {
          AlertDialog.Builder mBuilder = new AlertDialog.Builder(mContext);
          mBuilder.setMessage(message);
          mBuilder.setPositiveButton(mContext.getString(R.string.ok),
                  new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                          dialog.dismiss();
                      }
                  });
          mBuilder.setCancelable(true);
          AlertDialog alertDialog = mBuilder.create();
          alertDialog.show();
      }
    }
}
        Vinod Pattanshetti
        
- 2,465
 - 3
 - 22
 - 36
 
1
            
            
        You would have to keep a Boolean flag and add a check before posting a notification from your app. Save that Boolean flag in SharedPreferences. Once the user or your app disables/enables notifications, make that reflect in the SharedPreferences.
Also, you could make a utility class to post notifications so that you don't have to add a check in a lot of different places.
public class NotificationUtil {
    public static void showNotification(
            Context context,
            int notificationId,
            int iconId,
            Class parentStackClass,
            String notificationTitle,
            String notificationText
    ) {
        boolean showNotification = PreferenceManager
                .getDefaultSharedPreferences(context)
                .getBoolean("SHOW_NOTIFICATION", true);
        if (!showNotification) return;
        android.support.v4.app.NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(iconId)
                        .setContentTitle(notificationTitle)
                        .setContentText(notificationText);
        Intent resultIntent = new Intent(context, parentStackClass);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addParentStack(parentStackClass);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(notificationId, mBuilder.build());
    }
}
        rdsarna
        
- 248
 - 2
 - 9
 
- 
                    please see my attached screenshot. There how can I enable/disable the Show Notifications programatically. – Vinod Pattanshetti May 15 '17 at 07:29
 
1
            
            
        We can't programatically turn on/ turn off notification. We can check the status of notification with below snippet
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
     AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
     ApplicationInfo appInfo = context.getApplicationInfo();
     String pkg = context.getApplicationContext().getPackageName();
     int uid = appInfo.uid;
     Class appOpsClass;
     try {
       appOpsClass = Class.forName(AppOpsManager.class.getName());
       Method checkOpNoThrowMethod =
           appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE, String.class);
       Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);
       int value = (int) opPostNotificationValue.get(Integer.class);
       return ((int) checkOpNoThrowMethod.invoke(mAppOps, value, uid,
           pkg) == AppOpsManager.MODE_ALLOWED);
     } catch (ClassNotFoundException | NoSuchMethodException | NoSuchFieldException
         | InvocationTargetException | IllegalAccessException ex) {
       Utils.logExceptionCrashLytics(ex);
     }
     // checked
   } else {
     // unchecked
   }
        Rocky
        
- 537
 - 4
 - 5
 
-4
            
            
        You can save it in SharedPreferences and every time you want to display a notification you can check it.
        Chetan Ashtivkar
        
- 194
 - 2
 - 15