Choose Messages > Settings, click General, then select (or deselect) “Notify me when my name is mentioned.”
When this option is unselected, you get a notification only if you’re mentioned by someone who’s in your contacts or is someone you previously received messages from.
See Manage downtime in Screen Time on Mac