Reference channels
Channel referencing lets users mention channels in messages by typing # followed by at least three letters. Matching channel names appear as suggestions.
Requires App Context
Enable App Context for your keyset in the Admin Portal.
Generic referencing
Channel references, user mentions, and links are MessageElement instances with different MentionTarget types.
Suggestions include all channels in the app keyset (up to 100), regardless of user membership. Configure up to 100 channel references per message (default: 10) and display them as links.
Implementation is similar to user mentions and links.
Add channel references
Add channel references with # followed by at least three letters of the channel name (e.g., #Sup). Use CreateChannelMentionTarget() to create a mention target linking to a specified Channel.
Method signature
Add a channel reference by calling AddMention() with the MentionTarget of type Channel.
Refer to the AddMention() method for details.
Sample code
Create the Hello Alex! I have sent you this link on the #offtopic channel. message where #offtopic is a channel reference (channel name is offtopic).
1// Create a message draft
2UPubnubMessageDraft* MyMessageDraft = Channel->CreateMessageDraft();
3
4// Insert the whole text to the message draft
5MyMessageDraft->InsertText(0, "Hello Alex! I have sent you this link on the #offtopic channel.");
6
7// Create a channel mention target for #offtopic
8FString ChannelName = "offtopic";
9UPubnubMentionTarget* ChannelMentionTarget = UPubnubMentionTarget::CreateChannelMentionTarget(ChannelName);
10
11// Add the channel mention for #offtopic - '#offtopic' is mention of 'offtopic' channel
12MyMessageDraft->AddMention(45, ChannelName.Len() +1, ChannelMentionTarget);
13// 45 is Possition of # in the text.
14// ChannelName.Len() +1 because we add # to the reference
15
show all 17 linesRemove channel references
Remove a channel reference from a draft message with RemoveMention().
Method signature
Remove channel references by calling RemoveMention() at the exact offset where the reference starts.
Refer to the RemoveMention() method for details.
Offset value
Provide the exact position of the first character; otherwise the element is not removed.
Sample code
Remove the channel reference from the Hello Alex! I have sent you this link on the #offtopic channel. message where #offtopic is a channel reference.
1// Assume the message reads
2// Hello Alex! I have sent you this link on the #offtopic channel.`
3// Remove the channel reference for "#offtopic"
4MessageDraft->RemoveMention(45)
Get channel suggestions
The message elements listener returns matching channels from your keyset for the 3-letter string typed after #.
Typing #Sup returns channels like Support or Support-Agents. Default: 10 suggestions (max: 100).
Method signature
Refer to the Add a message draft listener section for details.
Sample code
Insert the first referenced channel whenever the draft is updated and channels are detected.
1// Assuming you have a UPubnubMessageDraft pointer named MyMessageDraft
2
3// Define your callback function to handle updates with suggestions
4void AMyActor::OnMessageDraftUpdateWithSuggestions(const TArray<UPubnubMessageElement*>& MessageElements, const TArray<FPubnubSuggestedMention>& SuggestedMentions)
5{
6 // Check if there are any suggested mentions
7 if (SuggestedMentions.Num() == 0)
8 {
9 return;
10 }
11
12 // Iterate over suggested mentions to find a channel reference
13 for (const FPubnubSuggestedMention& Suggestion : SuggestedMentions)
14 {
15 // Check if the suggestion corresponds to a channel reference
show all 32 linesGet referenced channels
ReferencedChannels() returns all channel names referenced in a message.
Method signature
- Blueprint
- C++ / Input parameters
1Message->ReferencedChannels();
Output
| Type | Description |
|---|---|
TArray<FPubnubReferencedChannel> | Returned array of referenced channel (FString ID, FString Name) objects. |
Sample code
Check if a message on the support channel contains any channel references.
1#include "Kismet/GameplayStatics.h"
2#include "PubnubChatSubsystem.h"
3
4UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
5UPubnubChatSubsystem* PubnubChatSubsystem = GameInstance->GetSubsystem<UPubnubChatSubsystem>();
6
7UPubnubChat* Chat = PubnubChatSubsystem ->InitChat("demo", "demo", "my_user");
8
9UPubnubChannel* Channel = Chat->GetChannel("support");
10
11FString Timetoken = "17228457425416757";
12
13// Fetch the message
14UPubnubMessage* Message = Channel->GetMessage(Timetoken);
15
show all 16 lines