Create persistent notification on Android using NativeScript

I was experimenting with some background tasks in nativescript for android and I found that one way to ensure that your android app will always run even when it is in background is to use a persistent notification. So, I was able to create this using the following code:

NativeScript Code:


var APP = require('application');
var utils = require("utils/utils");
var application =;

exports.navigatingTo =  function (args) {
    var page = args.object;

    var randomCode = new java.util.Random().nextInt();//to be used later to associate the 2 things
        var intent = new android.content.Intent(application.foregroundActivity, com.tns.NativeScriptActivity.class);
    var pendingIntent =, randomCode, intent, android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK);
    var builder = new;
    builder.setContentTitle("NativeScript Running");
    builder.setContentText("This notification cannot be cleared");
    builder.setTicker("Persistent Notification");
    builder.setOngoing(true);//this tells the OS that the notification is persistant
    var notification =;
    var notificationManger =;
    notificationManger.notify(randomCode, notification);

and voila, you have persistant notification. Ofcourse this can be better managed but now you know how to create a persistent notification. Enjoy.