跨平台应用开发进阶(九) :uni-app 原生APP-本地打包集成极光推送(JG-JPUSH)详细教程

x33g5p2x  于2022-04-23 转载在 其他  
字(6.7k)|赞(0)|评价(0)|浏览(695)

一、前言

因项目需求,需要uni-app 原生APP-本地打包集成极光推送,现将集成过程梳理得出此文。

二、集成

2.1 uni-app 项目集成至 Android Studio

2.1.1 拷贝HbuilderX uni-app 源码至 AS

hbuilderX中使用本地打包生成android资源如下:

构建空的android项目构建如下文件结构,apps下面放hbuilderX本地打包生成的资源文件。

2.1.2 下载最新 SDK

Dcloud里下载最新的SDK

下载解压后目录如下

其中,

  • HBuilder-Hello:是HelloH5打包App的示例,可以用AS打包成APK
  • HBuilder-Integrate-AS:是HBuilder 5+ SDK 集成AS的示例;
  • SDK:是HBuilder SDK库文件
  • UniPlugin-Hello-AS:是开发插件并集成到Vue的示例。
2.1.3 复制 HBuilder-Hello 项目 data 到自己项目对应目录中

2.1.4 复制 HBuilder-Hello 项目 libs 下的三个文件到自己项目对应目录中

2.1.5 修改 dcloud_control.xml 配置文件

注意⚠️:appidHbuilderX uni-app项目标识。

  1. <hbuilder>
  2. <apps>
  3. <app appid="__UNI__1DA6F85" appver=""/>
  4. </apps>
  5. </hbuilder>
2.1.6 androidmanifest.xml 中添加 activity 节点

添加如下内容

  1. <activity
  2. android:name="io.dcloud.PandoraEntry"
  3. android:configChanges="orientation|keyboardHidden|keyboard|navigation"
  4. android:label="@string/app_name"
  5. android:launchMode="singleTask"
  6. android:hardwareAccelerated="true"
  7. android:theme="@style/TranslucentTheme"
  8. android:screenOrientation="user"
  9. android:windowSoftInputMode="adjustResize" >
  10. <intent-filter>
  11. <action android:name="android.intent.action.MAIN" />
  12. <category android:name="android.intent.category.LAUNCHER" />
  13. </intent-filter>
  14. </activity>
  15. <activity
  16. android:name="io.dcloud.PandoraEntryActivity"
  17. android:launchMode="singleTask"
  18. android:configChanges="orientation|keyboardHidden|screenSize|mcc|mnc|fontScale|keyboard"
  19. android:hardwareAccelerated="true"
  20. android:permission="com.miui.securitycenter.permission.AppPermissionsEditor"
  21. android:screenOrientation="user"
  22. android:theme="@style/DCloudTheme"
  23. android:windowSoftInputMode="adjustResize">
  24. <intent-filter>
  25. <category
  26. android:name="android.intent.category.DEFAULT" />
  27. <category
  28. android:name="android.intent.category.BROWSABLE" />
  29. <action
  30. android:name="android.intent.action.VIEW" />
  31. <data
  32. android:scheme="h56131bcf" />
  33. </intent-filter>
  34. </activity>

经过以上配置,可实现hbuilderx本地离线打包android项目,生成APK安装包。

首先下载jpush-hbuilder-demo Demo 应用。

拷贝 ./android/app/src/main/java/io.dcloud.feature.jpush 文件夹至你 Android Studio 工程的 /src/main/java/ 目录下。

拷贝 ./jpush.js 到你 Android Studio 工程的 /assets/apps/HBuilder应用名/js/ 下。

/assets/apps/你的应用名/www/manifest.json 文件中添加:

  1. "Push": {
  2. "description": "消息推送"
  3. }

/assets/data/dcloud_properties.xml 中添加(如果已存在 Push feature,可以直接修改):

  1. <feature
  2. name="Push"
  3. value="io.dcloud.feature.jpush.JPushService" >
  4. </feature>

app/build.gradle 中添加:

  1. android {
  2. ...
  3. defaultConfig {
  4. applicationId "com.xxx.xxx" // JPush 上注册的包名.
  5. ...
  6. ndk {
  7. // 选择要添加的对应 cpu 类型的 .so 库。
  8. abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a'
  9. // 还可以添加 'x86', 'x86_64', 'mips', 'mips64'
  10. }
  11. manifestPlaceholders = [
  12. JPUSH_PKGNAME : applicationId,
  13. JPUSH_APPKEY : "应用的 AppKey", // JPush上注册的包名对应的 appkey
  14. JPUSH_CHANNEL : "developer-default", // 暂时填写默认值即可
  15. ]
  16. ...
  17. }
  18. ...
  19. }
  20. dependencies {
  21. ...
  22. compile 'cn.jiguang.sdk:jpush:3.3.4' // 此处以JPush 3.3.4 版本为例。
  23. compile 'cn.jiguang.sdk:jcore:2.1.2' // 此处以JCore 2.1.2 版本为例。
  24. ...
  25. }

