Home > Blockchain >  How I can send a message to only the specific user and when the user login can see the message in me
How I can send a message to only the specific user and when the user login can see the message in me

Time:02-05

I have create contact form the admin to send messages to any user, and when the user access his account they can then view the messages in the messages screen.

Here is my contact form code:

const Contact =()=>{

    const [name,setName] = useState('')
    const [email, setEmail] = useState('')
    const [message, setMessage] = useState('')
    const [sendTime, setSendTime] = useState('')


  function renderForm(){
        return(
            <View>
                 {/*Name Input*/}
               <FormInput 
                     label='Name:'
                     value={name}
                     onChange={(value)=>{
                        setName(value)
                }}  
               />

                   {/*Email Input*/}
                   <FormInput 
                     label='Email:'
                     keyboardType='email-address'
                     value={email}
                     onChange={(value)=>{
                        setEmail(value)
                }}
               />

                   {/*message Input*/}
                   <FormInput 
                     label='Message:'
                     placeholder='Type something'
                     multiline={true}
                     numberOfLines={10}
                     inputStyle={{height:100,}}
                     value={message}
                     onChange={(value)=>{
                        setMessage(value)
                        
                }}
               
               />


               {/*Send Button*/}
               <View>
                 <AppButton
                    label='Send'
                    icon={icons.send}
                    disabled={!isEnableDonate()}
                    onPress={sendMessage}
                 />
              </View>    
            </View>
        )

    }

           {/*send Message to the specific user function*/}
    const sendMessage = async()=>{

        setLoader(true)

        db.collection('messages').add({
            userId:auth.currentUser?.uid,
            name:name,
            email:email,
            message:message,
            sendTime:sendTime
        })
        .then(()=>{
            Alert.alert('Message has been sent to the user successfully')
            setLoader(false)
        })
        .catch((error)=>{
            Alert.alert(error.message)
            setLoader(false)
        })

        setName('')
        setEmail('')
        setMessage('')
    }

So when the user access his account can see the message in the messages screen.

Here is my message screen code:

import {db} from '../../firebase'; 
const MessagesScreen = ()=>{
     
    const message = db.collection('messages')
    const [userData, setUserData] = useState([]);
     
    {/*fetch messages which send to the current user by admin*/}

    const fetchData = async()=>{
        try {
            const list=[];
             await message
            .get()
            .then((querySnapshot)=>{
                querySnapshot.forEach((doc)=>{
                    const {userId,name,email,message,} = doc.data()
                    list.push({
                        userId,
                        name,
                        email,
                        message,
                     
                        
                    })
                })
    
            })
            setUserData(list)
            if(loading){
                setLoading(false)
            }
        } catch (error) {
            
        }
    }
    useEffect(()=>{
        fetchData()
    },[])

       return(
            <ScrollView>
                 
            <FlatList 
               data={userData}
               numColumns={1}
               renderItem={({item})=>(

                      <View>
                       <Text>Message:</Text>
                       <Text>{item.name}</Text>
                      </View>                     
             )}
             />
            </ScrollView>
           
        )
   

Right now admin can send message via contact form and add all messages saves in Messages Collections. However, what I'm trying to do is that I want the admin only send message to the specific user and user can view the message in the Message Screen.

CodePudding user response:

What you can do is to create a field in each message document called for example recipient and then get all messages where the logged in user matches that field:

const message = db.collection('messages').where("recipient","==",userData.uid)

This will fetch only messages that is meant for the current logged in user

Example

As admin when you create a message, might have the following collection message:

{
 text: "some text for my message",
 recipient: "uidA"
}

So as admin you create a new message like:

// Message for user with id *uidA*
db.collection('messages').add({
{
     text: "some text for my message",
     recipient: "uidA" //Id of intented user
    }
})

 // Message for user with id *uidB*
db.collection('messages').add({
    {
         text: "some text for my message",
         recipient: "uidB" //Id of intented user
        }
    })

Now as a user you can only access your intended messages with the following query

const message = db.collection('messages').where("recipient","==",userData.uid

Here userA will only access messsages where recipient is equal their userID

  • Related