Notification System in Duke-CSA App

OK, today is the day my JFLAP program ends, so I have a whole evening to write this big post about how I’m implementing notifications in Duke-CSA app.

First I’ll talk about the functionalities I achieved and then about how I implemented them.


  1. notification initialization

Notifications can be triggered by two circumstances: 1. A user replies/answers questions by another user in QA, replies/comments Rendezvous by another user etc. 2. A new event is pushed by CSA to all users.

The former kind of notifications is created by client apps which call a function in the cloud code to push. The latter, however, is created by an event poster that I created using Parse JavaScript SDK and calls the same cloud function to push.

2. notification presentation

The following table shows how notifications are handled. Rows are the states of the app and columns are how users open the app.

Click on Notification Click on App icon
not running go to corresponding view show badges and red dots
background go to corresponding view go to corresponding view
foreground N/A show badges and red dots

Red colored entry is undesired behavior that I have not figured out how to fix.

3. notification indication

Whenever there is an unread reply/answer etc. The corresponding tab will have a badge showing how many unread notifications that view and its subviews have. Red dots are also shown on the TableViewCells. The badge number will decrement and the dot will disappear whenever the user clicks on unread posts.



  • push notifications

I’m using Parse Cloud Code to perform the pushing tasks to Apple Push Notifications (APN) and call this function from any client app. The cloud code looks like this:

Parse.Cloud.define("push", function (request, response) {
    var query = new Parse.Query(Parse.Installation);
    var targetUser;
    var toUser = request.params.toUser; // this is the id of target user
    if (request.user && == toUser) {
    if (toUser) {
        targetUser = new Parse.User(); = toUser; 
        query.equalTo('user', targetUser);

        where: query,
    }, {
        useMasterKey: true,
        success: function () {
            if (targetUser) {
                saveNotifDataForUser(targetUser, request, response);
            else {
                saveNotifDataForAll(request, response);
        error: function (error) {
            response.error("Error! " + error.message);

Basically, whenever there is a “toUser” variable in the request sent to the cloud, I push the notification to the user using a query and save the notification to the database (more about this later). If there is nothing in “toUser”, it means the request wants to push to all users. The program will then save the notifications for all.

  • save notifications

Below’s what “saveNotifDataForUser” looks like. I’m gonna save the code for “saveNotifDataForAll” because it’s very similar.

function saveNotifDataForUser (targetUser, request, response) {
    var NotifData = Parse.Object.extend("NotifData");
    var qry = new Parse.Query(NotifData);
    var type =;
    var instanceID =;
    qry.find().then( function (results) {
        var notifData;

        if (results.length == 0) {
            notifData = new NotifData();
            for (var i = 0; i < notifTypes.length; i++) {
                notifData.set(notifTypes[i], []);
        } else {
            notifData = results[0];

        var notifOfType = notifData.get(type);
        if (!notifOfType.includes(instanceID)) {
    }).then( function (notifData) {
        console.log("user notif data saved.");
        response.success("Successfully pushed notification to " + + " with type " + type + " and id " + instanceID);

Here I’m creating a new instance of “NotifData” class and save relevant information in there. Let me explain a little bit about this class. I came up with the idea of storing notification data online when I was trying to find a way to show badges on tab bar items when users click on the app icon to open the app instead of the notification. If I store notifications to the online database and retrieve them every time app is not launched from a notification. I can correctly update the notification data.

Besides this online class, I’m also storing notifications locally in persistent storage because I don’t want to lose them when users close the app without reading new notifications. For this storage, I classified notifications in categories and saved the arrays of IDs of PFInstances. Variable List:

var events: [String] = []
var rendezvous: [String] = []
var questions: [String] = []
var answers: [String] = []
var ansQuestions: [String] = []
var newEvents: [String] = []

Whenever a new post/reply is read, I remove its ID from the array and save again.

  • retrieve notifications

I’m retrieving notifications in the method didFinishLaunchingWithOptions. If “launchOption” is not nil, then it means the app is launched by the user clicking on the notification. With this notification I am able to create the local storage notification class and go from there. However, when “launchOption” is nil, I have to retrieve from the database the notifications sent to the user when the app was not active. I wrote a cloud function for this:

Parse.Cloud.define("getNotifData", function (request, response) {
    var userID = request.params.userID;
    var NotifData = Parse.Object.extend("NotifData");
    var query = new Parse.Query(NotifData);
    query.equalTo("UserID", userID);
    query.find().then( function(results) {
        if (results.length == 0) {
            response.error("No notification data for this user found.");
        var notifData = results[0];
        var result = [];
        for (var i = 0; i < notifTypes.length; i++) {
            var instances = notifData.get(notifTypes[i]);
            for (var j = 0; j < instances.length; j++) {
                var notification = {"notifType": notifTypes[i], "PFInstanceID": instances[j]};

So client apps will just call this function to retrieve the notification data for the current users. After each time of retrieving, local storage should take over so I’m calling another cloud function to wipe the notification data. The code is farely similar to “getNotifData”.

This function is VERY tricky to use. This function is called when:

  1. the app is in the foreground and receives a notification, called twice.
  2. the app is in the background and receives a notification, called once. Note that this function is called in background only when the “remote notification” background mode is enabled in the capabilities of the app.
  • Event Poster

This is a tool to push events to the app and also all users. A demo video is below:

Basically it’s a lot of reading docs and playing around with html forms.


Seems like only I understand what I wrote….

A little more notes about debugging/coding:

  1. I’m using Postman as a fantastic tool to send post requests to Parse server. I can also export the requests in a json file so my friend Jay can also test the app.
  2. I find that the Parse.Promise class is very useful for getting rid of block structures wrapping each other.
  3. Moving functions to cloud is nice. In this way even if things go wrong after the app ships I have a chance to fix them.
  4. Vim is the best editor.
  5. I’m very excited that back4app (Parse Server group) promised to release a command line tool for their servers. I won’t have to click on many buttons to deploy cloud code any more. They also said we would be able to run local Parse servers and debug locally.
  6. The Event Poster is not using any kind of authentication. Literally anyone can post to the database. I’m hoping no one would be boring enough to go there and post random things.

Comments are closed.