1. 程式人生 > 其它 >unity 國內sdk接入總結(android)-友盟推送SDK接入

unity 國內sdk接入總結(android)-友盟推送SDK接入

技術標籤:unity sdkunity3d

注意:一開始想接友盟Unity的SDk,但是匯入後缺少各種jar,所以最後還是接了android的,demo文件齊全

官方文件:https://developer.umeng.com/docs/67966/detail/153908

按照官方文件對接即可,

接入流程

1、專案中com.android.tools.build:gradle配置,如果版本號>=3.0.0,請使用如下配置指令碼新增遠端依賴:

//基礎元件庫依賴(必須) Push 6.2.0版本必須升級新版本Common SDK
implementation 'com.umeng.umsdk:common:9.3.0'
implementation 'com.umeng.umsdk:utdid:1.5.2'
implementation  'com.umeng.umsdk:asms:1.1.3'
implementation 'com.umeng.umsdk:crash:0.0.5'

//友盟push相關依賴(必須)
implementation 'com.umeng.umsdk:push:6.2.0'

implementation 'com.umeng.umsdk:alicloud-httpdns:1.2.5'
implementation 'com.umeng.umsdk:alicloud-utils:1.1.5'
implementation 'com.umeng.umsdk:alicloud_beacon:1.0.1'

implementation 'com.umeng.umsdk:agoo-accs:3.3.9.7-open'
implementation 'com.umeng.umsdk:agoo_networksdk:3.5.5'
implementation 'com.umeng.umsdk:agoo_tlog:3.0.0.17'
implementation 'com.umeng.umsdk:agoo_tnet4android:3.1.14.9'

2、在工程build.gradle配置指令碼中buildscript和allprojects段中新增【友盟+】sdk 新maven倉庫地址(注意位置別新增錯,不然依賴項出錯)

