創建應用
進入極光控制台后,點擊“創建應用”按鈕,進入創建應用的界面。 填上你的應用程序的名稱以及應用包名這二項就可以了, 最后點擊最下方的 “創建我的應用”按鈕,創建應用完畢。
創建應用
填寫應用程序的名稱以及上傳圖標
創建成功
添加應用包名
jcenter 自動集成步驟
- 確認android studio的 Project 根目錄的主 gradle 中配置了jcenter支持(基本默認支持)
buildscript {
repositories {
jcenter()
}
......
}
allprojets {
repositories {
jcenter()
}
}
- 在 module 的 gradle 中添加依賴
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 版本為例。 ...... }
- AndroidManifest替換變量(在本地的 AndroidManifest 中定義同名的組件並配置想要的屬性,然后用 xmlns:tools 來控制本地組件覆蓋 jcenter 上的組件,一般替換receiver,記得修改category屬性值)
<application ...> <!-- 替換原生極光推送接收器 --> <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="com.xxx.xxx" /> <!--JPush上注冊的包名 --> </intent-filter> </receiver> </application>
調試以及使用
- 在Application的onCreate()初始化Sdk
@Override public void onCreate() { super.onCreate(); JPushInterface.setDebugMode(true); JPushInterface.init(this); }
- MyReceiver對推送進行處理
public class MyReceiver extends BroadcastReceiver { private static final String TAG = "JIGUANG"; public static String regId; @Override public void onReceive(Context context, Intent intent) { try { Bundle bundle = intent.getExtras(); 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)); // 自定義消息不是通知,默認不會被SDK展示到通知欄上,極光推送僅負責透傳給SDK。其內容和展示形式完全由開發者自己定義。 // 自定義消息主要用於應用的內部業務邏輯和特殊展示需求 } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) { Log.d(TAG, "[MyReceiver] 接收到推送下來的通知"); String extra_json = bundle.getString(JPushInterface.EXTRA_EXTRA); if (!TextUtils.isEmpty(extra_json)) Log.d(TAG, "[MyReceiver] 接收到推送下來的通知附加字段" + extra_json); // 可以利用附加字段來區別Notication,指定不同的動作,extra_json是個json字符串 // 通知(Notification),指在手機的通知欄(狀態欄)上會顯示的一條通知信息 } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) { Log.d(TAG, "[MyReceiver] 用戶點擊打開了通知"); // 在這里根據 JPushInterface.EXTRA_EXTRA(附加字段) 的內容處理代碼, // 比如打開新的Activity, 打開一個網頁等.. } 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) { e.printStackTrace(); } } }
- 利用別名精准推送
// 一般登錄之后調用此方法設置別名 // sequence 用來標識一次操作的唯一性(退出登錄時根據此參數刪除別名) // alias 設置有效的別名 // 有效的別名組成:字母(區分大小寫)、數字、下划線、漢字、特殊字符@!#$&*+=.|。限制:alias 命名長度限制為 40 字節。 JPushInterface.setAlias(context, int sequence, String alias); // 退出登錄刪除別名 JPushInterface.deleteAlias(Context context,int sequence);
