I am doing one flutter-related course, where I am working on a messaging application and I now want to notify the current user about the new message, I have checked many videos, posts but nothing helped, or mainly there are mostly about group chats, but I want for one-on-one messaging app, just like WhatsApp, And I have this code but I don’t know how to show the notification and I do have fcm token inside the firebase firestore “users” collection for each user along with their other details.
body: Column(
children: [
Expanded(
child: StreamBuilder(
stream: _firestore
.collection('messages')
.doc(_auth.currentUser!.uid)
.collection(widget.clickedUserId)
.orderBy('timestamp', descending: true)
.snapshots(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(
child: CircularProgressIndicator(
color: orangeColor,
),
);
}
if (snapshot.hasError) {
return Center(
child: Text('Error: ${snapshot.error}'),
);
}
var messages = snapshot.data!.docs;
return ListView.builder(
reverse: true,
itemCount: messages.length,
itemBuilder: (context, index) {
var messageData = messages[index].data();
String message = messageData['text'];
String sender = messageData['sender'];
Timestamp timestamp =
messageData['timestamp'];
String status =
messageData['status'] ?? 'sent';
List<dynamic> seenBy =
messageData['seenBy'] ?? [];
bool isMe = sender == _auth.currentUser!.uid;
bool seen = seenBy.contains(_auth.currentUser!.uid);
if (!isMe && !seen) {
_markMessageAsSeen(messages[index].reference);
}
return MessageBubble(
message: message,
timestamp: timestamp,
isMe: isMe,
status: status,
seen: seen,
);
},
);
},
),
),
Align(
alignment: Alignment.bottomCenter,
child: Padding(
padding: const EdgeInsets.only(top: 8),
child: Container(
decoration: BoxDecoration(
border: const Border(
top: BorderSide(color: Color(0xff241D15), width: 2.0),
),
borderRadius: BorderRadius.circular(20.0),
),
child: Row(
children: [
Expanded(
child: Container(
padding: const EdgeInsets.symmetric(
horizontal: 12.0, vertical: 4),
child: TextField(
style: const TextStyle(
fontWeight: FontWeight.w500,
),
controller: _messageController,
cursorColor: const Color(0xffE66C37),
maxLines: 5,
minLines: 1,
textInputAction: TextInputAction.newline,
onChanged: (text) {
if (text.isNotEmpty) {
_startTyping();
} else {
_stopTyping();
}
},
decoration: const InputDecoration(
hintText: 'Type your message here...',
hintStyle: TextStyle(
fontFamily: 'Sen',
),
border: InputBorder.none,
),
),
),
),
IconButton(
icon: const Icon(Icons.send,
color: Color(0xffE66C37), size: 24),
onPressed: () {
_sendMessage();
},
),
],
),
),
),
),
],
),
);
}`
`void _sendMessage() async {
String text = _messageController.text.trim();
if (text.isEmpty) {
return;
}
_messageController.clear();
// Create an instance of ProfanityFilter
ProfanityFilter profanityFilter = ProfanityFilter();
// Check for profanity in the message
bool containsProfanity = profanityFilter.hasProfanity(text);
if (!containsProfanity) {
// Get the current timestamp
Timestamp timestamp = Timestamp.now();
// Update 'lastMessageTimestamp' for the sender
await FirebaseFirestore.instance
.collection('users')
.doc(_auth.currentUser!.uid)
.update({'lastMessageTimestamp': timestamp});
// Update 'lastMessageTimestamp' for the receiver
await FirebaseFirestore.instance
.collection('users')
.doc(widget.clickedUserId)
.update({'lastMessageTimestamp': timestamp});
// Add the message to the sender's collection
await _firestore
.collection('messages')
.doc(_auth.currentUser!.uid)
.collection(widget.clickedUserId)
.add({
'text': text,
'sender': _auth.currentUser!.uid,
'receiver': widget.clickedUserId,
'timestamp': timestamp,
'status': 'sent',
'seenBy': [],
});
// Add the message to the receiver's collection
await _firestore
.collection('messages')
.doc(widget.clickedUserId)
.collection(_auth.currentUser!.uid)
.add({
'text': text,
'sender': _auth.currentUser!.uid,
'receiver': widget.clickedUserId,
'timestamp': timestamp,
'status': 'sent',
'seenBy': [],
});
} else {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Your message contains inappropriate content.'),
),
);
}
_stopTyping();
}
I want that when receiver sends the message the message to the current user or the sender then we want to notify the user, or in short whenever any user receives any new message we wanna notify the user about it.