Last time I showed how to configure a basic notification with an analysis in code. You may have noticed that the only link between the notification and the analysis was via the notification's criteria. The criteria specified a filter that said "only notify me when the event frame is from this analysis". There is a good reason for this. It makes the triggering more flexible and open to other tools generating event frames. We can then easily configure notifications to trigger for these events. Here's a short example that shows the configuration based on only a name filter:

 

        private static void DemoCreateNotification(string afServerName, string emailAddress)
        {
            var systems = new PISystems();
            var system = systems[afServerName];
            var db = system.Databases["notificationtestdb"] ?? system.Databases.Add("notificationtestdb");


            // create an element with an attribute
            var element = db.Elements["NotificationsExample"];
            if (element == null)
            {
                Console.WriteLine("Creating element");
                element = db.Elements.Add("NotificationsExample");
            }


            // Configure the trigger for the notification rule
            Console.WriteLine("Creating notification");
            var notificationRule = element.NotificationRules.Add("MyNotification*");
            notificationRule.Criteria = $"Name:\"Downtime*\"";
            notificationRule.SetStatus(AFStatus.Enabled);


            var testEmail = system.NotificationContactTemplates["TestEmail"];
            if (testEmail == null)
            {
                Console.WriteLine("Creating TestEmail endpoint");
                testEmail = new AFNotificationContactTemplate(system, "TestEmail");
                testEmail.DeliveryChannelPlugIn = system.DeliveryChannelPlugIns["Email"];
                testEmail.DeliveryChannel.ConfigString = $"ToEmail={emailAddress}";
                testEmail.CheckIn();
            }


            // Add a subscription to a notification.
            notificationRule.Subscribers.Add(testEmail);


            db.CheckIn();
            Console.WriteLine("Finished creation");


            Console.WriteLine("Hit enter to trigger the notification");
            Console.ReadLine();


            // let's trigger the notification here.
            var st = AFTime.Now;
            var ef = new AFEventFrame(db, "Downtime #1");
            ef.PrimaryReferencedElement = element;
            ef.SetStartTime(st);
            ef.CheckIn();
            Console.WriteLine($"Notification triggered at {st}");


            Console.WriteLine("Done");
        }

 

The important part here is line 20, where we set the criteria to Name:"Downtime*". This means match any event frame (for this element) whose name starts with Downtime. The other valid filter criteria you can mix in are: Category, Template, and Value filters. See the AFSDK documentation for more details.

 

At the end we are triggering the notification to send simply by creating the an event frame that matches our filter.