-
安装jdk最好安卓jdk1.8
-
安卓androidstudio版本要4.0以上
-
开始安卓华为给提供的androidstudio的插件
4.我们必须要注册华为联盟者帐号地址:developer.huawei.com/consumer/cn… 并且要实名认证成为开发者账号
接下是我们要选择你要集成项目,比如:我们是以推送为例子,首先是给app签名
然后我们就可以给App添加推送业务
选择好了开始工具就开始帮我们编译,然后他会报错,我们点击报错信息,这个错误是因为我们App没有绑定推送业务,根据他的提示我们继续往下走就行可以了
选择好了就编译成功了,插件帮我注册成功了,测试的时候我们有俩种方式,一种是通过网站发送,一种是可以本地通过插件发送推送通知,测试我们是否集成完成。
上面配置是一种方式,我们也可以通过传统的方法来配置
- 华为推送集成,需要给自己app签名:第一步
红颜色的代码密码重要,四个红的最好写成一样密码,黄颜色代码app年限一般写50年
蓝颜色的不重要了,可以随便写, 第二工程配置不要低于我的配置
第三步:找到自己jdk安装目录,用jdk的Keytool工具还有就是咱们签名文件
导出SHA256 ,我的位置是在C盘
蓝色是jdk的目录,红色是我用keytool工具输入命令 黄色是我们签名的时候的密码,你输入的时候是看见不到的,这个我建议你一个字一个字输入容易错,最后一个颜色是我们签名地址jks文件位置, 这样我们就拿到SHA256然后我们就能去华为官网网配置我们的应用了。
接下来是我们项目中代码具体展示:
-
首先工程的build.gradle
buildscript { repositories { google() jcenter() //这个是华为的仓库 1 maven { url 'https://developer.huawei.com/repo/' } } dependencies { classpath "com.android.tools.build:gradle:4.1.1" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files //这个是华为需要 2 classpath 'com.huawei.agconnect:agcp:1.4.2.300' } } allprojects { repositories { google() jcenter() //这个是华为的仓库 3 maven { url 'https://developer.huawei.com/repo/' } } } task clean(type: Delete) { delete rootProject.buildDir } -
然后是你主项目module的build.gradle
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.qf.push"
minSdkVersion 17
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
//华为配置信息 1
signingConfigs {
config {
storeFile file('D:\worker\Push\push.jks')
storePassword '123456'
keyPassword '123456'
keyAlias 'qf'
}
}
buildTypes {
debug {
signingConfig signingConfigs.config
}
release {
signingConfig signingConfigs.config
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
//华为配置推送包信息 2
implementation 'com.huawei.hms:push:5.1.1.301'
}
//华为配置 3
apply plugin: 'com.huawei.agconnect'
-
最后是我们的androidMainfest
<!-- 第一步加网络权限 1 --> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.MyPush"> <meta-data android:name="push_kit_auto_init_enabled" android:value="true"/> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- 加入服务第二步 2 --> <service android:name=".DemoHmsMessageService" android:exported="false"> <intent-filter> <action android:name="com.huawei.push.action.MESSAGING_EVENT"/> </intent-filter> </service> </application> <!-- 加入服务第三步 2 --> <queries> <intent> <action android:name="com.huawei.hms.core.aidlservice" /> </intent> </queries> -
开始进入的第一个activity,例如:我的是MainActivity代码如下
private static final String TAG = "千锋"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getToken(); } //获取token信息 private void getToken() { // Create a thread. new Thread() { @Override public void run() { try { // Obtain the app ID from the agconnect-service.json file. String appId = "104524303"; // 输入token标识"HCM" String tokenScope = "HCM"; String token = HmsInstanceId.getInstance(MainActivity.this).getToken(appId, tokenScope); Log.i(TAG, "get token: " + token); // Check whether the token is empty. if (!TextUtils.isEmpty(token)) { sendRegTokenToServer(token); } } catch (ApiException e) { Log.e(TAG, "get token failed, " + e); } } }.start(); } private void sendRegTokenToServer(String token) { Log.i(TAG, "sending token to server. token:" + token); }