Hi Hindie,
It sounds like you're trying to filter unknown senders. Here's an excerpt from the Block, filter, and report messages on iPhone resource that should help:
"With iMessage, you can filter messages from unknown senders, and you won’t get notifications from them.
Go to Settings
> Messages, scroll down to Message Filtering, then turn on Filter Unknown Senders.
When this setting is on, you can only see messages from people who aren’t in your contacts when you go to Filters > Unknown Senders.
Note: You canʼt open any links in a message from an unknown sender until you add the sender to your contacts or reply to the message."
Take care.