Send Safari Push Notifications to your Mac users using Sitecore – part 3

PushBrokerManager

Push notifications have been around for a while and we have all experienced them in our phones and tablets. Sending notifications to web users have also been possible but the browser and the website must be active.

But did you know that you can send push notifications to your web users even when the browser is closed? It’s called Safari Push Notifications and was presented when Apple released their latest OS – Maverick. It works on Safari running on Maverick – basically every Mac user.

In my previous post, Send Safari Push Notifications to your Mac users using Sitecore – part 1, I showed you guys how to prepare your website for Safari Push Notifications and store the the receivers/visitors in Sitecore.
In this post I will show you to push/send notifications by using the best CMS platform ever – Sitecore 🙂

In order to send push notifications we need the device token(receiver id – which was described in previous post) and a connection to APNS(Apple push notification server). There are a lot of push notification services who can help you with that, like Notification Hubs, Urban Airship, ZeroPush etc. But I wanted to use something that is more integrated to Sitecore and I found a very nice open source library PushSharp – A server-side library for sending Push Notifications to iOS (iPhone/iPad APNS), OSX (APNS 10.8+) Android (C2DM and GCM – Google Cloud Message), Chrome (GCM) Windows Phone, Windows 8, Blackberry (PAP), and Amazon (ADM) devices

So this what I did: I took PushSharp, integrated it with Sitecore and did a module of it – PushBroker Manager. The main idea is to make the PushBroker Manager work like the Email Experience(Campaign) Manager. Instead of sending emails it will send push notifications. It’s a long way to go so I will start with the basics like sending notifications, be able to hook up campaigns, do follow ups, do geofence notifications.

In order to send a push notification(using the PushBroker Manager) you need to create a campaign and map a market campaign to it(optional):
PushBrokerManagerCampaign

Next thing is to create the actual message(push notification)
PushBrokerManagerMessage

Add/Select the subscribers(receivers)
StoreReceiver

A scheduled task will send the notification to the subcsribers and move the notification message to the “Proccessed Messages” folder
ProccessedMessage

This is how the notification will be presented for the subscriber
AlertView
(Click on the “View” button will direct the user to the website and trigger a campaign)

Let’s show it in code instead 🙂
I will just take the part when the message will be send to the subscribers/receivers

public void SendWebMessages(IList<string> receiversIds, IMessage pushMessage, Campaign campaign)
{
            
    foreach (string receiver in receiversIds)
    {

        PushBrokerNotification pushBrokerNotification = new PushBrokerNotification()
            {
                CampaignId = campaign.Id,
                DeviceTokenId = receiver,
                PushMessageId = pushMessage.Id,
                LanguageCode =  languageCode
            };

        try
        {

              
            Push.QueueNotification(new AppleNotification()
                                .ForDeviceToken(receiver)
                                .WithPayload(new AppleSafariNotificationPayload(pushMessage.PushMessage.Name, pushMessage.PushMessage.Text, pushMessage.PushMessage.ButtonLabel, pushMessage.PushMessage.QueryParameters))
                                .WithTag(pushBrokerNotification)
                                );

              

        }
        catch (Exception ex)
        {
            Sitecore.Diagnostics.Log.Error(String.Concat("Error when trying to send to receiver:", receiver, ", pushmessage: ", pushMessage.Id, ", Campaign: ", campaign.Id), this);
        }
    }
}

There are some interesting events you can hook up on, for instance the PushOnOnNotificationSent. In this case if the message is successfully send I’ll set a “message send” date on the subscriber/receiver.

private void PushOnOnNotificationSent(object sender, INotification notification)
{
    PushBrokerNotification message = notification.Tag as PushBrokerNotification;

    if (message == null)
        return;

    Sitecore.Diagnostics.Log.Info(string.Format(CultureInfo.InvariantCulture,
                        "Notification {0} for device {1} has been sent successfully",
                        message.PushMessageId, message.DeviceTokenId), this);

    NotificationService notificationService = new NotificationService();
    notificationService.NotificationMessageSent(message.DeviceTokenId, message.PushMessageId, message.LanguageCode);
}

I will do a post on how the PushBroker Manager will work for the IOS apps 🙂
Hopefully the PushBroker Manager will be released soon on the Sitecore Market Place.

That’s all for now folks 🙂


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.