using HaloPixelToolBox.Core.Models.Notifications; namespace HaloPixelToolBox.Core.Utilities; /// 从 Windows 通知的应用身份和结构化文本读取消息,不访问聊天客户端。 public static class WindowsMessageNotificationParser { public static MessageNotificationSource? GetSource(string appId, string displayName) { foreach (var (source, ids) in new (MessageNotificationSource, string[])[] { (MessageNotificationSource.WeChat, ["Tencent.WeChat", "Tencent.Weixin", "com.tencent.WeChat", "WeChat", "Weixin"]), (MessageNotificationSource.QQ, ["Tencent.QQ", "Tencent.QQNT", "com.tencent.qq", "QQ", "QQNT"]), (MessageNotificationSource.WeCom, ["Tencent.WeWork", "Tencent.WXWork", "com.tencent.WeWork", "WXWork", "WeCom"]) }) if (ids.Any(id => appId.Equals(id, StringComparison.OrdinalIgnoreCase) || (appId.StartsWith(id, StringComparison.OrdinalIgnoreCase) && appId.Length > id.Length && appId[id.Length] is '.' or '_' or '!'))) return source; return displayName.Trim() switch { "微信" or "WeChat" or "Weixin" => MessageNotificationSource.WeChat, "QQ" or "QQ NT" or "腾讯QQ" => MessageNotificationSource.QQ, "企业微信" or "WeCom" or "WXWork" => MessageNotificationSource.WeCom, _ => null }; } public static MessageNotification Parse(MessageNotificationSource source, IEnumerable textElements) { var texts = textElements.Select(text => text.Trim()).Where(text => text.Length > 0).ToList(); if (texts.Count > 1 && GetSource("", texts[0]) == source) texts.RemoveAt(0); if (texts.Count == 0) return new(source); if (texts.Count == 1) return MessageNotificationTextParser.Parse(source, texts); // Windows Toast 第一段是标题,后续段是正文;数字正文不能按未读角标丢弃。 var sender = texts[0]; var content = string.Join(" ", texts.Skip(1)); return new(source, sender, content); } }