Pushbullet + C/AL + JSON Buffer = Easy notifications on multiple platforms

UPDATE 07-07-2018: Now with SMS functionality added.

Sometimes you just want to push a notification or send an SMS the cheap way. Here is how you could do it in Dynamics NAV 2018 via C/AL with the use of Pushbullet, “JSON Buffer”, “JSON Management” and “HTTP Web Request Mgt”. This can pretty easily be converted to AL for use in Business Central.

In this example I will use Pushbullet – a cross-platform notification system which you can read more about here.

A quick summary of what you will see:

  • GET and POST calls to a HTTP Web service from C/AL.
  • Import JSON using Table 1236 JSON Buffer.
  • Export/generating JSON using Codeunit 5459 JSON Management.
  • Sending Pushbullet notifications from C/AL.

The Pushbullet API is well documented, compared to JSON Buffer and JSON Management (*Wink wink* Microsoft). Most of this is based on a lot of trial and error, sprinkled with Google.

There is a link to the entire code at the end. Today I will not post the Variables at the code snippets, most of them are self explaining anyway and this is more a quick rundown than a step-by-step tutorial. If you want, you can just import the object and then use this post as documentation on what the functionality is; see this as a framework to build upon.

Pushbullet requirements

If you do not have a Pushbullet account, just sign up for one as it is free (up til 100 messages a day). After that you need an access token, you can request a new one on the Account page by pressing the button below.

This will give you a key looking something like this o.tMVApnoTquzX0JKLa9A1yS23zK07j5V6, which you need to insert into the “Access Token” field on a record in the “Pushbullet Setup” table.
You need to install the Pushbullet app on a device if you want a notification, so either install it in a browser or on a mobile phone.

 

Setup

Create a table as the following

On Access Token set the following:

Add this helper function on the table, it fills in the extra information and checks if the email property is filled, which indicates if the access token was valid.

Helperfuntion

The code will request which devices the Access Token is allowed access to and store it in this temporary table.

We will only use a small part of this information, but I will be storing it all in the table for now, just to show you how to pull out different types of data from JSON. As with the variables, most of the fields are self-explaining.
Some of you might be specifically interested in the Has SMS field which shows if the selected device has SMS capabilities. This functionality is easy to implement by modifying my examples – you might even find a few hints in the code –SMS function is now added at the bottom, but for now I will just focus on sending to a specific device or to an account associated with a specific email address.

Make a page with the following design.

Setuptable

And add these actions.

Send buttons

 

Email push

Email is the easy one, so we will start with that.

As per the documentation:

Send the push to this email address. If the email address is associated with a Pushbullet user, we will send it directly to that user, otherwise we will fallback to send an email to the email address (this will also happen if a user exists but has no devices registered).

When you press the “Send to Email” action added above you will get to this page.

Send to Email page

Fill in the fields and press Send.

Send to Email action

This calls PushNoteToEmail function in Pushbullet Mgt codeunit. It creates the JSON the API requires as per the documentation.

{
"email": "[email protected]",
"body": "#MSDyn365BC can actually do this to.",
"title": "Hello from #MSDyn",
"type": "note"
}

For now you just skip the response from the web service, but actually you get a Push object in return. This can be used in more advanced scenarios if needed, we just “fire and forget” for now.

If the Access Token was added correctly the user should now receive a message.

Device Push

You can also send a notification to a specific device. To do this you need a bit more code. First you need to request all devices associated with the Access Token, then you need to select a device and finally add its ID to the JSON.

When coming from the action on the Pushbullet Setup page, you will arrive to this page.

Device Overview Page

It is filled with the devices, attached to this Token, through these functions.

This is the part which makes the actually HTTP call.

This converts the Blob into a string and then parses it into the JSON Buffer table.

Each device is added to the temporary table, so you can get the “iden/ID” to add as receiver.

Adds the devices to the table

Enter something into the fields, select a device and press Send.

This creates the JSON with the ID found above and makes a HTTP POST call to the Pushbullet API with the JSON as Body.

Again, if all went well, you should now receive a notification on the first active device on the list.

 

SMS

This will be for you to solve, I have included some hints in the code below.

First one that sends a working prototype will have his solution added here, with credits.

Here is how you send a SMS through Pushbullet. Call it from an action on the Pushbullet Devices page.
Code snippet of SMS func
Credit: Me 🙂

 

AL?

If you want, I can post the entire code converted to AL. Just post a comment and if more than 2 of you requests it, I will post it quickly afterwards. But as said in another post, I do not use AL when I prototype.

 

Get the code here.

Leave a Reply

Your email address will not be published. Required fields are marked *