Skip to content

background service? #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
danielzzz opened this issue Mar 6, 2015 · 59 comments
Closed

background service? #2

danielzzz opened this issue Mar 6, 2015 · 59 comments
Labels

Comments

@danielzzz
Copy link

hi, would it be possible to create a background service with native script?

thx, dan

@abhikmitra
Copy link

Yes same question

@sunel
Copy link

sunel commented Mar 7, 2015

👍

@mzalazar
Copy link

mzalazar commented Mar 9, 2015

I need to know the same thing, anybody? ;-)

@slavchev
Copy link

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.

@enchev
Copy link
Contributor

enchev commented May 18, 2015

Please use this issue: #85

@enchev enchev closed this as completed May 18, 2015
@gavinengel
Copy link

There are 2 thread referencing each other ... is this ticket issues/2 the main one for commenting on background threads, or is this?: #85

@RangerMauve
Copy link

As @enchev said, #85 should be used as the main one, I think.

@sgon00
Copy link

sgon00 commented Aug 4, 2015

+1
If "Native"script can not even create a simple background service, it should not be called "native".

@NathanaelA

This comment was marked as abuse.

@pacal
Copy link

pacal commented Oct 22, 2015

@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.

@x4080
Copy link

x4080 commented Nov 13, 2015

Is it possible that the service is in a library / jar and running the service from ns?

@slavchev
Copy link

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 #85, with active discussion.

@x4080
Copy link

x4080 commented Nov 13, 2015

OK, since its quite different, we must solve these 2 problems I think . notifications is quite important as responsiveness of the ux . thanks

@N3ll N3ll reopened this Nov 23, 2015
@N3ll N3ll added the feature label Dec 3, 2015
@mdgiles
Copy link

mdgiles commented Dec 3, 2015

+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.

@ahmadmarafa
Copy link

+1 i wish if there any way to make background service with nativescript

@burkeholland
Copy link

+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

@Daxito
Copy link

Daxito commented Feb 10, 2016

+1
I also need this, somehow we need to report user location at all times (private app)

@erkanarslan
Copy link

When does NativeScript plans to implement background services? We need to show reminders to users when they are not using the application.

@Daxito
Copy link

Daxito commented Mar 2, 2016

@erkanarslan I think you can use local notifications for this, I haven't used it but that is my understanding.

@x4080
Copy link

x4080 commented Mar 2, 2016

I wondered if it's possible to create service in Android studio then call the nativescript app?

@erkanarslan
Copy link

@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.

@AntonioCuevaUrraco
Copy link

+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

@erkanarslan
Copy link

@AntonioCuevaUrraco You can use https://github.com/NativeScript/push-plugin. This plugin will get messages from internet even if application is not running.

@andrew19881123
Copy link

is anyone able to make this example work?
sample-android-background-services

@boxgundam
Copy link

@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.

@slavchev
Copy link

@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.

@slavchev
Copy link

@andrew19881123 I checked the example project and so it works as expected. I saw you already created issues (NativeScript/sample-android-background-services#1 and NativeScript/sample-android-background-services#2) so I would suggest to move the discussion there.

@sadiqna
Copy link

sadiqna commented Mar 21, 2017

@slavchev can you provide a sample for background-service with nativescript angular ?

@slavchev
Copy link

slavchev commented Mar 21, 2017

@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.

@surdu
Copy link
Contributor

surdu commented Jun 18, 2017

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>

@Liooo
Copy link

Liooo commented Sep 10, 2017

@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

@Liooo
Copy link

Liooo commented Sep 10, 2017

Okay got it, for others coming here in the future. If you're using typescript, do the following

  1. run npm install tns-platform-declarations --save
  2. add the lines in 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" />
  1. add declare var com; before doing var intent = new android.content.Intent(context, com.something.SomeService.class); in the example above.

@surdu
Copy link
Contributor

surdu commented Sep 11, 2017

@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} 😅

@Liooo
Copy link

Liooo commented Sep 11, 2017

@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)

@surdu
Copy link
Contributor

surdu commented Sep 11, 2017

@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 😄

@Liooo
Copy link

Liooo commented Sep 11, 2017

@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 👍

@c1ngular
Copy link

c1ngular commented Sep 15, 2017

@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

@surdu
Copy link
Contributor

surdu commented Sep 15, 2017

@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 ?

@christocracy
Copy link

but I don't think there is an Android phone without Google Play Services

Talk to someone from China or Iran

@surdu
Copy link
Contributor

surdu commented Sep 15, 2017

@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

@christocracy
Copy link

If you want an alternative to play-services, with identical API, use this.

https://github.com/mapzen/lost

@c1ngular
Copy link

@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

@lock
Copy link

lock bot commented Aug 27, 2019

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.

@lock lock bot locked and limited conversation to collaborators Aug 27, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests