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

HMS integrate location #74

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -243,5 +243,6 @@ task copyDownloadableDepsToLibs(type: Copy) {

apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.huawei.agconnect'


13 changes: 13 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,18 @@
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<receiver
android:name="com.huawei.hms.rn.location.RNLocationBroadcastReceiver"
android:exported="false"
android:enabled="true">
<intent-filter>
<action android:name="${applicationId}.ACTION_HMS_LOCATION" />
<action android:name="${applicationId}.ACTION_HMS_ACTIVITY_IDENTIFICATION" />
<action android:name="${applicationId}.ACTION_HMS_ACTIVITY_CONVERSION" />
<action android:name="${applicationId}.ACTION_HMS_GEOFENCE" />
</intent-filter>
</receiver>
<service android:name="com.huawei.hms.rn.location.RNTaskService" />

</application>
</manifest>
6 changes: 6 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ buildscript {
jcenter()
google()
maven { url 'https://jitpack.io' }
maven { url 'https://developer.huawei.com/repo/' }

}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.2'
classpath('com.google.gms:google-services:4.3.3')
classpath 'com.huawei.agconnect:agcp:1.2.0.300'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Expand All @@ -50,6 +54,8 @@ allprojects {
// react-native-background-fetch
url("${project(':react-native-background-fetch').projectDir}/libs")
}
maven { url 'https://developer.huawei.com/repo/' }

}
}
subprojects {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@emotion/core": "^10.0.28",
"@emotion/native": "^10.0.27",
"@react-hook/throttle": "^1.0.12",
"@hmscore/react-native-hms-location": "^5.0.4-300",
"@react-native-community/async-storage": "^1.6.1",
"@react-native-community/clipboard": "^1.1.0",
"@react-native-community/image-editor": "^2.3.0",
Expand Down
100 changes: 100 additions & 0 deletions src/services/background-tracking-hms.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import BackgroundGeolocation from 'react-native-background-geolocation'
import { getAnonymousHeaders } from '../api'
import { Platform } from 'react-native'
import { API_URL } from '../config'
import I18n from '../../i18n/i18n'
import DeviceInfo from 'react-native-device-info'
import HMSLocation from "@hmscore/react-native-hms-location";

class BackgroundTrackingHms {

private locationHmsRequest:HMSLocation.LocationRequest = {
priority: HMSLocation.FusedLocation.PriorityConstants.PRIORITY_BALANCED_POWER_ACCURACY,
// interval: 5*60*1000,
interval: 1000,
numUpdates: 2147483647,
fastestInterval: 600000.0,
expirationTime: 9223372036854775807.0,
expirationTimeDuration: 10000.0,
smallestDisplacement: 0.0,
maxWaitTime: 0.0,
needAddress: false,
language: "",
countryCode: "",
};

private locationHms: HMSLocation.Location

private locationSettingsRequest = {
locationRequests: [this.locationHmsRequest],
alwaysShow: false,
needBle: false,
};

private handleLocationUpdate = location => {
var locationResult:HMSLocation.LocationResult = location
this.locationHms = locationResult
console.log("Retrieved HMS Location Lat:"+locationResult.lastLocation.latitude);
console.log("Retrieved HMS Location Lat:"+locationResult.lastLocation.longitude);

};


setup(startImmediately?: boolean) {
if (startImmediately) {
this.start()
}
}

start() {
if (!this.canUseGeoLocation) {
return Promise.resolve()
}
HMSLocation.LocationKit.Native.init()
.then(() => {
console.log("HMS,Done loading")
this.startHMSLocation()
})
.catch((err: Error) => console.log('HMS,Fused Location Headless Task, data:', err.message))
}

private startHMSLocation(){
HMSLocation.FusedLocation.Native.requestLocationUpdates(10, this.locationHmsRequest)
.then(({ requestCode }) => console.log("HMS Location RequestCode"+requestCode))
.catch(err => console.log("Exception while requestLocationUpdates " + err))

HMSLocation.FusedLocation.Events.addFusedLocationEventListener(
this.handleLocationUpdate,
);
}


stop() {
if (!this.canUseGeoLocation) {
return Promise.resolve()
}
HMSLocation.FusedLocation.Events.removeFusedLocationEventListener(
this.handleLocationUpdate,
);
}

destroyLocations() {
if (!this.canUseGeoLocation) {
return Promise.resolve()
}
}

getLocation(extras: any = {}) {
if (!this.canUseGeoLocation) {
return Promise.resolve({ ...extras })
}
return this.locationHms
}

get canUseGeoLocation() {
const hasHMS = DeviceInfo.hasHmsSync()
return Platform.OS !== 'ios' && hasHMS
}
}

export const backgroundTrackingHms = new BackgroundTrackingHms()
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,11 @@
dependencies:
"@hapi/hoek" "^8.3.0"

"@hmscore/react-native-hms-location@^5.0.4-300":
version "5.0.4-300"
resolved "https://registry.yarnpkg.com/@hmscore/react-native-hms-location/-/react-native-hms-location-5.0.4-300.tgz#73948ec1621cc75e40ec556ca704d9743ba10048"
integrity sha512-FM6HGqM88XpssOXb+XtvGbjo9ihIx2rQ2l9V1i+OOiKBtJpXBq98MzT6kbmqAcq5+pw8pnttwvpxqjgmZgGv6w==

"@istanbuljs/load-nyc-config@^1.0.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz#fd3db1d59ecf7cf121e80650bb86712f9b55eced"
Expand Down