# Script to disable/enable Notification Center on macOS devices

Notification Center is a valuable tool in macOS devices that provides an overview of alerts and notifications, providing users timely updates. Although at times, Notification Center can be a major productivity hindrance disrupting the employees from focusing on their task at hand. With Hexnode, IT admins can now easily disable and enable the Notification Center using [Execute custom script](https://www.hexnode.com/mobile-device-management/help/how-to-run-scripts-on-mac-using-hexnode-mdm/) action.

This document contains sample code snippets that you can use to enable and disable the Notification Center on macOS devices.

Scripting Language – Bash

File extension – .sh

 Disclaimer:The sample scripts provided below are adapted from third-party open-source sites.

 

Disable Notification Center 
----------------------------

Execute the script to disable Notification Center for the currently logged-in user.

Script to disable Notification Center

\#!/bin/sh # Variable and function declarations export PATH=/usr/bin:/bin:/usr/sbin:/sbin # Get the currently logged in user currentUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ { print $3 }' ) # Global check if there is a user logged in if \[ -z "$currentUser" -o "$currentUser" = "loginwindow" \]; then echo "no user logged in, cannot proceed" exit 1 fi # Get the current user's UID uid=$(id -u "$currentUser") # Convenience function to run a command as the current user runAsUser() { if \[ "$currentUser" != "loginwindow" \]; then launchctl asuser "$uid" sudo -u "$currentUser" "$@" else echo "no user logged in" # uncomment the exit command # to make the function exit with an error when no user is logged in # exit 1 fi } # Disable Notification Center runAsUser launchctl unload -w /System/Library/LaunchAgents/com.apple.notificationcenterui.plist 

   1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

  \#!/bin/sh 

\# Variable and function declarations 

export PATH=/usr/bin:/bin:/usr/sbin:/sbin

\# Get the currently logged in user 

currentUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ { print $3 }' )

\# Global check if there is a user logged in 

if \[ -z "$currentUser" -o "$currentUser" = "loginwindow" \]; then

 echo "no user logged in, cannot proceed"

 exit 1

fi

\# Get the current user's UID 

uid=$(id -u "$currentUser")

\# Convenience function to run a command as the current user 

runAsUser() {

 if \[ "$currentUser" != "loginwindow" \]; then

 launchctl asuser "$uid" sudo -u "$currentUser" "$@"

 else

 echo "no user logged in"

 \# uncomment the exit command 

 \# to make the function exit with an error when no user is logged in 

 \# exit 1 

 fi

}

\# Disable Notification Center 

runAsUser launchctl unload -w /System/Library/LaunchAgents/com.apple.notificationcenterui.plist 

   

 

  

Enable Notification Center 
---------------------------

Execute the script to enable Notification Center for the currently logged-in user.

Script to enable Notification Center

\#!/bin/sh # variable and function declarations export PATH=/usr/bin:/bin:/usr/sbin:/sbin # get the currently logged in user currentUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ { print $3 }' ) # global check if there is a user logged in if \[ -z "$currentUser" -o "$currentUser" = "loginwindow" \]; then echo "no user logged in, cannot proceed" exit 1 fi # get the current user's UID uid=$(id -u "$currentUser") # convenience function to run a command as the current user runAsUser() { if \[ "$currentUser" != "loginwindow" \]; then launchctl asuser "$uid" sudo -u "$currentUser" "$@" else echo "no user logged in" # Uncomment the exit command to make the function exit with an error when no user is logged in # exit 1 fi } # Enable Notification Center runAsUser launchctl load -w /System/Library/LaunchAgents/com.apple.notificationcenterui.plist 

   1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

  \#!/bin/sh 

\# variable and function declarations 

export PATH=/usr/bin:/bin:/usr/sbin:/sbin

\# get the currently logged in user 

currentUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ { print $3 }' )

\# global check if there is a user logged in 

if \[ -z "$currentUser" -o "$currentUser" = "loginwindow" \]; then

 echo "no user logged in, cannot proceed"

 exit 1

fi

\# get the current user's UID 

uid=$(id -u "$currentUser")

\# convenience function to run a command as the current user 

runAsUser() {

 if \[ "$currentUser" != "loginwindow" \]; then

 launchctl asuser "$uid" sudo -u "$currentUser" "$@"

 else

 echo "no user logged in"

 \# Uncomment the exit command to make the function exit with an error when no user is logged in 

 \# exit 1 

 fi

}

\# Enable Notification Center 

runAsUser launchctl load -w /System/Library/LaunchAgents/com.apple.notificationcenterui.plist 

   

 

  

 Notes:- The scripts provided are particularly suitable for devices with SIP disabled, such as Macs used to install and test drivers, kernel extensions and other low-level code during app development.
- System Integrity Protection (SIP) needs to remain disabled for the above scripts to work. To turn off System Integrity Protection (SIP), restart your device in Recovery Mode and execute the *csrutil disable* command. After the Mac restarts, you can run the above scripts successfully.
- Keep in mind that if you go back to Recovery Mode and enable SIP, the Notification Center will appear again. To turn on System Integrity Protection (SIP), restart your device in Recovery Mode and execute the *csrutil enable* command.
- It is recommended to manually validate the script execution on a system before executing the action in bulk.
- Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.

 

 Warning:Disable SIP temporarily to toggle the Notification Center and reenable it as soon as possible. Failure to reenable SIP when you are done with your requirement leaves your device vulnerable to malicious code.