///
/// Event handler for button Click event.
///
///
///
private void btnStartNotifications_Click(object sender, EventArgs e)
{
ReceiveNotifications();
}
///
/// Use RingCentral C# Framework to access RingCentral API and request Subscription notifications.
///
private async void ReceiveNotifications()
{
try
{
// By default client talks to sandbox server (for production use "true" as third argument or actual server string e.g. "https://platform.devtest.ringcentral.com")
RestClient rc = new RestClient(ringCentralAppKey, ringCentralAppSecret);
// Authorization - If you use direct number as username, leave extension empty.
await rc.Authorize(ringCentralUserName, "", ringCentralPassword);
// Account and extension
var account = rc.Restapi().Account();
var extension = rc.Restapi().Account().Extension();
// Create subscription to receive notifications
var subscription = rc.Restapi().Subscription().New();
rc.Restapi().Account().Extension().MessageStore(ringCentralAppKey);
// Set filters for subscription
subscription.EventFilters.Add("/restapi/v1.0/account/~/extension/~/message-store");
subscription.EventFilters.Add("/restapi/v1.0/account/~/extension/~/presence?detailedTelephonyState=true");
// Subscription Connect event handler
subscription.ConnectEvent += ConnectHandler;
// Subscription Notification event handler
subscription.NotificationEvent += NotificationHandler;
// Subscription Error event handler
subscription.ErrorEvent += ErrorHandler;
// Register subscription
bool isRegistered = await subscription.Register();
// Display registration status for subscription service
txtNotifications.Text = "Subscription to Notification Success: " + isRegistered.ToString();
}
catch (Exception ex)
{
txtNotifications.Text = "ERROR: " + Environment.NewLine + ex.Message + ((ex.InnerException != null) ? ex.InnerException.Message : "");
if (ex.Message != null)
{
Console.WriteLine("EXCEPTION / ERROR OCCURED: ");
Console.WriteLine(ex.Message);
if (ex.InnerException != null)
{
Console.WriteLine("MORE DETAILS: ");
Console.WriteLine(ex.InnerException.Message);
}
}
}
}
///
/// Handler method for Notifications returned from Subscription service.
///
///
///
private void NotificationHandler(object sender, NotificatioEventArgs e)
{
txtNotifications.Text += "Notification: " + e.notification.ToString() + Environment.NewLine;
Console.WriteLine("Notification: " + e.notification);
}
///
/// Handler method for Errors returned from Subscription service.
///
///
///
private void ErrorHandler(object sender, SubscriptionEventArgs e)
{
txtNotifications.Text += "Error: " + e.Message + Environment.NewLine;
Console.WriteLine("Error: " + e.Message);
}
///
/// Handler method for Connection with Subscription service.
///
///
///
private void ConnectHandler(object sender, SubscriptionEventArgs e)
{
txtNotifications.Text += "Connected: " + e.Message + Environment.NewLine;
Console.WriteLine("Connected: " + e.Message);
}