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:18
    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. (hopefully only the one)
                            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.

    At the moment this is just a proof of concept. Any ideas for better implementations would be considered :)

  • Markus Johansson 1914 posts 5761 karma points MVP c-trib
    Sep 25, 2017 @ 20:23
    Markus Johansson
    1

    Hi!

    Have you tried to debug the code and step trough it? I'm thinking that this might give you some clues to why the code hangs.

    About your SQL-code you could use the PetaPoco-implementation in Umbraco to avid hardcoding the database-stuff:

    https://nddt-webdevelopment.de/umbraco/create-custom-database-table-umbraco-petapoco

    (Not at all necessary but could be fun =D)

  • Dave Pearcey 46 posts 178 karma points
    Sep 26, 2017 @ 07:53
    Dave Pearcey
    0

    I have tried stepping through it.

    The notification gets sent and then it gets stuck after the function ends. nothing comes up in the debugger to say there's an error or anything either.

    I was wondering if it was anything to do with not returning anything, but i can't see why that would be an issue.

    I'll have to keep trying and see if i cant get anything to pop up.

    Do you by chance know how it'd be possible for me to retrieve the articles URL at all, while still knowing that it's the first publish?

  • Markus Johansson 1914 posts 5761 karma points MVP c-trib
    Sep 26, 2017 @ 08:23
    Markus Johansson
    0

    Hi!

    I guess that you would need to hook into the Published-event to get the URL - not sure.

    But you would find the URL using entity.Url

    // m

  • Dave Pearcey 46 posts 178 karma points
    Sep 26, 2017 @ 08:26
    Dave Pearcey
    0

    The Url isn't in the published event. I read that it doesn't exist until the cache is written, which is after all the possible event hooks.

    Also, the Published event doesn't have any indication whether its a new item or not, as by this point it's already published and has an id and published state.

    edit:

    this is where i read about when the URL is created

    https://our.umbraco.org/forum/umbraco-7/using-umbraco-7/47922-Problem-With-V702-Events#comment-171543

Please Sign in or register to post replies

Write your reply to:

Draft