Folks,
I'm facing a problem here that I can't find a solution for it.
In my app I offer external links that the user can click on and open websites in browser.
They work fine in both IOS and Android emulator, but do not work in real Android phones.
Here the code that operates that:
void _makeAction(String url) async {
if (await canLaunch(url)) {
await launch(url);
} else {
_showMessage(Translate.of(context)!.translate('cannot_make_action'));
}
}
Future<bool> canLaunch(String urlString) async {
return await UrlLauncherPlatform.instance.canLaunch(urlString);
}
Any idea what can be happening?
Thanks in advance.
Related
I have a todo app built in Flutter and intended only for Android. I built a home screen widget for it (using the home_widget package in Flutter) to allow users to see a list of tasks and check them off directly from the widget.
At midnight, the tasks should reset with the new tasks for the day (I used the workmanager package to accomplish this, although I also tried the android_alarm_manager_plus package, with the same results). All of this functionality is working perfectly in debug mode, and even in profile mode (I can't test it in release mode because, according to my understanding, that would remove services and thus the home_widget would not work; however, when I do the build, that doesn't seem to be the problem because the home widget still shows up). BUT! When I build the release APK and submit it to Google Play for internal testing, then download it onto my Pixel 7 (with no power saving modes on, as far as I'm aware), the midnight function does not run. :(
Here's the relevant code:
main_prod.dart
void main() async {
return mainGeneric('Prod Name', ProdFirebaseOptions.currentPlatform, Environment.prod);
}
main_generic.dart
/// Used for Background Updates using Workmanager Plugin
#pragma('vm:entry-point')
void workmanagerCallbackDispatcher() {
Workmanager().executeTask((taskName, inputData) {
if (taskName == 'widgetBackgroundUpdate') {
try {
return Future.wait<void>([
// This is a static Future<void> function from a helper class that resets
// the tasks; it seems to be working when I test it by itself, as well as
// in debug or profile mode.
MidnightService.resetTasks(),
]).then((value) {
return Future.value(true);
});
} catch(err) {
print(err.toString());
throw Exception(err);
}
}
return Future.value(true);
});
}
void _startBackgroundUpdate() async {
if (await MidnightService.shouldUpdateWorkManagerTasks()) {
(await SharedPreferences.getInstance()).setInt('midnight_tasks_update_version', Constants.MIDNIGHT_TASKS_UPDATE_VERSION);
await Workmanager().cancelAll();
DateTime now = DateTime.now();
int nowMillis = now.millisecondsSinceEpoch;
int midnightTonightMillis = DateTime(now?.year ?? 0, now?.month ?? 0, (now?.day ?? 0) + 1).millisecondsSinceEpoch;
int millisUntilMidnight = midnightTonightMillis - nowMillis;
await Workmanager().registerPeriodicTask('midnightUpdate', 'widgetBackgroundUpdate', initialDelay: Duration(milliseconds: millisUntilMidnight), frequency: Duration(days: 1));
}
}
void mainGeneric(String appName, FirebaseOptions firebaseOptions, Environment environment) async {
// Avoid errors caused by flutter upgrade.
WidgetsFlutterBinding.ensureInitialized();
Workmanager().initialize(workmanagerCallbackDispatcher, isInDebugMode: kDebugMode).then((_) => _startBackgroundUpdate());
...
// If this is the first time opening the app with widget functionality.
HomeWidget.getWidgetData<String>('todays_tasks_string', defaultValue: '').then((todaysTasksString) async {
if (todaysTasksString == '') {
List<Task> todaysTasks = await Repositories().taskRepository.getFocusedTasks();
await HomeWidgetUtils.setTodaysTasks(todaysTasks);
return true;
}
return false;
});
Firebase.initializeApp(
name: appName,
options: firebaseOptions,
).then((_) async {
...
});
HomeWidget.registerBackgroundCallback(homeWidgetBackgroundCallback);
runApp(AppConfig(
child: MyApp(),
environment: environment,
appTitle: appName,
));
}
// Called when doing background work initiated from home screen widget
#pragma('vm:entry-point')
Future<void> homeWidgetBackgroundCallback(Uri uri) async {
if (uri.host.startsWith('completetask_')) {
String todaysTasksString = await HomeWidgetUtils.updateTaskById(uri.host.split('_')[1], true);
await HomeWidget.saveWidgetData<String>('todays_tasks_string', todaysTasksString);
await HomeWidget.updateWidget(androidName: 'TodaysTasksWidgetProvider');
}
}
midnight_service.dart
class MidnightService {
...
static Future<bool> shouldUpdateWorkManagerTasks() async {
try {
final prefs = await SharedPreferences.getInstance();
int midnightTasksUpdateVersion = prefs.getInt('midnight_tasks_update_version');
return Constants.MIDNIGHT_TASKS_UPDATE_VERSION > midnightTasksUpdateVersion;
}
catch (e) { print(e); }
return true;
}
}
It might also be valuable to note that, when a user checks off a task from the home screen widget, sometimes the task takes a while to actually be checked off (and sometimes requires the app to be opened before it will execute). However, I figured this is just a slowness issue or something controlled by the OS that I can't do much about.
With all of that, my question is then, why is the workmanager not executing its midnight task?
I've been smashing my head against this for days, so any help and/or advice is greatly appreciated!!
I have a React app that attempts to prevent a screen from sleeping when a component loads or on press of a button. For some reason it doesn't work in Samsung Internet. I does work in Chrome on Mac and in Chrome on Samsung phone. I can't find anything to suggest that this is a common problem. It looks like it is a supported feature. I do see the code being executed in logs, but the phone goes to sleep anyway. Is there anything that maybe should be enabled on the phone or in Samsung Internet settings of some sort?
Here is my implementation
async requestLock() {
Debugger.log('requesting wakeLock')
try {
if ('wakeLock' in navigator) {
this.wakeLock = await navigator.wakeLock.request('screen');
Debugger.log('wakeLock requested')
}
} catch (e) {
Debugger.log('wakeLock error');
}
}
async releaseLock() {
if (this.wakeLock) {
Debugger.log('releasing wakeLock')
await this.wakeLock.release();
Debugger.log('wakeLock released')
this.wakeLock = null;
}
}
Here is my initializeCamera code :
initializeCamera() async {
try {
final cameras = await availableCameras();
final firstCamera = cameras.first;
ContextManager.camera = firstCamera;
setState(() {
cameraAuthorized = true;
ContextManager.cameraAuthorized = cameraAuthorized;
});
} catch(e) {
ContextManager.camera = null;
setState(() {
cameraAuthorized = false;
ContextManager.cameraAuthorized = cameraAuthorized;
});
print(e);
}
}
Here is my initState code where I call the initializeCamera function and after that initialize the CameraController :
#override
void initState() {
super.initState();
initializeCamera().then((value) {
if (ContextManager.camera != null) {
setState(() {
_controller = CameraController(
ContextManager.camera,
ResolutionPreset.medium,
);
_initializeControllerFuture = _controller.initialize();
});
}
});
}
When I am running the code with flutter web from my computer browser I get this popup from my initializeCamera step :
When I click "allow" the rest of the code which is called in the then()
and especially _controller.initialize(), which initialize my camera on the device, is executed and the camera starts working. When I click "deny" another part of the code that I customed is run to display a message on the screen, in other words everything works fine.
When I'm running on the Android Emulator everything from my initializeCamera() function to _controller.initialize() is run without interruption until I get this popup :
Whether I choose "while using the app" or "only this time" the camera starts working and everything is ok. But when I click "deny" the app throws a CameraException.
I tried to wrap this line (which is causing the issue) :
_initializeControllerFuture = _controller.initialize()
in try/catch, and also specifying the name of the exception, but nothing worked.
What I don't get here, is that _controller.initialize() function is called before the popup show in the emulator, so the camera is initialized on the device before we can actually choose to deny/allow it.
While on the computer you actually get to choose whether you deny/allow the camera before the _controller.initialize() function is executed, which make perfect sense to me.
The ways it works on the emulator doesn't make any sense for me, if someone more knowledgeable could enlighten me, thanks.
According to the Flutter's official deep linking page, we do not require any plugin or native Android/iOS code for handling deep links.
But it doesn't really tell us how we can get the data from that link. I'm talking from coding perspective. Sure, they have written in there that:
But this does not tell me where should I write what code to actually get the complete link. I've looked for examples/tutorials but I'm unable to find anything that is not using a plugin for handling deep linking.
Right now, all I've done is add <intent-filter> tags in AndroidManifest.xml file and on clicking the link, my app has started to show up. But I don't know how to extract data from that link.
Is there anyone who can guide me here? Thanks in advance.
You need platform specific code to handle deep linking. If you follow link mention in documention, you will find complete example.
private val CHANNEL = "poc.deeplink.flutter.dev/channel"
private var startString: String? = null
override fun configureFlutterEngine(#NonNull flutterEngine:FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine)
MethodChannel(flutterEngine.dartExecutor, CHANNEL).setMethodCallHandler { call, result ->
if (call.method == "initialLink") {
if (startString != null) {
result.success(startString)
}
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val intent = getIntent()
startString = intent.data?.toString()
}
Flutter Code:
class DeepLinkBloc extends Bloc {
//Event Channel creation
static const stream = const
EventChannel('poc.deeplink.flutter.dev/events');
//Method channel creation
static const platform = const
MethodChannel('poc.deeplink.flutter.dev/channel');
StreamController<String> _stateController = StreamController();
Stream<String> get state => _stateController.stream;
Sink<String> get stateSink => _stateController.sink;
//Adding the listener into contructor
DeepLinkBloc() {
//Checking application start by deep link
startUri().then(_onRedirected);
//Checking broadcast stream, if deep link was clicked in opened appication
stream.receiveBroadcastStream().listen((d) => _onRedirected(d));
}
_onRedirected(String uri) {
// Here can be any uri analysis, checking tokens etc, if it’s necessary
// Throw deep link URI into the BloC's stream
stateSink.add(uri);
}
#override
void dispose() {
_stateController.close();
}
Future<String> startUri() async {
try {
return platform.invokeMethod('initialLink');
} on PlatformException catch (e) {
return "Failed to Invoke: '${e.message}'.";
}
}
}
Follow this link for more detail.
https://medium.com/flutter-community/deep-links-and-flutter-applications-how-to-handle-them-properly-8c9865af9283
The Flutter way to do that, assuming you've already made the steps in the guide you posted, is to create a onGenerateRoute and/or onGenerateInitialRoutes handlers in your MaterialApp so that these handlers deals with the routes passed or pushed by the framework according to the described behaviors. You can even create an expected named route coming from a deeplink on the routes property of MaterialApp, even though I believe the dynamic generation of routes is more appropriate due to the dynamic nature of deeplinking, specially if you're dealing with "authentication needed content" inside your app.
Or, if you don't want to pass trough the platform specific code, you could use firebase dynamic links. That would allow to easily listen to links coming from both platforms and you also get the advantage that your link would bring up the store listing page if the user doesn't have the app installed.
I've written a full example here: https://gbaccetta.medium.com/flutter-deep-linking-with-firebase-dynamic-links-and-bloc-architecture-660f0517fbc2
Do for android as said in the web page: "Add a metadata tag and intent filter to AndroidManifest.xml inside the tag with the ".MainActivity" name". Do what required for ios too.
Then use onGenerateRoute in the usual way in MaterialApp, don't use "routes:". For example:
onGenerateRoute: (settings) {
print("settings.name " + settings.name.toString());
if (settings.name == '/') return MaterialPageRoute(builder: (_) => ScreenStart());
return MaterialPageRoute(builder: (_) => ScreenUnknown());
},
Then to simulate I did:
cd /Users/Utente/AppData/Local/Android/Sdk/platform-tools
adb shell
am start -W -a android.intent.action.VIEW -c android.intent.category.BROWSABLE -d "http://theaddressichoosed.com/helloworld?byebye"
And
print("settings.name " + settings.name.toString());
printed
settings.name /helloworld?byebye
After spending some time on this, here's my take using the Navigator 2 API. It also shows how to perform query and path arguments parsing. Hope it will save someone the time I spent researching this.
Obviously you also need to edit your platform-specific build files (such as AndroidManifest.xml for Android) as shown in the Flutter Deep Linking page.
A special note for Android 12 and above: you'll also need to securely approve the app's domain in the Google Play Developer Console for deep linking to work.
class App extends StatelessWidget {
const App({super.key});
#override
Widget build(BuildContext context) => MaterialApp(
initialRoute: '/',
routes: {
'/': (context) => HomeScaffold(),
'/route1': (context) => const RouteOneScaffold(),
'/route2': (context) => const RouteTwoScaffold(),
// Other routes which don't need any sort of query parsing
},
onGenerateRoute: (settings) {
// This is executed to determine which route to follow if no adequate entry is found in the `routes` array above.
// Here we can parse path and query parameters as we like.
final fullRoute = settings.name;
if (fullRoute == null) {
return null;
}
final routeData = Uri.tryParse(fullRoute);
if (routeData == null) {
return null;
}
final pathParameters = routeData.pathSegments;
final queryParameters = routeData.queryParameters;
// Here you can write your route handling logic
return MaterialPageRoute(builder: (context) => RouteThreeScaffold(pathParameters,queryParameters));
},
);
}
I am trying to use PortableRest to make an Async call to a Web API 2.2 Rest service from Xamarin Forms.
I think I have some kind of deadlock / synchronisationcontext issue but I cannot work it out (newbie to async etc).
Can anyone please help?
My controller test method (removed any call to database) -
public IEnumerable<ContentModel> GetTestRest()
{
return new List<ContentModel> {
new ContentModel() {Categoryid = 1, Title = "Title"}};
}
My Unit Test Passes -
[TestMethod]
public async Task TestRest()
{
MyNewsApiClient MyNewsApiClient = new MyNewsApiClient();
var models = await MyNewsApiClient.TestRest();
int count = models.Count;
Assert.AreEqual(1, count);
}
My PortableRest Proxy (PCL) Method -
public async Task<List<ContentModel>> TestRest()
{
var request = new RestRequest();
request.Resource = "Content/GetTestRest";
return await ExecuteAsync<List<ContentModel>>(request);
}
Xamarin Forms ContentPage (PCL) -
public partial class NewsType1CP : ContentPage
{
public NewsType1CP ()
{
InitializeComponent ();
}
protected override void OnAppearing ()
{
LoadData (); // this is a sync call of an async method, not sure how else to approach, make OnAppearing async?
}
public async Task LoadData ()
{
Debug.WriteLine ("LoadData");
HeaderLabel.Text = "Load Data!";
MyNewsApiClient api = new MyNewsApiClient ();
var cm = await api.TestRest ();
// this will work on its own, control returns to this method - await api.GetDelay ();
HeaderLabel.Text = "After! - ";
NewsListView.ItemsSource = cm;
}
}
The await to api.TestRest() never results in HeaderLabel.After or ListView being set.
If I just add a test Proxy Method GetDelay() which does not call PortableRest via return await ExecuteAsync>(request);
public async Task<bool> GetDelay ()
{
await Task.Delay (1000);
return true;
}
Then all "works".
Thanks for your help, I will download the source code for PortableRest if required (using Nuget dll at present), just was not sure if missing something basic at this top level.
Right, this eventually has nothing to do with Async (or any Issue with PortableRest), but thought I would share in case it can help others.
I eventually put a try block (obvious next step now!) and caught this exception -
Method not found: 'System.Net.Http.HttpClientHandler.set_AutomaticDecompression'.
So, looking at this -
http://davidburela.wordpress.com/2013/07/12/error-when-using-http-portable-class-library-compression/
PortableRest was already in my Forms PCL, but I needed to add Microsoft Http Client Libraries Nuget to my Android Application.
I then got -
Error: ConnectFailure (The requested address is not valid in this context)
I thought it may be something to do with Android Permissions, but it was not in this case (although I now realise I need to add Internet permission anyway for future calls).
Because I am running my App in a VM (from GenyMotion / Virtual Box) then the Android App cannot access LocalHost (where my Web Api resides)
So, from this -
http://bbowden.tumblr.com/post/58650831283/accessing-a-localhost-server-from-the-genymotion
I can confirm that using the address shown in VirtualBox > File > Preferences > Network > VirtualBox Host-Only Network Adapter > Modify does the job beautifully.
Thanks for your help.