hi, would it be possible to create a background service with native script?
thx, dan
Yes same question
:+1:
I need to know the same thing, anybody? ;-)
Currently, NativeScript for Android does not support this scenario (at least we haven't tested it). However, this is technically possible and we will provide support for it.
Please use this issue: https://github.com/NativeScript/NativeScript/issues/85
There are 2 thread referencing each other ... is this ticket issues/2 the main one for commenting on background threads, or is this?: https://github.com/NativeScript/NativeScript/issues/85
As @enchev said, #85 should be used as the main one, I think.
+1
If "Native"script can not even create a simple background service, it should not be called "native".
Just to clarify for any new people coming to this issue. This is a different request than #85, 85 is to allow background processing as a new thread. This one is to create a Service which they typically run all the time. Now maybe this is dependent on #85, but they are different requests.
@NathanaelA this issue looks closed however. @slavchev would it be possible to re-open this? Or is there a general ticket covering this issue for IOS and Android? Thanks.
Is it possible that the service is in a library / jar and running the service from ns?
Currently NativeScript runtime for Android (you mentioned jar library) does not support this scenario though it is technically possible. I would suggest to keep this issue closed as there are other issues on this topic, for example https://github.com/NativeScript/NativeScript/issues/85, with active discussion.
OK, since its quite different, we must solve these 2 problems I think . notifications is quite important as responsiveness of the ux . thanks
+1 this is essential for apps that incorporate functionality like geo-fencing and event reminders. We need to be able to have a background service running.
+1 i wish if there any way to make background service with nativescript
+1 Need this for a simple app that reports the users location periodically even in the background. This requires a service for Android as best I can tell
+1
I also need this, somehow we need to report user location at all times (private app)
When does NativeScript plans to implement background services? We need to show reminders to users when they are not using the application.
@erkanarslan I think you can use local notifications for this, I haven't used it but that is my understanding.
I wondered if it's possible to create service in Android studio then call the nativescript app?
@Daxito Thank you. I found how to create local notifications and server push notifications:
https://github.com/EddyVerbruggen/nativescript-local-notifications
https://github.com/NativeScript/push-plugin
There is no need to run a service to show notifications, reminders or receive chat messages.
+1 to create a Create a Service that can run all the time. For us it would be perfect to get data from our servers to update Today extension and android widget.
@NathanaelA do you think it would be possible to use your websockets plugin to open a connection to a server even when the app is not running? So If pull is not possible maybe push data from the servers
@AntonioCuevaUrraco You can use https://github.com/NativeScript/push-plugin. This plugin will get messages from internet even if application is not running.
+1
+1 really looking for this support as we are looking for a process to upload data consistently, even when the app isn't running.
Hi guys. I'm working with Telerik Platform and I have the follow question. does somebody know how to get a notification and save it when the app is not open?. I'm using "nativescript-push-notifications" module for sending/receiving notifications; and I have been saving it with "application-settings" module . This works if my app is open, but, when the app is not, I just receive a notification on the top of my device and I can't save it for later being displayed in one of my views. I appreciate any ideas. Thank you
+1
Was researching a way to get an app auto-started and run as a service (because it needs to constantly monitor the power state change of the device, after it boots up, don't ask why!), but currently this does not seem possible with Android?
Ive begun porting my advanced Background Geolocation (API docs) module to NativeScript. iOS is mostly implemented; Android will follow soon.
Originally designed for tracking first-responders into disaster zones, It monitors battery state, geofencing and motion-activity in the background. See Location Schema
It's very advanced and over three years old. It was initially developed for Cordova, where it has thousands of users. I've ported it to React Native, as well. Porting to NativeScript so far has been a breeze.
Impressive framework, this NativeScript. I like it better than React Native.
Any luck with implementing creating a background service ?? This issue if fixed would easy my pain right now.
@robophil what do you want to do in a bg service?
@christocracy i want to be able to update the location of a user after first log in. So even if the app is removed from memory or to the background it would keep running. Like an IntentService or Service in android
@Robophil Have you seen my background-geolocation plugin? It does exactly what you need (and probably things you haven't even thought of yet)
This is a plugin originally developed for Cordova that I've recently ported to NativeScript. It's evolved over ~3 years and has thousands of user in the Cordova universe. I've ported to ReactNative as well.
@christocracy thanks, I'll give it a try
Please check these two repositories that have a sample how to use background services in Android and iOS.
Android Background Services
https://github.com/NativeScript/sample-android-background-services
iOS Background Execution
https://github.com/NativeScript/sample-ios-background-execution
For the WebWorker discussion please check this issue - #85; We are also working ACTIVELY to enable it.
i ended up writing a plugin that solved this for me. Thanks
@Robophil I'm interested in your plugin, did you share it on github?
@andrew19881123 It was built specifically for the project i was working on. I wrote the service natively and exported it as an aar file, so I could start the service from nativescript when needed.
@valentinstoychev that iOS background-task will run for only 180s max before iOS automatically suspends the app.
@christocracy Correct. This limitation comes from the iOS operating system. In general (for better or worse) iOS doesn't allow you to execute a general-purpose background task forever (Android is not so restrictive on this). In some special cases your task is allowed to run longer, but you need to use UIBackgroundModes
in your Info.plist file to specify the specific intent of the task. Keep in mind that all of these scenarios can be handled entirely from JavaScript using NativeScript.
is anyone able to make this example work?
sample-android-background-services
@andrew19881123 You need to run it using the pre-generated platforms folder provided. Corresponding java files have been added to the platforms/android/src/main/java/com/tns folder.
@andrew19881123 I will have a look at it tomorrow. @boxgundam there are some changes with today's release (2.3.0). We no longer generate *.java
files. Instead we generate *.dex
files in platforms/android/build/nativescript-bindings
directory. The main motivation for this change is to use AsmDex library for both build-time and run-time class generation. It's a trade off - on one hand the generated *.java
source code provides better understanding how it works and on the other hand generating *.dex
files provide better maintainability as we have single code base for both build-time and run-time scenarios.
@andrew19881123 I checked the example project and so it works as expected. I saw you already created issues (https://github.com/NativeScript/sample-android-background-services/issues/1 and https://github.com/NativeScript/sample-android-background-services/issues/2) so I would suggest to move the discussion there.
@slavchev can you provide a sample for background-service with nativescript angular ?
@sadiqna I am not aware of sample for background service with NativeScript+Angular but it shouldn't be hard to adjust the existing sample. Basically, all (most) of the Angular things in NativeScript are related to UI and application components. For Android background services all you need is a service implementation and the according changes in AndroidManifest.xml
.
Anyway, I would suggest to ask in {N} community Slack channel.
This is how I made a service that runs in the background in NativeScript:
_SomeService.js_:
// I'm using toast to check that the service restarts once I close the app
var Toast = require("nativescript-toast"); //tns plugin add nativescript-toast
android.app.Service.extend("com.something.SomeService", {
onStartCommand: function(intent, flags, startId) {
this.super.onStartCommand(intent, flags, startId);
return android.app.Service.START_STICKY;
},
onCreate: function() {
var toast = Toast.makeText("SomeService STARTED");
toast.show();
},
onBind: function(intent) {
console.log("##onBind NOT YET IMPLEMENTED");
}
})
module.exports = {}
then, in my _main.js_ view controller:
const SomeService = require("./SomeService");
const utils = require("utils/utils");
function onPageLoaded() {
var context = utils.ad.getApplicationContext();
var intent = new android.content.Intent(context, com.something.SomeService.class);
context.startService(intent);
}
Also, don't forget to add the service to the manifest file
<service android:name="com.something.SomeService"
android:exported="false" >
</service>
@surdu does this code still work? I'm using tns-android 3.2, and android.app is not found
import { android } from "tns-core-modules/application/application";
android.app // <= not found
Okay got it, for others coming here in the future. If you're using typescript, do the following
npm install tns-platform-declarations --save
reference.d.ts
/// <reference path="./node_modules/tns-core-modules/tns-core-modules.d.ts" />
/// <reference path="./node_modules/tns-platform-declarations/ios.d.ts" />
/// <reference path="./node_modules/tns-platform-declarations/android.d.ts" />
declare var com;
before doing var intent = new android.content.Intent(context, com.something.SomeService.class);
in the example above.@Liooo I'm not using this exact code in my app anymore, but it should still work.
I needed it to track locations in background and figured out I don't need a continuously running background service for that.
And yeah, maybe I should have mentioned that that code is written in vanilla JS version of {N} 馃槄
@surdu thanks for the reply, and yea it will still work for vanilla JS {N}.
I needed it to track locations in background and figured out I don't need a continuously running background service for that.
Oh I'm doing the same thing, and if we want the location tracking to work even when the app is in the background or killed, we need Service don't we?
(sorry it's bit off track from the original issue tho)
@Liooo No, you don't 馃槃
You need to create am instance of IntentService like so:
const LocationResult = com.google.android.gms.location.LocationResult;
com.pip3r4o.android.app.IntentService.extend("com.my.IntentService", {
onHandleIntent: function (intent) {
const loc = LocationResult.extractResult(intent).getLastLocation();
}
});
You will need this plugin for the above code in order to be able to declare IntentServices with an empty constructor.
Then, when you request for the location updates, you pass the class created above .
const context = application.android.context;
const intent = new android.content.Intent(context, com.my.IntentService.class);
const pendingIntent = android.app.PendingIntent.getService(context, 0, intent, android.app.PendingIntent.FLAG_UPDATE_CURRENT);
LocationServices.FusedLocationApi.requestLocationUpdates(api, locationRequest, pendingIntent);
What I didn't realize, is that the com.my.IntentService
class will be instantiated even if you killed the app, once you registered it for location updates.
Note: the code is mostly copy & pasted for reference only, it will not work as-is. If you don't manage to figure out the missing parts, please let me know ;)
Also, we are now working on an open source natives script plugin that will allow you to use location tracking in background. The code was copy pasted from that 馃槃
@surdu
ah right, saw this repo taking the same strategy. Thanks for that.
Also, we are now working on an open source natives script plugin that will allow you to use location tracking in background. The code was copy pasted from that 馃槃
That'd be awesome, looking forward to seeing that 馃憤
@surdu
`const LocationResult = com.google.android.gms.location.LocationResult;
com.pip3r4o.android.app.IntentService.extend("com.my.IntentService", {
onHandleIntent: function (intent) {
const loc = LocationResult.extractResult(intent).getLastLocation();
}
});`
is this code snippet depend on google play service ? if play service is not available , what is my alternative ? thanks
@c1ngular Yes, it depends on Google Play Services. I believe there are no other alternatives, but I don't think there is an Android phone without Google Play Services ... I mean how do you install the app on the phone otherwise ?
but I don't think there is an Android phone without Google Play Services
Talk to someone from China or Iran
@c1ngular According to this you do have an alternative to Google Play Services when it comes to location. I don't know if for that you still have the ability to use it in the background without a continuously running service.
Here you have some more details: https://stackoverflow.com/a/33023788/460750
If you want an alternative to play-services, with identical API, use this.
@surdu @christocracy thanks !
I have Google play service on my phone, but most phones in China don't, ! besides the background location , is it possible that I use this background service to send user notifications (Push service) even my app is not running ? Any advice on this approach? Thanks
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
+1
If "Native"script can not even create a simple background service, it should not be called "native".