Email subscribers
The subscribers in Notification service are used to provide information to the notification message receiver. The Notification service contract requires an ISubscriberRequest to subscribe or unsubscribe a recipient of the messages. The system provides a SubscriberRequestProxyclass that implements it.
For more information, see Working with subscriber's ID and ResolveKey.
Use the following sample to create a subscriber. The sample use ISubscriberRequest and ISubscriberResponse objects. In general, an ISubscriberRequest object contains all information needed for creating a subscriber. In case you want to work with an already persisted subscriber, you must use an ISubscriberResponse object, which contains all the information related to an already persisted subscriber.
using System;
using Telerik.Sitefinity.Services;
using Telerik.Sitefinity.Services.Notifications;
namespace Telerik.Sitefinity.Documentation.CodeSnippets.DeepDive.NotificationService
{
public partial class NotificationServiceSnippets
{
public static void CreateSubscriber()
{
var context = new ServiceContext("myNotificationAccount", "MyCustomModule");
var ns = SystemManager.GetNotificationService();
ISubscriberRequest subscriber = new SubscriberRequestProxy()
{
Email = "test.subscriber@company.com",
FirstName = "First",
LastName = "Last",
ResolveKey = "unique-identifier-in-context-of-MyCustomModule"
};
ISubscriberResponse persistedSubscriber = ns.CreateSubscriber(context, subscriber);
Guid subscriberId = persistedSubscriber.Id;
}
}
}
Working with subscriber's ID and ResolveKey
The Notification service provides GetSubscriber()methods that you can use to obtain a persisted subscriber bysubscriber.Id or by subscriber.ResolveKey.
There is a difference between subscriber.Id and by subscriber.ResolveKey. The subscriber.ResolveKeyis a unique identifier used by external systems to synchronize notification subscribers to external entities. For example, one can use a username or login ID for resolve key to match external system membership user entity. The ID of the subscriber is unique across the notification system and is generated when creating the subscriber – it cannot be set with the subscriber request.
How to get an existing subscriber by ID
The following sample shows how to get the existing subscriber by ID.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.Sitefinity.Services;
using Telerik.Sitefinity.Services.Notifications;
namespace Telerik.Sitefinity.Documentation.CodeSnippets.DeepDive.NotificationService
{
public partial class NotificationServiceSnippets
{
public static void GetSubscriberById()
{
var context = new ServiceContext("myNotificationAccount", "MyCustomModule");
var ns = SystemManager.GetNotificationService();
// Id of an existing subscriber
Guid subscriberId = Guid.Empty;
ns.GetSubscriber(context, subscriberId);
}
}
}
Getting an existing subscriber by ResolveKey
The following sample shows how to get the existing subscriber by ResolveKey.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.Sitefinity.Services;
using Telerik.Sitefinity.Services.Notifications;
namespace Telerik.Sitefinity.Documentation.CodeSnippets.DeepDive.NotificationService
{
public partial class NotificationServiceSnippets
{
public static void GetSubscriberByResolveKey()
{
var context = new ServiceContext("myNotificationAccount", "MyCustomModule");
var ns = SystemManager.GetNotificationService();
// ResolveKey of an existing subscriber, valid in the specified context
string subscriberResolveKey = "sample-resolve-key";
ns.GetSubscriber(context, subscriberResolveKey);
}
}
}