如何按组向特定用户发送通知?我唯一的工作代码适用于所有移动用户。
我能够从控制台发送通知
await hubConnection.InvokeAsync("SendMessage", "Sample notification.");
我的中心:
public async Task SendMessage(string message)
{
await Clients.Others.SendAsync("ReceiveMessage", message);
}
毛伊岛后台服务:
hubConnection.On<string>("ReceiveMessage", (message) =>
{
// Display the message in a notification
BadgeNumber++;
notification.SetNumber(BadgeNumber);
notification.SetContentTitle(message);
StartForeground(myId, notification.Build());
});
在我的应用程序中,消息发送者在消息中包含组 ID。聊天中心从消息中提取群组 ID 并将其发送到请求的群组:
等待 Clients.Group(group).SendAsync("MessageReceived", message);
大多数消息都会发送给群组中的每个人。但我确实在发送给单个收件人的消息中包含了 ID 字段,而这些消息会被其他人忽略。