We are building a nativescript app(v3.1.2, Angular 2) in which we neeed to create a service which should run continuously in background. For that I have created Native Android service in typescript. Please find below the code of my service class:
var app = require("application");
var timer = require("timer");
var _timerId;
var Toast = android.widget.Toast;
var context = app.android.context;
function setUpTimer() {
_timerId = timer.setTimeout(() => {
Toast.makeText(content, "Toast after timeout from Service", Toast.LENGTH_SHORT).show();
}, 2000);
}
function processStartNotification() {
// Do something. For example, fetch fresh data from backend to create a rich notification?
var utils = require("utils/utils");
var context = utils.ad.getApplicationContext();
var builder = new android.app.Notification.Builder(context);
builder.setContentTitle("Scheduled Notification")
.setAutoCancel(true)
.setColor(android.R.color.holo_purple)//getResources().getColor(R.color.colorAccent))
.setContentText("This notification has been triggered by Notification Service")
.setVibrate([100, 200, 100])
.setSmallIcon(android.R.drawable.btn_star_big_on);
// will open main NativeScript activity when the notification is pressed
var mainIntent = new android.content.Intent(context, com.tns.NativeScriptActivity.class);
var pendingIntent = android.app.PendingIntent.getActivity(context,
1,
mainIntent,
android.app.PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
builder.setDeleteIntent(getDeleteIntent(context));
var manager = context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
manager.notify(1, builder.build());
}
function getDeleteIntent(context) {
var intent = new android.content.Intent(context, com.tns.broadcastreceivers.NotificationEventReceiver.class);
intent.setAction("ACTION_DELETE_NOTIFICATION");
return android.app.PendingIntent.getBroadcast(context, 0, intent, android.app.PendingIntent.FLAG_UPDATE_CURRENT);
}
=====================
I have created a demo native script app in which I am calling this service on tap of a button.But it is giving me following error:
An uncaught Exception occurred on "main" thread.
com.tns.NativescriptException:
calling js method onClick failed
TypeError: Cannot read property 'class' of undefined
File: "file:///data/data/org.nativescript.bgservice/files/app/main-view-model.js,
line 22, column 123"
Code snippet of button tap event:
viewModel.onTap = function ()
{
var intent = new android.content.Intent(utils.ad.getApplicationContext(), com.tns.notifications.NotificationService.class); // It give me error on this line
utils.ad.getApplicationContext().startService(intent);
}
The same code works in intentservice, but crashes if I use service. Can anyone please help here, its urgent!
We are building a nativescript app(v3.1.2, Angular 2) in which we neeed to create a service which should run continuously in background. For that I have created Native Android service in typescript. Please find below the code of my service class:
var app = require("application"); var timer = require("timer"); var _timerId; var Toast = android.widget.Toast; var context = app.android.context;
android.app.Service.extend("com.tns.notifications.NotificationService", { onBind: function (intent) { return null; } }, { onStartCommand(intent, flags, startId) { setUpTimer(); return android.app.Service.START_STICKY; } }, { onDestroy:function () { this.timer.clearTimeout(this._timerId); } });
function setUpTimer() { _timerId = timer.setTimeout(() => { Toast.makeText(content, "Toast after timeout from Service", Toast.LENGTH_SHORT).show(); }, 2000); } function processStartNotification() { // Do something. For example, fetch fresh data from backend to create a rich notification?
}
function getDeleteIntent(context) { var intent = new android.content.Intent(context, com.tns.broadcastreceivers.NotificationEventReceiver.class); intent.setAction("ACTION_DELETE_NOTIFICATION"); return android.app.PendingIntent.getBroadcast(context, 0, intent, android.app.PendingIntent.FLAG_UPDATE_CURRENT); }
===================== I have created a demo native script app in which I am calling this service on tap of a button.But it is giving me following error: An uncaught Exception occurred on "main" thread. com.tns.NativescriptException: calling js method onClick failed
TypeError: Cannot read property 'class' of undefined File: "file:///data/data/org.nativescript.bgservice/files/app/main-view-model.js, line 22, column 123"
Code snippet of button tap event:
The same code works in intentservice, but crashes if I use service. Can anyone please help here, its urgent!
Thanks in anticipation!