创建和发送通知

已完成

要创建通知,需要定义数据类型通知的变量。 可在数据类型通知的变量上使用的一些属性和函数包括:

  • ID - 指定通知的标识符。

  • Message - 指定 UI 中显示的通知的内容。

  • Scope - 指定通知显示的范围。 不需要指定本属性,因为默认值为 LocalScope

  • Send - 发送要由客户端显示的通知。

  • AddAction - 针对通知添加操作。 使用 AddAction 属性,可以在另一个 codeunit 中运行特定方法。

  • SetData - 为通知设置数据属性值。

  • GetData - 从通知获取数据属性值。

  • Recall - 撤回已发送的通知。

如果指定 ID,则可以使用 Recall 函数撤回本通知。 使用 ID 还将确保具有相同 ID 的通知仅在列表中显示一次。

要发送基本通知,需要提供消息并使用 Send 函数。 请注意,对于 Message 函数,不能像在常规 Message 语句上一样使用占位符。 如果要使用占位符,则需要使用 StrSubstNo 函数将占位符替换为相应的值。

var 
   MyNotification: Notification;
begin
   MyNotification.Id('2f511c20-b456-4894-8472-4eed76826c65');
   MyNotification.Message('Here comes your notification text.');
   MyNotification.Send();
end;

还可以向通知添加操作。 本功能允许用户选择操作并在 codeunit 中运行函数。 要发送带有通知的操作,请使用 AddAction 函数。

MyNotification.AddAction(Caption, CodeUnitID, MethodName);

以下示例通过使用 AddAction 函数对上一个示例进行了更改。

var
    MyNotification: Notification;
    ShowDetailsTxt: Label 'Show details';
begin
    MyNotification.Id('2f511c20-b456-4894-8472-4eed76826c65');
    MyNotification.Message('Here comes your notification text.');
    MyNotification.AddAction(ShowDetailsTxt, Codeunit::"Notif Mgmt", 'ShowInfo');
    MyNotification.Send();
end;

以下示例显示如何在具有通知数据类型参数的另一个 codeunit 中创建全局函数。 可以将代码添加到该函数以处理该操作。

codeunit 50102 "Notif Mgmt"
{
    procedure ShowInfo(MyNotification: Notification)
    begin
        Page.Run(Page::"Customer List");
    end;
}

还可以将数据传递到 Codeunit 函数。 因此,无法将数据传递为参数,但需要使用 SetDataGetData 函数将数据添加到通知。 SetDataGetData 函数将数据定义为键/值对。 键/值对只能接受文本数据类型。 如果要传递 DateTime 值或整数,则首先需要为该值设置格式。

var
    MyNotification: Notification;
    ShowDetailsTxt: Label 'Show details';
begin
    MyNotification.Id('2f511c20-b456-4894-8472-4eed76826c65');
    MyNotification.Message('Here comes your notification text.');
    MyNotification.SetData('Created', Format(CurrentDateTime, 0, 9));
    MyNotification.SetData('CustomerNo', '10000');
    MyNotification.AddAction(ShowDetailsTxt, Codeunit::"Notif Mgmt", 'ShowInfo');
    MyNotification.Send();
end;

GetData 函数使用密钥再次检索数据。

codeunit 50102 "Custom Notification Management"
{
    procedure ShowInfo(MyNotification: Notification)
    var
        Customer: Record Customer;
        CustomerNo: Text;
        Created: DateTime;
    begin
        CustomerNo := MyNotification.GetData('CustomerNo');
        Evaluate(Created, MyNotification.GetData('Created'));

        if Customer.Get(CustomerNo) then
            Page.Run(Page::"Customer Card", Customer);
    end;
}