Saturday, March 6, 2021

AWS SNS - How to publish Platform specific payload

 Below is a screen from AWS SNS which helps to do that 



Sample messges formats are


{ "GCM":"{ \"notification\": { \"body\": \"Sample message for Android endpoints\", \"title\":\"TitleTest\" } }" }


{ "GCM":"{ \"notification\": { \"body\": \"Sample message for Android endpoints\", \"title\":\"TitleTest\" }, \"data\": {\"time_to_live\": 3600,\"collapse_key\":\"deals\"}" } }


{ "default": "This is the default message which must be present when publishing a message to a topic. The default message will only be used if a message is not present for one of the notification platforms.", "APNS": "{\"aps\":{\"alert\": \"Check out these awesome deals!\",\"url\":\"www.amazon.com\"} }", "GCM": "{\"data\":{\"message\":\"Check out these awesome deals!\",\"url\":\"www.amazon.com\"}}", "ADM": "{\"data\":{\"message\":\"Check out these awesome deals!\",\"url\":\"www.amazon.com\"}}" }

references:

https://docs.aws.amazon.com/sns/latest/dg/sns-send-custom-platform-specific-payloads-mobile-devices.html

Monday, March 1, 2021

How to document function callbacks using JSDoc

JSDoc 3 has a @callback tag for exactly this purpose. Here's a usage example:


/**

 * Callback for adding two numbers.

 *

 * @callback addStuffCallback

 * @param {int} sum - An integer.

 */


/**

 * Add two numbers together, then pass the results to a callback function.

 *

 * @param {int} x - An integer.

 * @param {int} y - An integer.

 * @param {addStuffCallback} callback - A callback to run.

 */

function addStuff(x, y, callback) {

  callback(x+y);

}



References:

https://stackoverflow.com/questions/24214962/whats-the-proper-way-to-document-callbacks-with-jsdoc

Sunday, February 28, 2021

JSDoc disable source code references:

If you use the default template, then use a configuration file with templates.default.outputSourceFiles set to false. For instance:


{

    "templates": {

        "default": {

            "outputSourceFiles": false

        }

    }

}


You'd pass the path to this file to jsdoc using the -c option on the command line.


The template is the entity providing this functionality so if you use another template than the default one, you have to check its documentation to find whether there is a setting you can set to do this.


References:

https://stackoverflow.com/questions/20907501/how-can-i-omit-the-source-links-in-jsdoc

JSDoc for Javascript Documentation

There are many ways for documenting a piece of code. For example you can write:

  • howtos guides for using your code
  • a nice README for the repo
  • code documentation in the source


Speaking of JavaScript, we can use a documentation layer called, JSDoc. It's a command line tool and a "documentation language" at the same time. Let's see how it can helps.


First thing first what's the anatomy of a JSDoc annotation? JSDoc is simple as adding a comment before the function:


But let's make things interesting with JSDoc annotations for function parameters. Here's the syntax:


/**

*

* @param {param type} param name - description

*

*/


  • its type, i.e. string, number, HTMLTableElement and so on
  • its name
  • a description



Example is like below 


/**

 * Generates a table head

 * @param {HTMLTableElement} table - The target HTML table

 * @param {Array} data - The array of cell header names

 */

function generateTableHead(table, data) {

  const thead = table.createTHead();

  const row = thead.insertRow();

  for (const i of data) {

    const th = document.createElement("th");

    const text = document.createTextNode(i);

    th.appendChild(text);

    row.appendChild(th);

  }

}



Adding JSDoc documentation has a side effect. Auto completion will improve in your IDE and you'll get real-time hints:


Moreover, the editor will scream if you try to pass the wrong kind of parameters:

It might sound crazy, but adding JSDoc annotations before writing the code, not after, is another thing you can do. And it has two nice outcomes. First, you will probably write better, simpler code because of the idea of it you formed while writing the documentation.



Other samples are:


/**

 * Generates a table head

 * @author Valentino Gagliardi <valentinoDOTvalentinog.com>

 * @param {HTMLTableElement} table - The target HTML table

 * @param {Array} data - The array of cell header names

 */



/**

 * A silly logger function

 * @param {string} message

 * @return {void} Nothing

 */



/**

 * Raises a number to exponent

 * @param {number} value - The base to raise

 * @param {number} exponent - The exponent

 * @return {number} - The exponent power

 */



JavaScript With JSDoc: generating the docs


mkdir jsdoc-tutorial && cd $_

npm init -y

npm i jsdoc --save-dev


To run Jsdoc in entire directory, below to be done 


$ jsdoc -r .


This may needs to be installed globally to make it available entire system. 


Nam install -g jsdoc


References:

https://www.valentinog.com/blog/jsdoc/



Best Way to document Javascript 




References:

https://gomakethings.com/whats-the-best-way-to-document-javascript/

Wednesday, February 24, 2021

DNS Record Management between GoDaddy and Firebase

To add a custom domain to the firebase app, just need to do this 


  1. Go to hosting and press on Add Custom domain 
  2. Now the firebase will give TXT record details 
  3. Go to the Go Daddy domain management console and add the TXT record 
  4. Once the TXT record is verified, A records will be given by the firebase
  5. Very important to note is on Go daddy, the root is @ and not the domain given by the firebase. So add the domain as @ and then give the IP as the A record IPs given by firebase 
  6. May need to wait for 10-15 minutes to have this change happened. But then good to go! 

Tuesday, February 23, 2021

Android How to set custom Font for entire application

In the styles.xml, define a custom style 


     <style name="CustomTextViewStyle" parent="android:Widget.TextView">

       <item name="android:fontFamily">@font/fonts</item>

     </style>


now inside the fonts folder, create a fonts.xml file and define the below


 <?xml version="1.0" encoding="utf-8"?>

 <font-family xmlns:android="http://schemas.android.com/apk/res/android">

   <font

        android:fontStyle="normal"

        android:fontWeight="400"

        android:font="@font/montserrat" />

    <font

        android:fontStyle="normal"

        android:fontWeight="700"

        android:font="@font/montserrat_bold" />

 </font-family>


and place the font files in the fonts folder itself. 


references:

https://stackoverflow.com/questions/2711858/is-it-possible-to-set-a-custom-font-for-entire-of-application

Sunday, January 31, 2021

How to copy file from one user to another user in linux on same machine

sudo cp /home/USER1/FNAME /home/USER2/FNAME && sudo chown USER2:USER2 /home/USER2/FNAME

references:

https://askubuntu.com/questions/551047/copying-files-from-one-user-to-another-in-a-single-machine