A React Native wrapper for Huawei Mobile Services
This wrapper includes;
$ npm install @el173/react-native-hms --save
$ react-native link @el173/react-native-hms
maven {url 'https://developer.huawei.com/repo/'}
classpath 'com.huawei.agconnect:agcp:1.2.1.301'
apply plugin: 'com.huawei.agconnect'
implementation 'com.huawei.hms:hianalytics:4.0.1.300'
android/app/src/main/java/[...]/MainActivity.java
import com.el173.HmsPackage;
to the imports at the top of the filenew HmsPackage()
to the list returned by the getPackages()
methodandroid/settings.gradle
:
include ':@el173_react-native-hms'
project(':@el173_react-native-hms').projectDir = new File(rootProject.projectDir,'../node_modules/@el173/react-native-hms/android')
android/app/build.gradle
:
implementation project(':@el173_react-native-hms')
agconnect-services.json
configuration file.agconnect-services.json
file to your android/app
directory.<!--Network permission -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Check the network status. -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!--AppGallery channel number query permission. -->
<uses-permission android:name="com.huawei.appmarket.service.commondata.permission.GET_COMMON_DATA" />
# exclude HUAWEI Analytics Kit from obfuscation
-keep class com.hianalytics.android.**{*;}
-keep class com.huawei.updatesdk.**{*;}
-keep class com.el173.analytics.**{*;}
-keep interface com.huawei.hms.analytics.type.HAEventType{*;}
-keep interface com.huawei.hms.analytics.type.HAParamType{*;}
-keep class com.huawei.hianalytics.CommonUtil{*;}
-keep class com.huawei.hianalytics.HaInterfaceModule{*;}
-keep class com.huawei.hianalytics.HaInterfacePackage{*;}
-keep class com.huawei.hianalytics.HaSdkInitProvider{*;}
-keep class com.huawei.hianalytics.LifeCycleListener{*;}
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
import com.el173.map.HMSMapsPackage;
packages.add(new HMSMapsPackage());
-ignorewarnings
-keepattributes *Annotation*
-keepattributes Exceptions
-keepattributes InnerClasses
-keepattributes Signature
-keepattributes SourceFile,LineNumberTable
-keep class com.hianalytics.android.**{*;}
-keep class com.huawei.updatesdk.**{*;}
-keep class com.el173.analytics.**{*;}
import HMS from '@el173/react-native-hms';
isGMS = async () => { const isGMS = await HMS.Utils.isGMSAvailable(); console.log('Your device is running on Google Mobile Services', isGMS); return isGMS; };
isHMS = async () => { const isHMS = await HMS.Utils.isHMSAvailable(); console.log('Your device is running on Huawei Mobile Services', isHMS); return isHMS; };
* Analytics Kit
```javascript
import HMS from '@el173/react-native-hms';
setHmsEvent = () => {
const eventObj={
testString:'StrContent',
testInt:20,
testDouble:2.2,
testBoolean:false
}
HMS.Analytics.onEvent('newTestEvent',eventObj);
}
Read more about HMS analytics: https://developer.huawei.com/consumer/en/doc/development/HMS-Guides/react-native-development
import HMS from '@el173/react-native-hms';
const MapView = HMS.MapView;
<MapView
initialRegion={{
latitude: 37.78825,
longitude: -122.4324,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
}}
/>
Import other available map components
import {Marker, Polyline, ...} from '@el173/react-native-hms';
Generate HCM Token
import HMS from '@el173/react-native-hms';
getToken = () => {
HMS.InstanceId.getToken(
(retcode, token) => {
let msg = '';
if (retcode == 0) {
// Handle notification here
msg = msg + 'getToken result:' + token + '\n';
} else {
msg = msg + 'getToken exception, error:' + token + '\n';
}
console.log(msg);
});
}
Register HCM notification listener and token change listener
import {RNRemoteMessage} from '@el173/react-native-hms';
createHCMNotificationListener = onRegister => {
const eventEmitter = new NativeEventEmitter(NativeModules.ToastExample);
this.listenerHMCPushMsg = eventEmitter.addListener(
'PushMsgReceiverEvent',
event => {
const RNMessageParserSingled = new RNRemoteMessage(event.msg);
const msg =
RNMessageParserSingled.getData() +
'\n' +
RNMessageParserSingled.getDataOfMap() +
'\n' +
RNMessageParserSingled.getMessageId() +
'\n';
console.log('HCM notification received - ' + msg);
},
);
const eventTokenEmitter = new NativeEventEmitter(
NativeModules.ToastExample,
);
this.listenerHCMToken = eventTokenEmitter.addListener(
'PushTokenMsgReceiverEvent',
event => {
// Handle notification here
console.log('HCM token changed - ' + event.token);
},
);
}
unHCMRegister = () => {
this.listenerHMCPushMsg();
this.listenerHCMToken();
};
}
HMS Push FAQ : https://developer.huawei.com/consumer/en/doc/development/HMS-Guides/push-faq-v4
Read more about HMS push: https://developer.huawei.com/consumer/en/doc/development/HMS-Guides/push-rn-dev-guide