buildscript {
    repositories {
        google()
        jcenter()
        //友盟新增
        maven { url 'https://dl.bintray.com/umsdk/release' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.4'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()
        //友盟新增
        maven { url 'https://dl.bintray.com/umsdk/release' }
    }
}

3、【特別注意】新增aranger庫的依賴。下載aranger-x.x.x.jar庫拷貝到專案工程的libs目錄並引用該庫。

4、在AndroidManifest.xml檔案中引入許可權宣告:

<!-- 以下為需要的基本許可權,需要自行新增至您的AndroidManifest檔案中 start-->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.BROADCAST_PACKAGE_ADDED" />
    <uses-permission android:name="android.permission.BROADCAST_PACKAGE_CHANGED" />
    <uses-permission android:name="android.permission.BROADCAST_PACKAGE_INSTALL" />
    <uses-permission android:name="android.permission.BROADCAST_PACKAGE_REPLACED" />
    <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <!-- 以下為需要的基本許可權,需要自行新增至您的AndroidManifest檔案中 end-->

在AndroidManifest.xml檔案中引入相關基礎元件:

<!-- 以下為基本配置資訊,需要自行新增至您的AndroidManifest檔案中 start-->
        <service
            android:name="com.taobao.accs.ChannelService"
            android:exported="true"
            android:process=":channel">
            <intent-filter>
                <action android:name="com.taobao.accs.intent.action.SERVICE" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.taobao.accs.intent.action.ELECTION" />
            </intent-filter>
        </service>

        <service
            android:name="com.taobao.accs.data.MsgDistributeService"
            android:exported="true">
            <intent-filter>
                <action android:name="com.taobao.accs.intent.action.RECEIVE" />
            </intent-filter>
        </service>

        <receiver
            android:name="com.taobao.accs.EventReceiver"
            android:process=":channel">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_REMOVED" />
                <data android:scheme="package" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.USER_PRESENT" />
            </intent-filter>
        </receiver>

        <receiver
            android:name="com.taobao.accs.ServiceReceiver"
            android:process=":channel">
            <intent-filter>
                <action android:name="com.taobao.accs.intent.action.COMMAND" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.taobao.accs.intent.action.START_FROM_AGOO" />
            </intent-filter>
        </receiver>

        <service
            android:name="com.taobao.accs.internal.AccsJobService"
            android:permission="android.permission.BIND_JOB_SERVICE"
            android:process=":channel"/>

        <service
            android:name="com.taobao.accs.ChannelService$KernelService"
            android:process=":channel" />

        <service
            android:name="org.android.agoo.accs.AgooService"
            android:exported="true">
            <intent-filter>
                <action android:name="com.taobao.accs.intent.action.RECEIVE" />
            </intent-filter>
        </service>

        <service
            android:name="com.umeng.message.UmengIntentService"
            android:exported="true"
            android:process=":channel">
            <intent-filter>
                <action android:name="org.agoo.android.intent.action.RECEIVE" />
            </intent-filter>
        </service>

        <service
            android:name="com.umeng.message.XiaomiIntentService"
            android:exported="true"
            android:process=":channel">
            <intent-filter>
                <action android:name="org.agoo.android.intent.action.RECEIVE" />
            </intent-filter>
        </service>

        <receiver
            android:name="com.taobao.agoo.AgooCommondReceiver"
            android:exported="true"
            android:process=":channel">
            <intent-filter>
                <action android:name="${applicationId}.intent.action.COMMAND" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_REMOVED" />
                <data android:scheme="package" />
            </intent-filter>
        </receiver>

        <service
            android:name="com.umeng.message.UmengMessageIntentReceiverService"
            android:exported="true"
            android:process=":channel">
            <intent-filter>
                <action android:name="org.android.agoo.client.MessageReceiverService" />
            </intent-filter>
        </service>

        <receiver
            android:name="com.umeng.message.NotificationProxyBroadcastReceiver"
            android:exported="false" />

        <service
            android:name="com.umeng.message.UmengMessageCallbackHandlerService"
            android:permission="android.permission.BIND_JOB_SERVICE"
            android:exported="false">
            <intent-filter>
                <action android:name="com.umeng.messge.registercallback.action" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.umeng.message.enablecallback.action" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.umeng.message.disablecallback.action" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.umeng.message.message.handler.action" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.umeng.message.message.sendmessage.action" />
            </intent-filter>
        </service>

        <service
            android:name="com.umeng.message.UmengDownloadResourceService"
            android:permission="android.permission.BIND_JOB_SERVICE"
            android:exported="false" />

        <provider
            android:name="com.umeng.message.provider.MessageProvider"
            android:authorities="${applicationId}.umeng.message"
            android:exported="false">
            <grant-uri-permission android:pathPattern=".*" />
        </provider>
        <!-- 以下為基本配置資訊,需要自行新增至您的AndroidManifest檔案中 end-->

5、引入so檔案:全平臺so檔案下載地址

注意下載的so檔案要和sdk裡面平臺一致,缺少會導致deviceToken獲取失敗,最好從demo全部複製出來用,unity專案要加在 libs和jnilibs裡面

基礎介面引入

1、務必在工程的自定義Application類的onCreate()方法中做SDK程式碼初始化工作。

// 在此處呼叫基礎元件包提供的初始化函式 相應資訊可在應用管理 -> 應用資訊 中找到 http://message.umeng.com/list/apps
// 引數一:當前上下文context;
// 引數二:應用申請的Appkey(需替換);
// 引數三:渠道名稱;
// 引數四:裝置型別,必須引數,傳引數為UMConfigure.DEVICE_TYPE_PHONE則表示手機;傳引數為UMConfigure.DEVICE_TYPE_BOX則表示盒子;預設為手機;
// 引數五:Push推送業務的secret 填充Umeng Message Secret對應資訊(需替換)
UMConfigure.init(this, "替換為Appkey,服務後臺位置:應用管理 -> 應用資訊 -> Appkey", "Umeng", UMConfigure.DEVICE_TYPE_PHONE, "替換為祕鑰資訊,服務後臺位置:應用管理 -> 應用資訊 -> Umeng Message Secret");

//例如:UMConfigure.init(this, "5fbb2b49690bda19c7877192", "toutiao", UMConfigure.DEVICE_TYPE_PHONE, "6a2abdbd2938c67e698e949cc9335e3e");

2、Push註冊在UMConfigure.init方法之後註冊,註冊成功後可獲取deviceToken,進行訊息下發。呼叫程式碼如下所示:


UMConfigure.init(this, "替換為Appkey,服務後臺位置:應用管理 -> 應用資訊 -> Appkey", "Umeng", UMConfigure.DEVICE_TYPE_PHONE, "替換為祕鑰資訊,服務後臺位置:應用管理 -> 應用資訊 -> Umeng Message Secret");
//獲取訊息推送代理示例
PushAgent mPushAgent = PushAgent.getInstance(this);
//註冊推送服務,每次呼叫register方法都會回撥該介面
mPushAgent.register(new IUmengRegisterCallback() {

  @Override
  public void onSuccess(String deviceToken) {
    //註冊成功會返回deviceToken deviceToken是推送訊息的唯一標誌
    Log.i(TAG,"註冊成功:deviceToken:-------->  " + deviceToken);
  }

  @Override
  public void onFailure(String s, String s1) {
    Log.e(TAG,"註冊失敗:-------->  " + "s:" + s + ",s1:" + s1);
  }
});

混淆配置

在proguard-unity.txt新增如下混淆程式碼

-dontwarn com.umeng.**
-dontwarn com.taobao.**
-dontwarn anet.channel.**
-dontwarn anetwork.channel.**
-dontwarn org.android.**
-dontwarn org.apache.thrift.**
-dontwarn com.xiaomi.**
-dontwarn com.huawei.**
-dontwarn com.meizu.**

-keepattributes *Annotation*

-keep class com.taobao.** {*;}
-keep class org.android.** {*;}
-keep class anet.channel.** {*;}
-keep class com.umeng.** {*;}
-keep class com.xiaomi.** {*;}
-keep class com.huawei.** {*;}
-keep class com.meizu.** {*;}
-keep class org.apache.thrift.** {*;}

-keep class com.alibaba.sdk.android.**{*;}
-keep class com.ut.**{*;}
-keep class com.ta.**{*;}

-keep public class **.R$*{
   public static final int *;
}

注意事項:

1、Appkey一定得用對應包名生成出來的。

2、載的so檔案要和sdk裡面平臺一致,缺少會導致deviceToken獲取失敗,最好從demo全部複製出來用,unity專案要加在 libs和jnilibs裡面

3、無法獲取到Device Token

P_10000

問法1:註冊不成功,無法獲取到device token,並且log中顯示accs bindapp error

問法2:註冊不成功,無法獲取到device token,並且TNET_LOAD_SO_FAIL

解決方案:

1、檢查appkey和secret key是否配置正確。

2、解壓縮你的apk,看一下libs目錄裡的so資料夾,確保每個so資料夾裡都有PushSDK的so檔案。

3、PushSDK 3.x預設只提供armeabi和x86兩種so資料夾,若主工程(除PushSDK外的Module均看做是主工程)中的so資料夾與PushSDK下的so資料夾不一致,則可以有兩種方式處理(選擇一種即可):

1) 刪除主工程下多餘的so資料夾,與PushSDK下的so資料夾保持一致。

2) 在官網PushSDK下載處,下載全平臺so檔案,新增缺少的so資料夾至 PushSDK下,使PushSDK的so資料夾與主工程的so資料夾保持一致。

下載連結:https://files.alicdn.com/tpsservice/7c94e1a0a2e35f6f27038c974ad9ffbf.zip