1. 程式人生 > >極光推送傻瓜版配置(Android Studio)

極光推送傻瓜版配置(Android Studio)

Step1 : 

    build.gradle(Project : 專案名)中加入以下紅色部分配置(新 Android Studio 中預設就有)

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
} } task clean(type: Delete) { delete rootProject.buildDir }

Step2 : 開啟極光推送官網(  https://www.jiguang.cn/  )建立一個新的 APP,


應用名稱隨便寫



獲取 APP_key


獲取應用包名:


輸入你的 Android Studio 專案的包名 (build.gradle(Module : app)中的 applicationId)



Step3 : 開啟 build.gradle (Module : app) 在 defaultConfig

加入如下配置 :

ndk {
    //選擇要新增的對應cpu型別的.so庫。
    abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a'
    // 還可以新增 'x86', 'x86_64', 'mips', 'mips64'
}
manifestPlaceholders = [
        JPUSH_PKGNAME : applicationId,

JPUSH_APPKEY : "這裡寫在極光官網的那個 APP-Key", //JPush上註冊的包名對應的appkey.

JPUSH_CHANNEL : "developer-default"
, //暫時填寫預設值即可.]
Step4 : 開啟 build.gradle (Module : app),加入紅色配置
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    compile 'cn.jiguang.sdk:jpush:3.1.1'  // 此處以JPush 3.1.1 版本為例。
    compile 'cn.jiguang.sdk:jcore:1.1.9'  // 此處以JCore 1.1.9 版本為例。
}

 
Step5 : 建立一個Java 類:
public class ExampleApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        JPushInterface.setDebugMode(true);     // 設定開啟日誌,釋出時請關閉日誌
        JPushInterface.init(this);            // 初始化 JPush
    }
}
Step6 : 把這個類加入到 AndroidManifest.xml
Step7 : 把 JPush 用到一個 Service 注入進來

<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/AppTheme"
    android:name="msgtest.cybersmart.com.msg2.ExampleApplication"
    >
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

    <service android:name="cn.jpush.android.service.PushService"
             android:process=":multiprocess"
             tools:node="replace" >
    </service>
</application>