Push Notifications: To Create a Custom Destination Type
The following activity shows you how to create a custom destination type for push notifications.
Story
Suppose that predefined types of push notification destinations that are available in MYOB Acumatica (which are webhook, message queue, SignalR hub, and commerce push destination) do not satisfy the requirements of your integration application. You need to create a custom type in code.
Process Overview
In a project of your MYOB Acumatica extension library, you will define a class that implements the PX.PushNotifications.NotificationSenders.IPushNotificationSender interface and a class that implements the PX.PushNotifications.NotificationSenders.IPushNotificationSenderFactory interface.
System Preparation
Before you begin performing the steps of this activity, do the following:
- Prepare an MYOB Acumatica instance with any predefined dataset, as described in the Instance Deployment: To Deploy an Instance with Demo Data prerequisite activity.
- Create a customization project, as specified in the Customization Projects: To Create a Customization Project prerequisite activity.
- Create an extension library, as described in the To Create an Extension Library prerequisite activity.
Step: Creating a Custom Destination Type
To create a custom destination type, do the following:
- In a project of your MYOB Acumatica extension library, define a class that implements the PX.PushNotifications.NotificationSenders.IPushNotificationSender interface, which is a sender of push notifications.
- In the class that implements the IPushNotificationSender
interface, implement the following methods and properties of the interface:
- The Address property, which is the address to which
the system should send notifications. A user specifies the value of this
property in the Address box on the Push Notifications (SM302000) form. The property uses the following
syntax.
string Address { get; }
- The Name property, which is the name of the
notification destination. A user specifies the value of this property in
the Destination Name box on the Push Notifications (SM302000) form. Use the following syntax for the
property.
string Name { get; }
- The Send method, which sends a notification
synchronously and uses as the parameters the notification to be sent and
a cancellation token. The method uses the following
syntax.
void Send( NotificationResultWrapper results, CancellationToken cancellationToken );
- The SendAndForget method, which sends a notification
without blocking the current thread. We recommend that you use
HostingEnvironment.QueueBackgroundWorkItem in the
method implementation to delegate the execution to a parallel thread.
The following code shows a sample implementation of the
method.
using System; using System.Threading; using PX.PushNotifications; using PX.PushNotifications.NotificationSenders; public void SendAndForget( NotificationResultWrapper result, CancellationToken cancellationToken, Action onSendingFailed, Action finalizer) { try { Send(result, cancellationToken); } catch (Exception e) { onSendingFailed($"Send to target {Name} failed: ({e.Message})"); } finally { finalizer(); } }
- The Address property, which is the address to which
the system should send notifications. A user specifies the value of this
property in the Address box on the Push Notifications (SM302000) form. The property uses the following
syntax.
- Define a class that implements the PX.PushNotifications.NotificationSenders.IPushNotificationSenderFactory interface, which creates a sender of push notifications.
- In the class that implements the
IPushNotificationSenderFactory interface, implement the
following methods and properties of the interface:
- The Create method, which creates a sender and uses as
the parameters the destination address, the name of the notification
destination, and the additional parameters (such as a header for an HTTP
address). Use the following syntax for the
method.
IPushNotificationSender Create( string address, string name, IDictionary<string, object> additionalParameters );
- The Type property, which is a
string
identifier of the destination type that is exactly four characters long. The value of this property is stored in the database. The property uses the following syntax.string Type { get; }
- The TypeDescription property, which is a
string
label of the destination type. A user selects the value of this property in the Destination Type box on the Push Notifications (SM302000) form. Use the following syntax for the property.string TypeDescription { get; }
- The Create method, which creates a sender and uses as
the parameters the destination address, the name of the notification
destination, and the additional parameters (such as a header for an HTTP
address). Use the following syntax for the
method.
- Build the project of your MYOB Acumatica extension library.
- Run MYOB Acumatica and test the new destination type.