In the Part 4 of this blog series you showed user’s achievements in the profile page. In this blog post you will show notifications from the game.

It is important to provide immediate feedback on achievements to the users. Here you will use pop-up messages to notify the users about Experience Points assigned to him for creating issues.

In order to implement this, first you will retrieve notifications using gamification service API and then you will show the relevant notification by creating a pop-up message.


1) Retrieve notifications:


1. Replace the code in the file GamificationProxy.java located at package com.sap.gamification.plugins.servlet with code GamificationProxy.txt.zip provided as the additional file to this blog. Unzip the file and copy-and-paste the contents to the file GamificationProxy.java.

The class GamificationProxy

The class GamificationProxy has a method getPlayerNotifications() which uses Technical Endpoint to retrieve notifications by invoking gamification service method getNotificationsForPlayer(). The HTTP POST request will look like the following:


https://<gamification service host>/gamification/api/tech/JsonRPC?json={“method”:”getNotificationsForPlayer”,params”:[“JIRA_USER@mail.com“,1442061177178]}&app=JIRA

Query parameter

Value

json

method getNotificationsForPlayer() and parameters (retrieves notifications for given player which have been created after the provided timestamp)

app

JIRA (name of the app which contains the game mechanics as defined in the gamification workbench)

Input parameter

Description

JIRA_USER

Player name/Player email id

1442061177178

Timestamp (to retrieve notifications created just after this)

The class Gamification Proxy has another method getServerTime() which uses Technical Endpoint to retrieve current server time by invoking gamification method getCurrentServerTime(). The HTTP POST request will look like the following:

https://<gamification service host>/gamification/api/tech/JsonRPC?

json= {“method”:”getCurrentServerTime”,”params”:[ ]} &app=JIRA

Query parameter

Value

json

method getCurrentServerTime() and parameters (retrieves the current server time )

app

JIRA (name of the app which contains the game mechanics as defined in the gamification workbench)

Test the class GamificationProxy: 

The class GamificationProxy defines a method main() to test retrieving notifications using gamification service API.

public static void main(String arg[]) throws Exception{

   // arg[0] – player id

   //reading Environments values

   EnvironmentUtility.readEnvironmentValues();

   GamificationProxy gamificationProxy= new GamificationProxy();

   // Step 1: Getting the server time

   String serverTimeResp=gamificationProxy.getSeverTime();

   String serverTime = serverTimeResp.split(“,”)[0].split(“:”)[1];

   System.out.println(“Server Time “+ serverTime);

   // Step 2: Sending ‘issueCreated’ event to the gamification service    

   String eventName=”issueCreated”;

   gamificationProxy.sendEvent(arg[0], “Major”, eventName);

   //Step 3: Retrieving notifications created after the provided server time

   String notifications = gamificationProxy. getPlayerNotifications (

   arg[0],serverTime);

   System.out.print(notifications);

}

1. Run the configuration (which you created in the previous blog) to execute the class GamificationProxy. As a result following output will be displayed in console

Server Time 1442061177178

{“result”:[{“id”:672384,”message”:”Ticket created”,”category”:”POINT“,”type”:”ADD“,”subject”:”Experience Points”,”detail”:”2″,”dateCreated”:1442061178360}],”error”:null,”type”:null}

Now let’s see how this was achieved. In step 1 server time is calculated and in step 2 gamification relevant event issueCreated is sent. Step 3 retrieves the notifications created after the provided server time and in the current case only one event is sent which means only one notification is created.

Create Rest service for notifications

1. Add the following lines of code to the method doGet() of class UserAchievementsServlet.

if (request.getPathInfo().equalsIgnoreCase(“/servertime”)) {

      String serverTime = null;

      try {

              serverTime = gamificationProxy.getSeverTime();

      }  catch (Exception e) {

                log.error(” Error during accessing server time”, e.getMessage());

      }

      log.info(“Server time response”, serverTime);

      response.getWriter().println(serverTime);

}

else if (request.getPathInfo().equalsIgnoreCase(“/notifications”)) {

      String serverTime = request.getParameter(“serverTime”);

      String playerNotifications = null;

        try {

                  playerNotifications=gamificationProxy.getPlayerNotifications(playerName,serverTime);

          } catch (Exception e) {

                  log.error(” Error during accessing player notifications”,e.getMessage());

        }

      log.info(“Player’s notification response”, playerNotifications);

      response.getWriter().println(playerNotifications);

}

The doGet() method should look like the following:

/wp-content/uploads/2015/10/1_802530.png

2.) Show notification:

To show notification, you need to create a JavaScript file and embed it in JIRA pages.


1. Create a folder with name widgets in folder /src/main/resources as shown in the screenshot below

/wp-content/uploads/2015/10/2_802531.png


2. Create a file with name sap_gs_notifications.js and place it at folder location /src/main/resources/widgets. Substitute the code from sap_gs_notifications.txt.zip provided as the additional file to this blog. Unzip the file and copy-and-paste the contents to the file sap_gs_notifications.js.

The web resource Plugin Module:


The web resource Plugin Module allows JIRA plugins to define downloadable resources. If your plugin requires the application to serve additional static Javascript or CSS files, you will need to use downloadable web resources to make them available.


In the current case, JavaScript file sap_gs_notifications.js will be used as downloadable web resource to shows notification exactly the time when an issue is created in the JIRA. Now let’s understand how it works?


The file sap_gs_notifications.js has functions _getNotificationsForPlayer() and _getCurrentServerTime() which retrieves notifications and server time by making Ajax calls to the REST service.

The function _poll() schedule the repeating execution of these functions to continually check whether any gamification relevant event has occurred for which there is a notification to the user. If there are notifications they will be shown on the screen using the function _prepareAndInjectNotifications().


_prepareAndInjectNotifications() uses following JIRA API to show notification message

  JIRA.Messages.showSuccessMsg(message);

Embed the JavaScript file in JIRA pages


1. Add the following web-resource Plugin module in the atlassian-plugin element of jira-gamification-plugin/atlassian-plugin.xml.

<web-resource name=”Resources” key=”resources”>

  <resource name=”sap_gs_notifications.js” type=”download” location=”widgets/sap_gs_notifications.js”/>

    <context>atl.general</context>

</web-resource>

Note:

Here, web resource context is atl.general which means the widget sap_gs_notifications.js will be included on all screens of JIRA except administration screens.

The atlassian-plugin.xml should look like:

/wp-content/uploads/2015/10/3_802547.png

Test Integration with JIRA:

1. Open a command prompt and run the following set of commands to set environment variables.

set user_name=<HANA Cloud Platform user name>

set user_password=<HANA Cloud Platform password>

set host = < gamification service host>

If you are running this application behind the firewall you also need to set http_proxy_host and http_proxy_port by using following commands.

set http_proxy_host= <Http proxy name> 

set http_proxy_port= <Http port number>

2. Change directory to the folder jira-gamification-plugin and run the following command

atlas-run

3. Once JIRA has started successfully, open the URL printed in the message.

/wp-content/uploads/2015/10/5_802548.png

4. Log in with username JIRA_USER@mail.com and create an issue as described in the previous blog and notification would be visible as shown in the snapshot below

/wp-content/uploads/2015/10/6_802549.png

Congratulation!! 🙂 You are done with Part 5 and with this integration of SAP HCP gamification service with JIRA is complete 🙂 🙂 .  Next blog post Part 6 you will introduce the advance game mechanics like levels, badges, leaderboard and these elements will be visible in the User profile page.

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply