Skip to content

background service? #2

Closed
Closed

Description

@danielzzz

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

thx, dan

Activity

  1. abhikmitra commented on Mar 7, 2015

    @abhikmitra

    Yes same question

  2. sunel commented on Mar 7, 2015

    @sunel

    👍

  3. mzalazar commented on Mar 9, 2015

    @mzalazar

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

  4. slavchev commented on Mar 13, 2015

    @slavchev

    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.

  5. enchev commented on May 18, 2015

    @enchev
    Contributor

    Please use this issue: #85

  6. gavinengel commented on May 20, 2015

    @gavinengel

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

  7. RangerMauve commented on May 20, 2015

    @RangerMauve

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

  8. sgon00 commented on Aug 4, 2015

    @sgon00

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

  9. NathanaelA commented on Oct 22, 2015

    @NathanaelA
  10. pacal commented on Oct 22, 2015

    @pacal

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

  11. x4080 commented on Nov 13, 2015

    @x4080

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

  12. slavchev commented on Nov 13, 2015

    @slavchev

    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.

  13. x4080 commented on Nov 13, 2015

    @x4080

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

  14. reopened this on Nov 23, 2015
  15. 36 remaining items

  16. Liooo commented on Sep 10, 2017

    @Liooo

    @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
    
  17. Liooo commented on Sep 10, 2017

    @Liooo

    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.
  18. surdu commented on Sep 11, 2017

    @surdu
    Contributor

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

  19. Liooo commented on Sep 11, 2017

    @Liooo

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

  20. surdu commented on Sep 11, 2017

    @surdu
    Contributor

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

  21. Liooo commented on Sep 11, 2017

    @Liooo

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

  22. c1ngular commented on Sep 15, 2017

    @c1ngular

    @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

  23. surdu commented on Sep 15, 2017

    @surdu
    Contributor

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

  24. christocracy commented on Sep 15, 2017

    @christocracy

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

    Talk to someone from China or Iran

  25. surdu commented on Sep 15, 2017

    @surdu
    Contributor

    @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

  26. christocracy commented on Sep 15, 2017

    @christocracy

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

    https://github.com/mapzen/lost

  27. c1ngular commented on Sep 16, 2017

    @c1ngular

    @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

  28. added a commit that references this issue on Mar 20, 2019
  29. lock commented on Aug 27, 2019

    @lock

    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.

  30. locked and limited conversation to collaborators on Aug 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions