Checking if location services are enabled using react native - android

I am currently working on an app using react native that requires location services to be on for some of its features. I have been researching if there is a simple way to test if the user has them turned on or not so I can display a different view if they aren't. I have not found any results other than ones that are specific to IOS or Android. Has anyone else found a solution to this?

Use the "react native android location services dialog box" module for android
https://www.npmjs.com/package/react-native-android-location-services-dialog-box
install
npm install react-native-android-location-services-dialog-box --save
js
import LocationServicesDialogBox from "react-native-android-location-services-dialog-box";
LocationServicesDialogBox.checkLocationServicesIsEnabled({
message: "Use Location ?",
ok: "YES",
cancel: "NO"
}).then(function(success) {
console.log(success); // success => "enabled"
).catch((error) => {
console.log(error.message); // error.message => "disabled"
});

Do a watchPosition or getCurrentPosition, Your second argument is a callback in case of failure, and its argument is the reason it failed.
So, from there you know when and why it fails. You'll need an if for the reason if you only want to act on 'location disabled' and not 'request timed out' for example.
You can use the callback to add a flag to your state for example.

You can also try out these 2 libraries:
https://github.com/c19354837/react-native-system-setting
https://github.com/efkan/react-native-system-settings
What I have is the following:
I get this error code 1 or error message: 'No location provider available.' when my phone location service is turned off.
Other time, when it times out, I get this error code 3 or error message: 'Location request timed out.'.
Thus, based on the error code and message, I display different toasts to inform the user.
You can refer to this position error doc on MDN

I was able to solve this by displaying an error message when the location could not be retrieved.
(error) => {
this.setState({servicesOn: false}),
console.log(error);
},

Related

Flutter - programmatically close application when init failed

My app performs some checks inside main() function, before runApp( MyApp());, most important is load app configuration from Firebase. If failed, there's no any way to run not configured app. So I want to show message box saying "app configuration load failed", then after user press OK, exit app.
What is preferred way to exit app in that case? I need it working on iOS and Android.
If you need UI, I would suggest still calling runApp but on a different widget:
if(success){
runApp(MyApp());
} else {
runApp(ErrorApp());
}
For closing a flutter app programmatically, the best way is using SystemNavigator.pop() but if that didn't work, you can try exit(0).

Default FirebaseApp is not initialized in this process - React Native - Local Notification

(Before you mark this question as duplicate, please read the whole question)
I am using react-native-push-notification library in my React-Native Application.
In my app I have not initialized Firebase because I do not need any remote notifications as of now. Currently I am scheduling a local notification and when the notification comes I am expecting user actions from it. Based on the button pressed I want to make appropriate API calls.
So to achieve this I used PushNotification.configure method, but it is throwing me "Default FirebaseApp is not initialized in this process. Make sure Firebase is initialised" error.
I am being forced to use Firebase, when I have absolutely no need of it.
Could someone please help me out here, I have been searching for answer from past 2 days. I am quite new to React Native and I am still learning it. Any help is appreciated.
Thanks
Finally I found the solution, it was actually in the documentation itself but I somehow over looked.
I will post it here because I am sure there's someone like me out there.
We are supposed to add requestPermissions: Platform.OS === 'ios', in PushNotification.configure like this:
PushNotification.configure({
onNotification: function (notification) {
console.log('LOCAL NOTIFICATION ==>', notification);
},
// This line solves the problem that I was facing.
requestPermissions: Platform.OS === 'ios',
});

Ionic Android permission always returns false

I am using ionic 3.15 and trying to use the permissions plugin.
Using the code in the docs as mentioned here.
I have the code as shown in the docs but will mention it again here.
this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.CAMERA).then(
success => console.log('Permission granted'),
err => this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.CAMERA)
);
I always get hasPermission: false.
I do not get any prompt for turning the permissions on.
What is to be done here I am clueless.
Thanks.
You need to add something along the lines of:
this.androidPermissions.requestPermissions(
[
this.androidPermissions.PERMISSION.READ_EXTERNAL_STORAGE,
this.androidPermissions.PERMISSION.WRITE_EXTERNAL_STORAGE
]
);
You check to see if you have permissions, if it is false, then execute the above code to prompt the user to allow.
It seems like you have to make sure that you are using the permission you requested. Otherwise no prompt will be shown and the androidPermissions.requestPermissions will immediately return false.
From the docs:
Android 26 and above: due to Android 26's changes to permissions handling (permissions are requested at time of use rather than at runtime,) if your app does not include any functions (eg. other Ionic Native plugins) that utilize a particular permission, then requestPermission() and requestPermissions() will resolve immediately with no prompt shown to the user. Thus, you must include a function utilizing the feature you would like to use before requesting permission for it.

IBM Worklight 6.0 - WL.Client.Push.subscribe callbacks not fired

I am having issues with push notifications on Android,
My main issue is that WL.Client.Push.subscribe is called, then neither success or failure callback is called.
if (WL.Client.Push){
WL.Client.Push.onReadyToSubscribe = function(){
alert("onReadyToSubscribe");
if (isPushSubscribed() == false) {
doSubscribe();
}
};
}
function doSubscribe(){
alert("doSubscribe");
WL.Client.Push.subscribe("myPush", {
onSuccess: doSubscribeSuccess,
onFailure: doSubscribeFailure
});
}
function doSubscribeSuccess(){
WL.Logger.debug("doSubscribeSuccess");
alert("doSubscribeSuccess");
}
function doSubscribeFailure(){
WL.Logger.debug("doSubscribeFailure");
alert("doSubscribeFailure");
}
This code was working well few days before, and I recently changed my Android package name and my android displayed name (and also the used api key and project number in application descriptor).
Can this be a Worklight issue? Is it normal to not have any failure or success callback?
I also tried to debug with remote inspector (WEINRE) and there is no javascript error causing this behaviour.
Does any one have an idea why this occurs?
Is there something wrong within my code?
Edit:
I tested again my iOS version, and it's working fine. For Android, I am still having this issue.
onReadyToSubscribe is fired only at app launch on android, but not after user login.
in iOS it is fired after app launch (without subscribe failure or success callback) and also after login (with working subscribe callback).
Edit 2:
in android logs, I can see when I login :
W/CordovaPlugin(26319): Attempted to send a second callback for ID: Push924901903
Are you getting onReadyToSubscribe callback? Log?

Phonegap watchPosition not working correctly

I've tried the following in a number of 2.x android devices with phonegap version 2.0.0:
var onSuccess = function(position){
console.log("Success");
}
var onError = function(error){
console.log("Error");
}
gpsTrackWatchID = navigator.geolocation.watchPosition(
onSuccess,
onError,
{
enableHighAccuracy: true,
maximumAge: 5000,
timeout: 10000,
}
);
If I use the device indoors where there is no GPS signal onError is never called. The documentation states:
If the geolocationSuccess callback is not invoked within this time,
the geolocationError callback will be invoked with a
PositionError.TIMEOUT error code.
I've tried debugging this by putting a console.log in the watchPosition function in cordova.js, but the function seems never to be getting called. I wonder if the browser's native function is being executed instead of phonegap's?
First thing First, Do You set persmission in AndroidManifest correctly? Could You post the file here - especially the persission section?
Secondly, do You use Android 2.3.3 - if Yes, then You must use SDK with Google Maps APi as in normal API GPS location provider don't work.
Ant the last, do You have any JS errors on console, when registering for GPS location?

Categories

Resources