Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Dave Pearcey 46 posts 178 karma points
    Sep 25, 2017 @ 16:23
    Dave Pearcey
    0

    content service event intercepting

    Hey. I'm trying to send out web notifications whenever a new article is published for the frist time.

    I've got it for the most part. The notifications are sending, but there's a few things im missing that i can't work out.

    public class UmbracoEventInterceptor : ApplicationEventHandler
    {
        // Constructor method
        public UmbracoEventInterceptor()
        {
            //ContentService.Published += sendPushNotifications;
            ContentService.Publishing += sendPushNotifications;
        }
    
        private void sendPushNotifications(IPublishingStrategy sender, PublishEventArgs<IContent> args)
        {
            string sql = null;
    
            using (SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["umbracoDbDSN"].ConnectionString))
            {
                sql = "SELECT [Endpoint],[Key],[AuthSecret] FROM [dbo].[PushNotificationSubscriptions]";
                using (SqlCommand cmd = new SqlCommand(sql, cnn))
                {
                    cnn.Open();
                    DataSet dSet = new DataSet();
                    DataTable dt = new DataTable();
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
    
                    try
                    {
                        da.Fill(dSet);
                        dt = dSet.Tables[0];
    
                        //loop through each endpoint
                        foreach (DataRow a in dt.Rows)
                        {
                            //loop through each published article
                            foreach (var entity in args.PublishedEntities)
                            {
                                //only send notification if not already published and is an article
                                if (!entity.HasIdentity && !entity.HasPublishedVersion && entity.ContentType.Alias == "newsItem")
                                {
                                    PushSubscription subscription = new PushSubscription(a["Endpoint"].ToString(), a["Key"].ToString().Replace(" ", "+"), a["AuthSecret"].ToString().Replace(" ", "+"));
                                    string payload = "{\"Title\": \"New Article!\", \"Description\": \"" + entity.Name + "\", \"Url\": \"https://www.WEBSITENAME.com\"}";
                                    NotificationsController.createNotification(subscription, payload);
                                }
    
                            }
                        }
                    }
                    catch (SqlException)
                    {
                        throw;
                    }
    
                    if (cnn.State == ConnectionState.Open)
                        cnn.Close();
                }
            }
        }
    }
    

    Basically it just loops through a table in the database and sends out notifications to each endpoint.

    1. I need the URL for the newly published page, but cant access it yet. how can i, and still know that it hasn't been published before?
    2. once the notifications are sent, the function seems to just hang for an unknown reason. I dont get any errors come up either.
Please Sign in or register to post replies

Write your reply to:

Draft