1. 程式人生 > >Android極光推送(Android studio 3.0+)

Android極光推送(Android studio 3.0+)

使用步驟:

Step 1.建立應用:

進入極光控制檯後,點選“建立應用”按鈕,進入建立應用的介面。
填上你的應用程式的名稱以及應用包名這二項就可以了,
最後點選最下方的 “建立我的應用”按鈕,建立應用完畢。

這裡寫圖片描述

這裡寫圖片描述

這裡寫圖片描述

這裡寫圖片描述

Step 2. 根目錄的主 gradle 中預設配置了jcenter支援(預設支援ndk),所以直接在 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 { ...... compile 'cn.jiguang.sdk:jpush:3.1.1' // 此處以JPush 3.1.1 版本為例。 compile 'cn.jiguang.sdk:jcore:1.1.9' // 此處以JCore 1.1.9 版本為例。 ...... }

Step 3.測試:(在清單檔案進行註冊application)

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);

    }
}

執行成功
這裡寫圖片描述

Step 4.在本地的 AndroidManifest 中定義同名的元件並配置想要的屬性,然後用 xmlns:tools 來控制本地元件覆蓋 jcenter 上的元件

<!-- 替換原生極光推送接收器 -->
<receiver
    android:name=".jpush.MyReceiver"
    android:enabled="true"
    android:exported="false"
    tools:node="replace">
        <intent-filter>
            <action android:name="cn.jpush.android.intent.REGISTRATION" /> <!-- Required  使用者註冊SDK的intent -->
            <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" /> <!-- Required  使用者接收SDK訊息的intent -->
            <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" /> <!-- Required  使用者接收SDK通知欄資訊的intent -->
            <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /> <!-- Required  使用者開啟自定義通知欄的intent -->
            <action android:name="cn.jpush.android.intent.CONNECTION" /> <!-- 接收網路變化 連線/斷開 since 1.6.3 -->
            <category android:name="修改成自己app的包名" />
        </intent-filter>
</receiver>
public class MyReceiver extends BroadcastReceiver {

    private static final String TAG = "JIGUANG";

    // RegistrationID 定義
    // 集成了 JPush SDK 的應用程式在第一次成功註冊到 JPush 伺服器時,
    // JPush 伺服器會給客戶端返回一個唯一的該裝置的標識 - RegistrationID。
    // JPush SDK 會以廣播的形式傳送 RegistrationID 到應用程式。
    // 應用程式可以把此 RegistrationID 儲存以自己的應用伺服器上,
    // 然後就可以根據 RegistrationID 來向裝置推送訊息或者通知。
    public static String regId;

    @Override
    public void onReceive(Context context, Intent intent) {

        try {

            Bundle bundle = intent.getExtras();
            Log.d(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));

            if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
                regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
                Log.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);
                //send the Registration Id to your server...

            } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
                Log.d(TAG, "[MyReceiver] 接收到推送下來的自定義訊息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));

                // 對應極光後臺的 - 自定義訊息  預設不會出現在notification上 所以一般都選用傳送通知
            } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
                Log.d(TAG, "[MyReceiver] 接收到推送下來的通知");
                int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
                Log.d(TAG, "[MyReceiver] 接收到推送下來的通知的ID: " + notifactionId);

            } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
                Log.d(TAG, "[MyReceiver] 使用者點選打開了通知");


                //開啟自定義的Activity
                //Intent i = new Intent(context,ContentActivity.class);
                //i.putExtras(bundle);
                //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                //context.startActivity(i);

            } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
                Log.d(TAG, "[MyReceiver] 使用者收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
                //在這裡根據 JPushInterface.EXTRA_EXTRA 的內容處理程式碼,比如開啟新的Activity, 開啟一個網頁等..

            } else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
                boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
                Log.w(TAG, "[MyReceiver]" + intent.getAction() + " connected state change to " + connected);
            } else {
                Log.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());
            }

        }catch (Exception e){

        }


    }


    // 列印所有的 intent extra 資料
    private static String printBundle(Bundle bundle) {

        StringBuilder sb = new StringBuilder();
        for (String key : bundle.keySet()) {
            if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
                sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
            } else if (key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)) {
                sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
            } else if (key.equals(JPushInterface.EXTRA_EXTRA)) {
                if (bundle.getString(JPushInterface.EXTRA_EXTRA).isEmpty()) {
                    Log.i(TAG, "This message has no Extra data");
                    continue;
                }
                try {
                    JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
                    Iterator<String> it = json.keys();

                    while (it.hasNext()) {
                        String myKey = it.next().toString();
                        sb.append("\nkey:" + key + ", value: [" +
                                myKey + " - " + json.optString(myKey) + "]");
                    }
                } catch (JSONException e) {
                    Log.e(TAG, "Get message extra JSON error!");
                }

            } else {
                sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
            }
        }
        return sb.toString();
    }


}

Step 5.設定唯一標識(新別名alias),根據別名推送

//登入時進行繫結
JPushInterface.setAlias(LoginActivity.this,int sequence,唯一標識-一般userid);
//退出登入進行解除繫結(即不再接收根據別名推送的訊息)
JPushInterface.deleteAlias(ContentActivity.this,int sequence);

Step 6.網路許可權

//這個一點要+
<uses-permission android:name="android.permission.INTERNET"/>