AndroidManifest.xml 中添加:

  1. <!-- since 3.3.0 Required SDK 核心功能-->
  2. <!-- 可配置android:process参数将PushService放在其他进程中 -->
  3. <!--User defined. For test only 继承自cn.jpush.android.service.JCommonService-->
  4. <service android:name="io.dcloud.feature.jpush.PushService"
  5. android:process=":pushcore">
  6. <intent-filter>
  7. <action android:name="cn.jiguang.user.service.action" />
  8. </intent-filter>
  9. </service>
  10. <!-- User defined. For test only 用户自定义接收消息器,3.0.7开始支持,目前新tag/alias接口设置结果会在该广播接收器对应的方法中回调-->
  11. <!--since 3.3.0 接收JPush相关事件-->
  12. <receiver android:name="io.dcloud.feature.jpush.PushMessageReceiver">
  13. <intent-filter>
  14. <action android:name="cn.jpush.android.intent.RECEIVE_MESSAGE" />
  15. <category android:name="${applicationId}"></category>
  16. </intent-filter>
  17. </receiver>
  18. <receiver
  19. android:name="io.dcloud.feature.jpush.JPushReceiver"
  20. android:enabled="true"
  21. android:exported="false">
  22. <intent-filter>
  23. <action android:name="cn.jpush.android.intent.REGISTRATION" /> <!-- Required 用户注册SDK的 intent -->
  24. <action android:name="cn.jpush.android.intent.UNREGISTRATION" />
  25. <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" /> <!-- Required 用户接收SDK消息的 intent -->
  26. <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" /> <!-- Required 用户接收SDK通知栏信息的 intent -->
  27. <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /> <!-- Required 用户打开自定义通知栏的 intent -->
  28. <action android:name="cn.jpush.android.intent.ACTION_RICHPUSH_CALLBACK" /> <!-- Optional 用户接受 Rich Push Javascript 回调函数的intent -->
  29. <action android:name="cn.jpush.android.intent.CONNECTION" /> <!-- 接收网络变化 连接/断开 since 1.6.3 -->
  30. <category android:name="${JPUSH_PKGNAME}" />
  31. </intent-filter>
  32. </receiver>

三、遇到的问题及解决方案

3.1 libs中sdk版本与app sdk版本不一致

  1. uses-sdk:minSdkVersion 16 cannot be smaller than version 19 declared in library [weex_videoplayer-release.aar] /Users/huaqiangsun/.gradle/caches/transforms-2/files-2.1/22285093409dba775963f444b0533dd8/weex_videoplayer-release/AndroidManifest.xml as the library might be using APIs not available in 16Suggestion: use a compatible library with a minSdk of at most 16,or increase this project's minSdk version to at least 19,or use tools:overrideLibrary="io.dcloud.feature.weex_media" to force usage (may lead to runtime failures)

解决措施:需要在build.gradle(Moudle app)中minSdkVersion改为 19。

3.2 Suggestion: add ‘tools:replace=“android:resource”’ to element at AndroidManifest.xml to override

  1. Attribute meta-data#android.support.FILE_PROVIDER_PATHS@resource value=(@xml/filepaths) from [lib.5plus.base-release.aar] AndroidManifest.xml:243:17-61
  2. is also present at [torch-plgdtsdk-5.17.3157.aar] AndroidManifest.xml:48:17-57 value=(@xml/torch_file_paths).Suggestion: add 'tools:replace="android:resource"' to <meta-data> element at AndroidManifest.xml to override.

解决措施:在AndroidManifest.xml的根标签下加上 xmlns:tools="http://schemas.android.com/tools",然后在application标签下加入tools:replace="android:name"

  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. package="com.**">
  4. <application android:name="com.**.App"
  5. android:allowBackup="true"
  6. android:icon="@mipmap/app_icon"
  7. android:label="@string/app_name"
  8. android:supportsRtl="true"
  9. android:theme="@style/AppTheme"
  10. tools:replace="android:name">
  11. </application>
  12. </manifest>

四、拓展阅读

相关文章