Monday, 21 August 2017

OfficeDev: Don't build your own notification engine, use the exiting one

blur, close-up, code
If you take a look at the Office 365 top navigation bar, you will notice a nice notification bell next to the gear icon. I've seen a lot of developers trying to re-invent the wheel and build their own notification user interface which eventually becomes very confusing for the end users. to be honest I've done that in the past but on SharePoint 2013 on premises and my excuse was there was no "out of the box" notification engine.

let's first take a step back and see how notification works in Office 365 "to be more precise in outlook web access".

If we take a sneak peek into the request sent while a native Office 365 page is loading you will find this interesting request to
outlook notification endpoint

Which describes a single type of notification performed by outlook web app (pulling notification). If you look more carefully into the requests sent you will also find a subscribe to notification POST request. Interestingly enough, it subscribe to five different types of notification events:
POST
  • HierarchyNotification
  • ReminderNotification
  • NewMailNotification
  • SocialActivityNotification
  • SuiteNotification
And a sixth one,  with prefix "RowNotification trailed by a base64 encoded string which seems to be guid encoded (to be honest I didn't spend anytime trying to figure out what this guid represents)

To understand the various subscription models in outlook there is a great article at https://msdn.microsoft.com/en-us/library/office/dn458791(v=exchg.150).aspx , a more recent read also can be found at https://msdn.microsoft.com/en-us/office/office365/api/notify-rest-operations

In order to use the built-in notification engine in Office 365 we need to create a new entity of the which the Office 365 web interface already subscribed to. However, we don't want to create a lot of unnecessary noise by having our notification objects lurking as annoying meeting request or emails.


After a quick thinking, I've chosen to create the notification as an outlook task, the good news is Microsoft graph already has a beta endpoint enables you to create an outlook task

 The trick here as you can guys see , is to enable reminder by setting two value isReminderOn to true and to set the reminderDateTime if you do that when the reminder time approaches you will have a nice popup notification using the native notification engine.
The task notification will appear in the second div in the notification roaster which is reserved to the reminders (both events and tasks)


The mechanics 

 Basically a periodic  post request to the url https://outlook.office365.com/owa/ev.owa2?ns=PendingRequest&ev=PendingNotificationRequest&UA=0 is being triggered to get any notification the outlook web app subscribed to using an earlier POST request



When a change occurs specially in the case of our task a subsequent call to https://outlook.office365.com/owa/service.svc?action=GetReminders&UA=0 to get the actual reminder items ,which will be renders as below
and Voila! you can use outlook tasks as custom notification item with no front end customization at all

Related Articles