1. 程式人生 > 其它 >極光推送配置

極光推送配置

技術標籤:android

一 、新增工程配置
1、推薦使用 jcenter 選擇最新版本方式整合
在 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 ", //極光開發平臺上註冊的包名對應的appkey.
                JPUSH_CHANNEL : "developer-default", //暫時填寫預設值即可.
            ]
            ......
       }
        ......
   }

    dependencies {
        ......

        compile 'cn.jiguang.sdk:jpush:3.8.5'  // 此處以JPush 3.8.5 版本為例。
        compile 'cn.jiguang.sdk:jcore:2.5.5'  // 此處以JCore 2.5.5 版本為例。
        ......
   }

2、配置推送必須元件
注 : 如果你使用的 JCore 是 2.0.0 及以上的版本,需要額外在 Androidmanifest 中配置一個Service,以在更多手機平臺上獲得更穩定的支援,示例如下。(JCore1.x版本不需要)

 <!-- 可配置android:process引數將Service放在其他程序中;android:enabled屬性不能是false -->
 <!-- 這個是自定義Service,要繼承極光JCommonService,可以在更多手機平臺上使得推送通道保持的更穩定 -->
 <service android:name="xx.xx.XService"
         android:enabled="true"
         android:exported="false"
         android:process=":pushcore">
         <intent-filter>
             <action android:name="cn.jiguang.user.service.action" />
         </intent-filter>
 </service>    

注 : **從JPush3.0.7開始,需要配置繼承JPushMessageReceiver的廣播,原來如果配了MyReceiver現在可以棄用。示例如下。

 <!-- 新的 tag/alias 介面結果返回需要開發者配置一個自定的廣播 -->
 <!-- 3.3.0開始所有事件將通過該類回撥 -->
 <!-- 該廣播需要繼承 JPush 提供的 JPushMessageReceiver 類, 並如下新增一個 Intent-Filter -->
 <receiver
       android:name="自定義 Receiver"
       android:enabled="true" 
       android:exported="false" >
       <intent-filter>
            <action android:name="cn.jpush.android.intent.RECEIVE_MESSAGE" />
            <category android:name="您應用的包名" />
       </intent-filter>
 </receiver>

3、注意事項
jcenter整合其他注意事項,檢視解決方案
若需手動整合,檢視整合教程
二 、初始化推送服務
JPush SDK 提供的 API 介面,都主要集中在 cn.jpush.android.api.JPushInterface 類裡。

public class ExampleApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
JPushInterface.setDebugMode(true);
JPushInterface.init(this);
}
}