on your Mac, choose Messages > Preferences, 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 is listed in your Contacts or is someone you previously received messages from.