Skip to content
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

How can I use plugin inside callback? #34

Closed
vimalmistry opened this issue Apr 9, 2020 · 20 comments
Closed

How can I use plugin inside callback? #34

vimalmistry opened this issue Apr 9, 2020 · 20 comments
Labels
help wanted Extra attention is needed

Comments

@vimalmistry
Copy link

I want to call api on location change. I want to attach JWT_TOKEN which is stored in shared preferences. I am facing.

E/flutter (10000): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: MissingPluginException(No implementation found for method getAll on channel plugins.flutter.io/shared_preferences)

@RomanJos
Copy link
Contributor

RomanJos commented Apr 9, 2020

Can you show LocationService.kt ? Look like you didn't do the step 3 of Android. At the moment we can't use plugins on IOS by the way

@vimalmistry
Copy link
Author

@RomanJos I followed all 3 steps.
I created Application.kt and configured in manifest.xml.
I am not familiar with native programming.
Why IOS is not working? there is steps for configure in IOS.

@mehdok
Copy link
Collaborator

mehdok commented Apr 10, 2020

@vimalmistry The Document and the code need to be updated, I will do it this weekend. Please be hold until then.

@vimalmistry
Copy link
Author

@mehdok ohh :( Did you updated doc? I found some new informations in readme.md
One question. What is requirement of minimum android version? My client complaints it is not working on their older devices!

@mehdok
Copy link
Collaborator

mehdok commented Apr 10, 2020

Minimum android sdk is 16.

@RomanJos
Copy link
Contributor

Actually I modified the README 3 days ago so make sure to match changes on your project, it may resolve your issues 😉
I'm using it on a real Android 4.4.2 (API 19) and it work well I only have issue setting the accuracy to HIGH so I left it to NAVIGATION

@mehdok
Copy link
Collaborator

mehdok commented Apr 10, 2020

@RomanJos Sorry your changes in document isn't merged yet. If there is something that can help @vimalmistry please copy paste it here.

@RomanJos
Copy link
Contributor

No I mean the one that are already merged

I created Application.kt and configured in manifest.xml.

Its LocationService.kt on the readme

@vimalmistry
Copy link
Author

Thanks for replies. I actually configured using old doc (Application.kt). It works very well in most devices Except some devices and also I can not run it on emulator. But in physical device it works. But I found service is terminated after reboot. Is there any quick fix to auto start on reboot.
I am not a native developer. So I don't know much about .kt and .java file. I am just copy pasting every time.😇

@mehdok mehdok added the help wanted Extra attention is needed label Apr 11, 2020
@RomanJos
Copy link
Contributor

Thats strange it work on some devices but not even on the emulator, have you tried to run the example ? Starting at boot mean that we need a way to start the plugin directly from kotlin and not dart and I think it would require a lot of changes idk

And i do the same lol

@mehdok
Copy link
Collaborator

mehdok commented Apr 11, 2020

@vimalmistry I'm not sure why you can not get it working on some device or simulator, But I can confirm that the plugin is working fine on simulator, both Android and iOS.

On Android I can add the RECEIVE_BOOT_COMPLETED, it's not that hard to restart service when device rebooting, But it is something that only Android supports, as I know there is no similar feature on iOS, so for keeping everything equal I won't do it.

@TahaMalas
Copy link

Thanks for replies. I actually configured using old doc (Application.kt). It works very well in most devices Except some devices and also I can not run it on emulator. But in physical device it works. But I found service is terminated after reboot. Is there any quick fix to auto start on reboot.
I am not a native developer. So I don't know much about .kt and .java file. I am just copy pasting every time.😇

Hey, can you tell me how you managed to solve the issue? I'm getting the same problem with SharedPreferences package, while I'm trying to use it on the static callback function

@gpibarra
Copy link
Contributor

Have you followed the steps in the documentation?

https://github.com/rekab-app/background_locator/wiki/Use-other-plugins-in-callback

@khairu-aqsara
Copy link

i have same case and problem with you, but i follow step on https://github.com/rekab-app/background_locator/wiki/Use-other-plugins-in-callback and it's work, here is my Application.kt

import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin;
import io.flutter.view.FlutterMain
import rekab.app.background_locator.LocatorService

class Application : FlutterApplication(), PluginRegistrantCallback {
    override fun onCreate() {
        super.onCreate()
        LocatorService.setPluginRegistrant(this)
        FlutterMain.startInitialization(this)
    }

    override fun registerWith(registry: PluginRegistry?) {
        if (!registry!!.hasPlugin("io.flutter.plugins.sharedpreferences")) {
            SharedPreferencesPlugin.registerWith(registry!!.registrarFor("io.flutter.plugins.sharedpreferences"))
        }
    }
}

and change the Manifestfile.

<application
        android:name=".Application"

you need add shared pref plugin,change

import io.flutter.plugins.pathprovider.PathProviderPlugin

to this

import io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin;

and change

if (!registry!!.hasPlugin("io.flutter.plugins.pathprovider")) {
    PathProviderPlugin.registerWith(registry!!.registrarFor("io.flutter.plugins.pathprovider"))
 }

to this

 if (!registry!!.hasPlugin("io.flutter.plugins.sharedpreferences")) {     
   SharedPreferencesPlugin.registerWith(registry!!.registrarFor("io.flutter.plugins.sharedpreferences"))
}

and in the static callback just use the shared pref plugin as usual

SharedPreferences prefs = await SharedPreferences.getInstance();
String somthing = prefs.getString('somthing');

@wemersonrv
Copy link

wemersonrv commented Apr 24, 2020

@vimalmistry I'm not sure why you can not get it working on some device or simulator, But I can confirm that the plugin is working fine on simulator, both Android and iOS.

On Android I can add the RECEIVE_BOOT_COMPLETED, it's not that hard to restart service when device rebooting, But it is something that only Android supports, as I know there is no similar feature on iOS, so for keeping everything equal I won't do it.

@mehdok Any chance to convince you to make this implementation for Android?

Almost 90% here in Brazil are android devices: https://gs.statcounter.com/os-market-share/mobile/brazil

And worldwide is not so different: https://gs.statcounter.com/os-market-share/mobile/worldwide

@mehdok
Copy link
Collaborator

mehdok commented Apr 24, 2020

@wemersonrv I think I can do that this weekend.

mehdok added a commit that referenced this issue Apr 26, 2020
* #34 #41 Restart locator service after reboot;

* #42 Fix triggering android notification callback with wrong notification;
@MazEbeid
Copy link

MazEbeid commented Jun 11, 2020

Below is the Java equivalent to the above if your project doesn't support Kotlin

<YOUR PACKAGE NAME HERE>
import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
import io.flutter.plugins.GeneratedPluginRegistrant;
import rekab.app.background_locator.LocatorService;
import io.flutter.plugins.firebase.cloudfunctions.CloudFunctionsPlugin;
import io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin;

public class Application extends FlutterApplication implements PluginRegistrantCallback {
        @Override
        public void onCreate() {
                super.onCreate();
                LocatorService.setPluginRegistrant(this);
        }
        @Override
        public void registerWith(PluginRegistry registry) {

                final String sharedPrefKey = "io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin";
                final String functionsKey = "io.flutter.plugins.firebase.cloudfunctions.CloudFunctionsPlugin";
                if(!alreadyRegisteredWith(registry,sharedPrefKey))
                {
                   SharedPreferencesPlugin.registerWith(registry.registrarFor("io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin"));
                }
                if(!alreadyRegisteredWith(registry,functionsKey))
                {
                        CloudFunctionsPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebase.cloudfunctions.CloudFunctionsPlugin"));
                }
        }
        private static boolean alreadyRegisteredWith(PluginRegistry registry, String key) {
                if (registry.hasPlugin(key)) {
                        return true;
                }else{
                        return false;
                }

        }

}

@ozz-rjq
Copy link

ozz-rjq commented Jun 13, 2020

@MazEbeid thanks! can you help with ios configuration?

@MazEbeid
Copy link

MazEbeid commented Jun 13, 2020 via email

@mehdok
Copy link
Collaborator

mehdok commented Jun 14, 2020

@MazEbeid @ozz-rjq There is a bug with iOS that prevent the app to get a gps fix while app is terminated in iOS (or at least get a fix every 500 meter or so). Also there is a PR #59 which address this issue, but there is a bug with that too. It would be appreciated if anyone can help with that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

9 participants