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:
main.js
var APP = require('application');
var utils = require("utils/utils");
var application = APP.android;
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 = android.app.PendingIntent.getActivity(application.foregroundActivity, randomCode, intent, android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK);
var builder = new android.app.Notification.Builder(application.foregroundActivity);
builder.setDefaults(0);
builder.setContentTitle("NativeScript Running");
builder.setContentText("This notification cannot be cleared");
builder.setContentIntent(pendingIntent);
builder.setTicker("Persistent Notification");
builder.setSmallIcon(application.nativeApp.getApplicationInfo().icon);
builder.setPriority(android.app.Notification.PRIORITY_HIGH);
builder.setOngoing(true);//this tells the OS that the notification is persistant
var notification = builder.build();
var notificationManger = utils.ad.getApplicationContext().getSystemService(android.content.Context.NOTIFICATION_SERVICE);
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.