Learn How to Schedule a Server Code Script to Run Periodically to Send a Push Notification

Appery.io Server Code enables mobile developers to write any custom app logic that is automatically exposed via a REST API. You develop the script using JavaScript (which is nice, since you don’t have to learn a new language). Server Code provides built-in APIs to make it easy to integrate with other Appery.io services. For example, you can query the Appery.io Database, send a push notification, and invoke any 3rd-party REST API service.

For example, let’s say you want to develop a script that will query a products database. If an inventory for a particular product falls below a set number, you want to send a push message to a manager notifying him or her that inventory is low.

You probably want to automate this, rather than having to check manually. Running the script periodically is actually very simple once you have a finished script. In this post, I’m going to show you how to do just that–starting with the script:

var databaseId = "5625aaf.....";
var collectionName = "Products";
var result = {};

var params = {};
params.criteria = {
'quantity': {
"$lt": 5
}
};

result.query = Collection.query(databaseId, collectionName, params);

var msgText = [];
var productCount = result.query.length;
console.log(result.query);

if (productCount !== 0) {
for (var i = 0; i < productCount; i++) {
msgText.push(result.query[i].name + ": " + result.query[i].quantity);
}

PN.send("bb26439d.....", {
"message": msgText.toString();
});
}
response.success(result.query, "application/json");

Let’s review the script. It’s very simple:

  • Line 1 & 2: you set the database API key and the collection name.
  • Line 5: you set the query. This query will check if quantity for any product is less then 5.
  • Line 12: is where you query the database using the built-in Server Code API.
    • Because a number of products can have a low inventory, you iterate over the result and create the message to send.
  • Line 23: you send a push message. The ID is a Push API configured for an app that the manager installed.
  • At the end, the script returns a response.

You can test the script, but first, let’s look at the database so you know which product has low inventory. This is the database:

Screen Shot 2015-10-22 at 11.10.56 AM

Products database

Based on the query parameters on line 8, coffee has low inventory (less than 5). When you test the script, this is the result you will see:

Screen Shot 2015-10-22 at 11.15.12 AM

Testing the script

The result contains information about the product (coffee, which has low inventory).

You want this script to run periodically and automatically–say, once a day–without any user action. You can very easily schedule this script to run once a day via the Jobs tab.

Switch to the Jobs tab where you can schedule the script to run periodically:

Screen Shot 2015-10-22 at 11.19.44 AM

Scheduling a script to run periodically

At the top of the page you can configure the schedule. In the table  you will see all the scripts that have been scheduled. Use the toggle to set the schedule to run (or not). You can click on the link in Last result to see the the log trace for the last script execution.

Server Code enables you quickly to expose any app logic via a REST API. With Server Code Jobs, you can quickly schedule the script to run periodically.