Answer:
Go to Settings -> Messages, scroll down to "Message Filtering" section and click into "Unknown & Spam". Enable "Filter Unknown Senders" and SMS Filtering.
Step-by-step explanation:
This should turn off iMessage notifications if the text comes from addresses that are not in your contacts, unless you have replied to it.