本文将以一个场景讲述如果在 Dynamics CRM App 中使用 “消息提醒(Notification)”,我将使用三种推送方式为用户创建消息提醒:

  1. Cloud Flow
  2. Client API
  3. Csharp(C#)

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

开始之前,需要先在应用层面启用 In-App Notification 功能

操作路径如下:

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

启用消息通知 - 步骤1

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

启用消息通知 - 步骤2

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

启用消息通知 - 步骤3

如果配置成功,将在 Dataverse 的 Table 列表中看到新增的 Notification 表。

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

推送方式一: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

示例代码:

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"];
}

参考

  1. Send in-app notifications within model-driven apps

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

(完)