SDK-極光推送安卓

本文介紹cocos2dx項目里用自動集成方式集成極光推送安卓SDK 。官方文檔

  1. 除了官方文檔還參考了這篇,感謝分享。
  2. 注冊極光賬號并創(chuàng)建應用。
  3. 確認 android studio 的 Project 根目錄的主 gradle 中配置了 jcenter 支持。
  4. 在 module 的 gradle 中添加依賴和 AndroidManifest 的替換變量。
    android {
        ......
        defaultConfig {
            applicationId "com.xxx.xxx" //JPush 上注冊的包名.
            ......
    
            ndk {
                //選擇要添加的對應 cpu 類型的 .so 庫。
                abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a'
                // 還可以添加 'x86', 'x86_64', 'mips', 'mips64'
            }
    
            manifestPlaceholders = [
                JPUSH_PKGNAME : applicationId,
                JPUSH_APPKEY : "你的 Appkey ", //JPush 上注冊的包名對應的 Appkey.
                JPUSH_CHANNEL : "developer-default", //暫時填寫默認值即可.
            ]
            ......
        }
        ......
    }
    
    dependencies {
        ......
        implementation 'cn.jiguang.sdk:jpush:3.5.6'  // 此處以JPush 3.5.6 版本為例。
        implementation 'cn.jiguang.sdk:jcore:2.2.8'  // 此處以JCore 2.2.8 版本為例。
        ......
    }
    
  5. 如果你使用的 JCore 是 2.0.0 及以上的版本,需要額外在 Androidmanifest 中配置一個Service,以在更多手機平臺上獲得更穩(wěn)定的支持,示例如下。(JCore1.x版本不需要)
    <application
        android:label="@string/app_name"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher">
    
        ......
    
        <!-- Since JCore2.0.0 Required SDK核心功能-->
        <!-- 可配置android:process參數(shù)將Service放在其他進程中;android:enabled屬性不能是false -->
        <!-- 這個是自定義Service,要繼承極光JCommonService,可以在更多手機平臺上使得推送通道保持的更穩(wěn)定 -->
        <service android:name="org.cocos2dx.cpp.jpush.PushService"
            android:enabled="true"
            android:exported="false"
            android:process=":pushcore">
            <intent-filter>
                <action android:name="cn.jiguang.user.service.action" />
            </intent-filter>
        </service>
    
        ......
        
    </application>
    
    //org/cocos2dx/cpp/jpush這個目錄下建一個PushService.java文件,內(nèi)容如下
    package org.cocos2dx.cpp.jpush;
    import cn.jpush.android.service.JCommonService;
    public class PushService extends JCommonService {
    }
    
  6. 從JPush3.0.7開始,需要配置繼承JPushMessageReceiver的廣播,原來如果配了MyReceiver現(xiàn)在可以棄用。示例如下。
    <!-- Required since 3.0.7 -->
    <!-- 新的 tag/alias 接口結果返回需要開發(fā)者配置一個自定的廣播 -->
    <!-- 3.3.0開始所有事件將通過該類回調(diào) -->
    <!-- 該廣播需要繼承 JPush 提供的 JPushMessageReceiver 類, 并如下新增一個 Intent-Filter -->
    <receiver
        android:name="org.cocos2dx.cpp.jpush.MyJPushMessageReceiver"
        android:enabled="true"
        android:exported="false" >
        <intent-filter>
            <action android:name="cn.jpush.android.intent.RECEIVE_MESSAGE" />
            <category android:name="com.test.a" />
        </intent-filter>
    </receiver>
    
    package org.cocos2dx.cpp.jpush;
    
    import android.content.Context;
    
    import cn.jpush.android.api.JPushMessage;
    import cn.jpush.android.service.JPushMessageReceiver;
    /**
    * 自定義JPush message 接收器,包括操作tag/alias的結果返回(僅僅包含tag/alias新接口部分)
    * */
    public class MyJPushMessageReceiver extends JPushMessageReceiver {
        @Override
        public void onTagOperatorResult(Context context, JPushMessage jPushMessage) {
            super.onTagOperatorResult(context, jPushMessage);
        }
    
        @Override
        public void onCheckTagOperatorResult(Context context, JPushMessage jPushMessage) {
            super.onCheckTagOperatorResult(context, jPushMessage);
        }
    
        @Override
        public void onAliasOperatorResult(Context context, JPushMessage jPushMessage) {
            super.onAliasOperatorResult(context, jPushMessage);
        }
    
        @Override
        public void onMobileNumberOperatorResult(Context context, JPushMessage jPushMessage) {
            super.onMobileNumberOperatorResult(context, jPushMessage);
        }
    }
    
  7. 在我們自己的Application中初始化,并設置調(diào)試開關
    ......
    import cn.jpush.android.api.JPushInterface;
    public class AppActivity extends Cocos2dxActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            ......
            // DO OTHER INITIALIZATION BELOW
            JPushInterface.setDebugMode(true);
            JPushInterface.init(this);
        }
    }
    
  8. 以上。我們改了gradle文件、AndroidManifest文件、AppActivity.java。創(chuàng)建了兩個java文件。然后就可以取極光里面發(fā)推送了。
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容