Unread Messages
The plugin provides a method to check the number of unread messages for the current visitor session using the Crisp REST API.
Usage
dart
int? unreadCount = await FlutterCrispChat.getUnreadMessageCount(
websiteId: 'YOUR_WEBSITE_ID',
identifier: 'YOUR_CRISP_API_IDENTIFIER',
key: 'YOUR_CRISP_API_KEY',
);
if (unreadCount != null && unreadCount > 0) {
print('You have $unreadCount unread messages.');
}1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
Parameters
| Parameter | Type | Description |
|---|---|---|
websiteId | String | Your Crisp Website ID |
identifier | String | Your Crisp REST API Identifier |
key | String | Your Crisp REST API Key |
Return Value
- Returns an
intwith the unread message count if successful - Returns
nullif no active session exists or an error occurs - Returns
0if the session exists but has no unread messages
Obtaining API Credentials
The identifier and key are not the same as your Crisp dashboard login. They are REST API credentials from the Crisp Marketplace.
Step-by-Step
- Go to the Crisp Marketplace
- Sign in or create an account (this is separate from your main Crisp account)
- Go to Plugins > click New Plugin
- Select Private as the plugin type
- Name your plugin (e.g., "My Flutter App") and click Create
- Go to the Tokens tab > scroll to Development Token
- Copy your
identifierandkey
Link Your Workspace
Before using the token, associate your marketplace account with your Crisp workspace:
- Go to Settings in your Crisp Marketplace account
- Click Add Trusted Workspace and submit your
website_id - Enter your main Crisp account credentials and 2FA token (if enabled)
Production Tokens
Development tokens have rate limits. For production use, obtain a production token from the Crisp Marketplace once your plugin is ready.
Example: Badge with Unread Count
dart
class ChatButton extends StatefulWidget {
const ChatButton({super.key});
@override
State<ChatButton> createState() => _ChatButtonState();
}
class _ChatButtonState extends State<ChatButton> {
int unreadCount = 0;
void _checkUnread() async {
int? count = await FlutterCrispChat.getUnreadMessageCount(
websiteId: 'YOUR_WEBSITE_ID',
identifier: 'YOUR_IDENTIFIER',
key: 'YOUR_KEY',
);
if (count != null && count > 0) {
setState(() => unreadCount = count);
}
}
@override
Widget build(BuildContext context) {
return Badge.count(
count: unreadCount,
isLabelVisible: unreadCount > 0,
child: ElevatedButton(
onPressed: _checkUnread,
child: const Text('Messages'),
),
);
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
How It Works
Internally, getUnreadMessageCount does the following:
- Calls
getSessionIdentifier()to get the current session ID - Makes a GET request to
https://api.crisp.chat/v1/website/{websiteId}/conversation/{sessionId} - Authenticates with Basic auth using your
identifier:key - Parses the
data.unread.visitorfield from the response
If there is no active session (e.g., the user hasn't opened the chat yet), the method returns null.
Next Steps
- Firebase Setup — Set up push notifications for your app
