在日常的 Dynamics 365 项目中,经常会遇到这样的问题:
当某些关键数据发生变化时,如何在系统内第一时间提醒相关用户?

相比邮件或 Teams 消息,应用内消息提醒(In-App Notification) 更轻量,在有些场景还是蛮好用的。本文记录了我在 D365 中启用站内信功能,并通过 3 种方式向用户推送通知的完整实践。

本文示例统一围绕一个简单但常见的场景:

当 Account 的负责人(Owner)发生变更时,向新负责人发送应用内通知

前置条件:启用应用内消息通知功能

在使用 In-App Notification 之前,需要先在 应用层级 启用该功能,否则后续代码或 Flow 都无法生效

基本操作路径如下:

Power Apps → 点击菜单栏中的 App → 选择目标应用 → 点击 “三个点” → 选择 编辑(Edit)

启用消息通知 - 步骤1

在应用编辑页面顶部,点击 Settings 按钮

启用消息通知 - 步骤2

切换到 Features 选项卡,启用 In App Notification,并保存设置。

启用消息通知 - 步骤3

启用成功后,可以在 Dataverse 表列表中看到新增的 Notification(appnotification) 表,这也是后续所有通知的基础

启用消息通知 - 新增了Notification实体

推送方式一:Cloud Flow(低代码)

如果团队偏向低代码,或者通知逻辑主要基于数据变更事件,Cloud Flow 是成本最低、维护最友好的选择

创建流程路径:

New → Automation → Cloud flow → Automated

新建Cloud Flow - 步骤1

为 Flow 输入一个有意义的名称,然后通过模糊搜索(输入 Dataverse),选择触发器:

When a row is added, modified or deleted

点击 Create 创建流程。

新建Cloud Flow - 步骤2

触发器配置示例

在 Flow 设计器中,配置 When a row is added, modified or deleted 触发器,然后点击 + New step 继续后续步骤

本示例中的配置如下:

  • Change type:Modified
  • Table name:Accounts
  • Scope:Organization
  • Select columns:ownerid

配置Flow

该配置示例用于监听 Account 记录的负责人(owner)发生变化的场景,后续即可基于该事件向用户发送应用内通知

推送方式二:Client API

如果希望在 表单、按钮或前端逻辑中即时发送通知 时,Client API 会更加灵活

下面是一个简单示例,通过 Xrm.WebApi.createRecord 直接创建 appnotification:

async function sendInAppNotification() {
  let globalContext = Xrm.Utility.getGlobalContext();
  const userId = globalContext.userSettings.userId.replace(/[{}]/g, "");
  const inAppNotification = {
    title: "负责人变更提醒",
    body: "你已被设置为该客户的负责人,请及时查看。",
    "ownerid@odata.bind": "/systemusers(" + userId + ")",
    icontype: 100000000,
    toasttype: 200000000,
  };
  try {
    await Xrm.WebApi.createRecord("appnotification", inAppNotification);
    console.log("In-App Notification sent successfully.");
  } catch (error) {
    console.error("Failed to send notification:", error.message);
  }
}

Client Api Notification

Icon Type Value
Info 100000000
Success 100000001
Failure 100000002
Warning 100000003
Mention 100000004

推送方式三:Csharp(C#)

示例代码:

/// <summary>
/// Example of SendAppNotification with formatted content
/// </summary>
/// <param name="service">Authenticated client implementing the IOrganizationService interface</param>
/// <param name="userId">The Id of the user to send the notification to.</param>
/// <returns>The app notification id</returns>
public static Guid SendAppNotificationWithFormattedContent(IOrganizationService service, Guid userId)
{

    var request = new OrganizationRequest()
    {
        RequestName = "SendAppNotification",
        Parameters = new ParameterCollection
        {
            ["Title"] = "Complete overhaul required (sample)",
            ["Recipient"] = new EntityReference("systemuser", userId),
            ["Body"] = "Maria Campbell mentioned you in a post.",
            ["IconType"] = new OptionSetValue(100000004), //mention
            ["OverrideContent"] = new Entity()
            {
                Attributes =
                {
                    ["title"] = "[Complete overhaul required (sample)](?pagetype=entityrecord&etn=incident&id=0a9f62a8-90df-e311-9565-a45d36fc5fe8)",
                    ["body"] = "[Maria Campbell](?pagetype=entityrecord&etn=contact&id=43m770h2-6567-ebm1-ob2b-000d3ac3kd6c) mentioned you in a post: _\"**[@Paul](?pagetype=entityrecord&etn=contact&id=03f770b2-6567-eb11-bb2b-000d3ac2be4d)** we need to prioritize this overdue case, [@Robert](?pagetype=entityrecord&etn=contact&id=73f970b2-6567-eb11-bb2b-000d3ac2se4h) will work with you to engage with engineering team ASAP.\"_"
                }
            }
        }
    };

    OrganizationResponse response = service.Execute(request);
    return (Guid)response.Results["NotificationId"];
}

参考

如果本文对你有所帮助,可以请我喝杯咖啡

(完)