Android接入極光訊息推送
極光訊息推送:
極光推送(JPush)是一個端到端的推送服務,使得伺服器端訊息能夠及時地推送到終端使用者手機上,讓開發者積極地保持與使用者的連線
主要功能
- 保持與伺服器的長連線,以便訊息能夠即時推送到達客戶端
- 接收通知與自定義訊息,並向開發者App 傳遞相關資訊
主要特點
- 客戶端維持連線佔用資源少、耗電低
- SDK豐富的介面,可定製通知欄提示樣式
- 伺服器大容量、穩定
接入步驟
1.開啟極光官網進行註冊
2.登入極光官網進入使用者的後臺管理進行建立應用(按照步驟一步一步來)
3.應用建立成功後,可以拿到AppKey(後期用於在Android清單檔案中配置)
4.下載極光推送
5.下載成功後將Jpush-android-3.x.x-release.zip壓縮包的內容解壓如下圖所示目錄結構
example 是官方的demo案例
libs是JPush的Jar包和so檔案
AndroidManifest.xml JPush清單檔案配置
1.將libs目錄下的所有jar包複製到Android Studio中的Project目錄結構下的libs目錄
2.將libs目錄下的所有so檔案複製到Android Studio中的jniLibs目錄下。如下圖所示
6.配置AndroidManifest.xml清單檔案
根據 SDK 壓縮包裡的 AndroidManifest.xml樣例檔案,來配置應用程式專案的AndroidManifest.xml 。
主要步驟為:
- 複製備註為 “Required” 的部分
- 將標註為“您應用的包名”的部分,替換為當前應用程式的包名
- 將標註為“您應用的Appkey”的部分,替換為在JPush官網上註冊該應用的的AppKey,例如:9fed5bcb7b9b87413678c407
清單檔案示例
在你應用的Application中的onCrate()方法中進行初始化例如:
public class ExampleApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
JPushInterface.setDebugMode(true );
JPushInterface.init(this);
}
}
測試確認
- 確認所需的許可權都已經新增。如果必須的許可權未新增,日誌會提示錯誤。
- 確認 AppKey(在Portal上生成的)已經正確的寫入 Androidmanifest.xml 。
- 確認在程式啟動時候呼叫了init(context) 介面
- 確認測試手機(或者模擬器)已成功連入網路 + 客戶端呼叫 init 後不久,如果一切正常,應有登入成功的日誌資訊
- 啟動應用程式,在 Portal 上嚮應用程式傳送自定義訊息或者通知欄提示。詳情請參考管理Portal。
在幾秒內,客戶端應可收到下發的通知或者正定義訊息,如果 SDK 工作正常,則日誌資訊會如下:
[JPushInterface] action:init…….
[PushService] Login succeed!
如圖所示,客戶端啟動分為 4 步:
- 檢查 metadata 的 appKey 和 channel ,如果不存在,則啟動失敗
- 初始化 JPush SDK,檢查 JNI 等庫檔案的有效性,如果庫檔案無效,則啟動失敗
- 檢查 Androidmanifest.xml,如果有 Required 的許可權不存在,則啟動失敗
- 連線伺服器登入,如果存在網路問題,則登陸失敗,或者前面三步有問題,不會啟動JPush SDK
測試JPush訊息推送
進入你的極光推送後臺管理選擇剛才建立的應用進行推送。推送的型別如下圖所示
可以自行到後臺管理檢視配置
這時候如果前面的配置都沒有問題的話,在後臺推送訊息後,前臺是可以正確收到的。
別名與標籤使用
為什麼需要別名與標籤?
推送訊息時,要指定推送的物件:全部,某一個人,或者某一群人。
全部很好辦,針對某應用“群發”就好了。Portal與API都支援向指定的 appKey 群發訊息。
要指定向某一個特定的人,或者某一群特定的人,則相對複雜。因為對於 JPush 來說,某一個人就是一個註冊ID,這個註冊ID與開發者App沒有任何關係,或者說對開發者App是沒有意義的。
如果要對開發者App有意義的某個特定的使用者推送訊息,則需要:把 JPush 註冊使用者與開發者App 使用者繫結起來。
這個繫結有兩個基本思路:
- 把繫結關係儲存到 JPush 伺服器端
- 把繫結關係儲存到開發者應用伺服器中
前者,就是這裡要說到的:別名與標籤的功能。這個機制簡單易用,適用於大多數開發者。
後者,則是 JPush 提供的另外一套 RegistrationID 機制。這套機制開發者需要有應用伺服器來維護繫結關係,不適用於普通開發者。Android SDK 1.6.0 版本開始支援。
使用方式
別名與標籤的機制,其工作方式是:
客戶端開發者App呼叫 setAlias或者setTags API 來設定關係
JPush SDK 把該關係設定儲存到 JPush Server 上
在伺服器端推送訊息時,指定向之前設定過的別名或者標籤推送
使用標籤
用於給某一群人推送訊息。
標籤類似於部落格裡為文章打上 tag ,即為某資源分類。
動態標籤
JPush 提供的設定標籤的 API 是在客戶端的。開發者如何做到在自己的伺服器端動態去設定分組呢? 比如一個企業OA系統,經常需要去變更部門人員分組。以下是大概的思路:
設計一種自定義訊息格式(業務協議),App解析後可以呼叫 JPush SDK setAliasAndTags API 來重新設定標籤(分組)
例:
{"action":"resetTags", "newTags":["dep_level_1":"A公司", "dep_level_2":"技術部", "dep_level_3":"Android開發組", "address":"深圳", "lang":"zh"]}
要動態設定分組時,推送這條自定義訊息給指定的使用者
使用別名的機制,推送到指定的使用者。
客戶端App 呼叫 JPush SDK API 來設定新的標籤
別名與標籤設定異常處理
由於網路連線不穩定的原因,有一定的概率 JPush SDK 設定別名與標籤會失敗。 App 開發者合理地處理設定失敗,則偶爾失敗對應用的正常使用 JPush 影響是有限的。
自定義通知欄樣式教程
通知 vs 自定義訊息
極光推送包含有通知與自定義訊息兩種型別的推送。本文描述他們的區別,以及建議的應用場景。
兩者的區別 - 功能角度
通知
通知(Notification),指在手機的通知欄(狀態列)上會顯示的一條通知資訊。這是 Android / iOS 的基本功能。通知主要用於提示使用者。一條通知,簡單的填寫純文字的通知內容即可。應用加上通知功能,有利於提高應用的活躍度。
自定義訊息
自定義訊息不是通知,預設不會被SDK展示到通知欄上,極光推送僅負責透傳給SDK。其內容和展示形式完全由開發者自己定義。自定義訊息主要用於應用的內部業務邏輯和特殊展示需求。
兩者的區別 - 開發者使用角度
通知
簡單場景下的通知,使用者可以不寫一行程式碼,而完全由SDK來負責預設的效果展示,以及預設使用者點選時開啟應用的主介面。JPush Android SDK 提供了 API讓開發者來定製通知欄的效果,請參考:自定義通知欄樣式教程;也提供了 接收推送訊息Receiver 讓你來定製在收到通知時與使用者點選通知時的不同行為。
自定義訊息
SDK 不會把自定義訊息展示到通知欄。所以除錯時,需要到日誌裡才可以看到伺服器端推送的自定義訊息。自定義訊息一定要由開發者寫 接收推送訊息Receiver 來處理收到的訊息。
注意:
當自定義訊息內容msg_content為空時,SDK不會對訊息進行廣播,使得app無法接收到推送的訊息,因此建議在使用自定義訊息推送時新增內容。
使用通知
請參考以下示例程式碼。
public class MyReceiver extends BroadcastReceiver {
private static final String TAG = "MyReceiver";
private NotificationManager nm;
@Override
public void onReceive(Context context, Intent intent) {
if (null == nm) {
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
}
Bundle bundle = intent.getExtras();
Logger.d(TAG, "onReceive - " + intent.getAction() + ", extras: " + AndroidUtil.printBundle(bundle));
if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
Logger.d(TAG, "JPush使用者註冊成功");
} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
Logger.d(TAG, "接受到推送下來的自定義訊息");
} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
Logger.d(TAG, "接受到推送下來的通知");
receivingNotification(context,bundle);
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
Logger.d(TAG, "使用者點選打開了通知");
openNotification(context,bundle);
} else {
Logger.d(TAG, "Unhandled intent - " + intent.getAction());
}
}
private void receivingNotification(Context context, Bundle bundle){
String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
Logger.d(TAG, " title : " + title);
String message = bundle.getString(JPushInterface.EXTRA_ALERT);
Logger.d(TAG, "message : " + message);
String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
Logger.d(TAG, "extras : " + extras);
}
private void openNotification(Context context, Bundle bundle){
String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
String myValue = "";
try {
JSONObject extrasJson = new JSONObject(extras);
myValue = extrasJson.optString("myKey");
} catch (Exception e) {
Logger.w(TAG, "Unexpected: extras is not a valid json", e);
return;
}
if (TYPE_THIS.equals(myValue)) {
Intent mIntent = new Intent(context, ThisActivity.class);
mIntent.putExtras(bundle);
mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mIntent);
} else if (TYPE_ANOTHER.equals(myValue)){
Intent mIntent = new Intent(context, AnotherActivity.class);
mIntent.putExtras(bundle);
mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mIntent);
}
}
}
使用自定義訊息
使用自定義訊息,在客戶端App裡一定要寫程式碼,去接受 JPush SDK 的廣播,從而取得推送下來的訊息內容。具體請參考文件:接收推送訊息Receiver。
public class TalkReceiver extends BroadcastReceiver {
private static final String TAG = "TalkReceiver";
private NotificationManager nm;
@Override
public void onReceive(Context context, Intent intent) {
if (null == nm) {
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
}
Bundle bundle = intent.getExtras();
Logger.d(TAG, "onReceive - " + intent.getAction() + ", extras: " + AndroidUtil.printBundle(bundle));
if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
Logger.d(TAG, "JPush使用者註冊成功");
} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
Logger.d(TAG, "接受到推送下來的自定義訊息");
// Push Talk messages are push down by custom message format
processCustomMessage(context, bundle);
} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
Logger.d(TAG, "接受到推送下來的通知");
receivingNotification(context,bundle);
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
Logger.d(TAG, "使用者點選打開了通知");
openNotification(context,bundle);
} else {
Logger.d(TAG, "Unhandled intent - " + intent.getAction());
}
}
private void processCustomMessage(Context context, Bundle bundle) {
String title = bundle.getString(JPushInterface.EXTRA_TITLE);
String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
if (StringUtils.isEmpty(title)) {
Logger.w(TAG, "Unexpected: empty title (friend). Give up");
return;
}
boolean needIncreaseUnread = true;
if (title.equalsIgnoreCase(Config.myName)) {
Logger.d(TAG, "Message from myself. Give up");
needIncreaseUnread = false;
if (!Config.IS_TEST_MODE) {
return;
}
}
String channel = null;
String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
try {
JSONObject extrasJson = new JSONObject(extras);
channel = extrasJson.optString(Constants.KEY_CHANNEL);
} catch (Exception e) {
Logger.w(TAG, "Unexpected: extras is not a valid json", e);
}
// Send message to UI (Webview) only when UI is up
if (!Config.isBackground) {
Intent msgIntent = new Intent(MainActivity.MESSAGE_RECEIVED_ACTION);
msgIntent.putExtra(Constants.KEY_MESSAGE, message);
msgIntent.putExtra(Constants.KEY_TITLE, title);
if (null != channel) {
msgIntent.putExtra(Constants.KEY_CHANNEL, channel);
}
JSONObject all = new JSONObject();
try {
all.put(Constants.KEY_TITLE, title);
all.put(Constants.KEY_MESSAGE, message);
all.put(Constants.KEY_EXTRAS, new JSONObject(extras));
} catch (JSONException e) {
}
msgIntent.putExtra("all", all.toString());
context.sendBroadcast(msgIntent);
}
String chatting = title;
if (!StringUtils.isEmpty(channel)) {
chatting = channel;
}
String currentChatting = MyPreferenceManager.getString(Constants.PREF_CURRENT_CHATTING, null);
if (chatting.equalsIgnoreCase(currentChatting)) {
Logger.d(TAG, "Is now chatting with - " + chatting + ". Dont show notificaiton.");
needIncreaseUnread = false;
if (!Config.IS_TEST_MODE) {
return;
}
}
if (needIncreaseUnread) {
unreadMessage(title, channel);
}
NotificationHelper.showMessageNotification(context, nm, title, message, channel);
}
// When received message, increase unread number for Recent Chat
private void unreadMessage(final String friend, final String channel) {
new Thread() {
public void run() {
String chattingFriend = null;
if (StringUtils.isEmpty(channel)) {
chattingFriend = friend;
}
Map<String, String> params = new HashMap<String, String>();
params.put("udid", Config.udid);
params.put("friend", chattingFriend);
params.put("channel_name", channel);
try {
HttpHelper.post(Constants.PATH_UNREAD, params);
} catch (Exception e) {
Logger.e(TAG, "Call pushtalk api to report unread error", e);
}
}
}.start();
}
}
相關API說明
API - setDebugMode設定除錯模式
注:該介面需在init介面之前呼叫,避免出現部分日誌沒列印的情況。多程序情況下建議在自定義的Application中onCreate中呼叫。
API - init
初始化推送服務。呼叫了本 API 後,JPush推送服務進行初始化。建議在自定義的Application中的onCreate中呼叫。
API - resumePush恢復推送服務
呼叫了此 API 後,極光推送完全恢復正常工作。
API - isPushStopped
用來檢查 Push Service 是否已經被停止
API - setAlias
呼叫此 API 來設定別名。
需要理解的是,這個介面是覆蓋邏輯,而不是增量邏輯。即新的呼叫會覆蓋之前的設定。
API - deleteAlias
呼叫此 API 來刪除別名。
API - getAlias
呼叫此 API 來查詢別名。
API - setTags
呼叫此 API 來設定標籤。
需要理解的是,這個介面是覆蓋邏輯,而不是增量邏輯。即新的呼叫會覆蓋之前的設定。
API - addTags
呼叫此 API 來新增標籤。
API - deleteTags
呼叫此 API 來刪除指定標籤。
API - cleanTags
呼叫此 API 來清除所有標籤。
API - getAllTags
呼叫此 API 來查詢所有標籤。
API - checkTagBindState
呼叫此 API 來查詢指定tag與當前使用者繫結的狀態。
API - setMobileNumber
呼叫此API設定手機號碼。該介面會控制呼叫頻率,頻率為 10s之內最多三次。
新的訊息回撥方式說明
Class - cn.jpush.android.service.JPushMessageReceiver
新的訊息回撥方式中相關回調類。
新的tag與alias操作回撥會在開發者定義的該類的子類中觸發。
手機號碼設定的回撥會在開發者定義的該類的子類中觸發。
該類為回撥父類,開發者需要繼承該類並在Manifest中配置您對應實現的類,介面操作的結果會在您配置的類中的如下方法中回撥。
Class - cn.jpush.android.api.JPushMessage
新的訊息回撥方式中相關回調的結果類,使用該類物件可獲取對應的操作結果。
當前僅僅新的tag與alias操作回撥會涉及到該類
JPushMessageReceiver用法示例
public class MyJPushMessageReceiver extends JPushMessageReceiver {
/**
* tag增刪查改的操作會在此方法中回撥結果。
*
* @param context
* @param jPushMessage
*/
@Override
public void onTagOperatorResult(Context context, JPushMessage jPushMessage) {
super.onTagOperatorResult(context, jPushMessage);
}
/**
* 查詢某個tag與當前使用者的繫結狀態的操作會在此方法中回撥結果。
*
* @param context
* @param jPushMessage
*/
@Override
public void onCheckTagOperatorResult(Context context, JPushMessage jPushMessage) {
super.onCheckTagOperatorResult(context, jPushMessage);
}
/**
* alias相關的操作會在此方法中回撥結果。
*
* @param context
* @param jPushMessage
*/
@Override
public void onAliasOperatorResult(Context context, JPushMessage jPushMessage) {
super.onAliasOperatorResult(context, jPushMessage);
}
/**
* 設定手機號碼會在此方法中回撥結果。
*
* @param context
* @param jPushMessage
*/
@Override
public void onMobileNumberOperatorResult(Context context, JPushMessage jPushMessage) {
super.onMobileNumberOperatorResult(context, jPushMessage);
}
}
接收推送訊息Receiver
JPush SDK 收到推送,通過廣播的方式,轉發給開發者App,這樣開發者就可以靈活地進行處理。這個動作不是必須的。使用者有需要才定義 Receiver 類來處理 SDK過來的廣播。如果不做這個動作,即不寫自定義 Receiver,也不在AndroidManifest.xml 裡配置這個 Receiver,則預設的行為是:
- 接收到推送的自定義訊息,則沒有被處理
- 可以正常收到通知,使用者點選開啟應用主介面
如果全部型別的廣播都接收,則需要在 AndroidManifest.xml 裡新增如下的配置資訊:
<receiver
android:name="Your Receiver"
android:enabled="true">
<intent-filter>
<action android:name="cn.jpush.android.intent.REGISTRATION" />
<action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" />
<action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" />
<action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" />
<action android:name="cn.jpush.android.intent.NOTIFICATION_CLICK_ACTION" />
<action android:name="cn.jpush.android.intent.CONNECTION" />
<category android:name="You package Name" />
</intent-filter>
</receiver>
每個 Receiver action 詳細解釋可參考官網文件,也可參考筆者的JPushDemo程式碼示例的註釋程式碼
/**
* 自定義訊息接收器
* <p>
* 如果不定義這個 Receiver,則:
* 1) 預設使用者會開啟主介面
* 2) 接收不到自定義訊息
* </p>
*
*/
public class MyReceiver extends BroadcastReceiver {
private static final String TAG = "MyReceiver";
@Override
public void onReceive(Context context, Intent intent) {
try {
Bundle bundle = intent.getExtras();
LogUtil.d(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));
if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
//SDK 向 JPush Server 註冊所得到的註冊 ID
LogUtil.d(TAG, "接收RegistrationId");
String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
//收到了自定義訊息 Push
LogUtil.d(TAG, "接收到推送下來的自定義訊息");
//1.訊息標題
String title = bundle.getString(JPushInterface.EXTRA_TITLE);
//2.訊息內容
String content = bundle.getString(JPushInterface.EXTRA_MESSAGE);
//3.訊息附加欄位
String extra = bundle.getString(JPushInterface.EXTRA_EXTRA);
//4.訊息唯一標識ID
String msgId = bundle.getString(JPushInterface.EXTRA_MSG_ID);
} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
//收到了通知 Push
LogUtil.d(TAG, "接收到推送下來的通知");
//1.訊息標題
String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
//2.訊息內容
String content = bundle.getString(JPushInterface.EXTRA_ALERT);
//3.訊息附加欄位
String extra = bundle.getString(JPushInterface.EXTRA_EXTRA);
//4.通知欄的Notification ID,可以用於清除Notification
String notificationId = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_ID);
//5.富媒體通知推送下載的HTML的檔案路徑,用於展現WebView。
String fileHtml = bundle.getString(JPushInterface.EXTRA_RICHPUSH_HTML_PATH);
//6.富媒體通知推送下載的圖片資源的檔名
String fileStr = bundle.getString(JPushInterface.EXTRA_RICHPUSH_HTML_RES);
String[] fileNames = fileStr.split(",");
//7.唯一標識通知訊息的 ID, 可用於上報統計等。
String msgId = bundle.getString(JPushInterface.EXTRA_MSG_ID);
//8.大文字通知樣式中大文字的內容。
String bigText = bundle.getString(JPushInterface.EXTRA_BIG_TEXT);
//9.大圖片通知樣式中大圖片的路徑/地址
String bigPicPath = bundle.getString(JPushInterface.EXTRA_BIG_PIC_PATH);
//10.收件箱通知樣式中收件箱的內容
String inboxJson = bundle.getString(JPushInterface.EXTRA_INBOX);
//11.通知的優先順序。預設為0,範圍為 -2~2 ,其他值將會被忽略而採用預設。
String priority = bundle.getString(JPushInterface.EXTRA_NOTI_PRIORITY);
//12.通知分類。
String category = bundle.getString(JPushInterface.EXTRA_NOTI_CATEGORY);
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
//(1)使用者點選了通知。 一般情況下,使用者不需要配置此 receiver action。
//(2)如果開發者在 AndroidManifest.xml 裡未配置此 receiver action,那麼,SDK 會預設開啟應用程式的主 Activity,相當於使用者點選桌面圖示的效果。
//(3)如果開發者在 AndroidManifest.xml 裡配置了此 receiver action,那麼,當用戶點選通知時,SDK 不會做動作。開發者應該在自己寫的 BroadcastReceiver 類裡處理,比如開啟某 Activity 。
LogUtil.d(TAG, "[使用者點選打開了通知");
//1.訊息標題
String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
//2.訊息內容
String content = bundle.getString(JPushInterface.EXTRA_ALERT);
//3.訊息附加欄位
String extra = bundle.getString(JPushInterface.EXTRA_EXTRA);
//4.知欄的Notification ID,可以用於清除Notification
int notificationId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
//5.唯一標識調整訊息的 ID, 可用於上報統計等。
String msgId = bundle.getString(JPushInterface.EXTRA_MSG_ID);
} else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
//(1)使用者點選了通知欄中自定義的按鈕。(SDK 3.0.0 以上版本支援)
//(2)使用普通通知的開發者不需要配置此 receiver action。只有開發者使用了 MultiActionsNotificationBuilder 構建攜帶按鈕的通知欄的通知時,可通過該 action 捕獲到使用者點選通知欄按鈕的操作,並自行處理。
LogUtil.d(TAG, "[MyReceiver] 使用者收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
/**
* <p>
* private void setAddActionsStyle() {
* MultiActionsNotificationBuilder builder = new MultiActionsNotificationBuilder(PushSetActivity.this);
* builder.addJPushAction(R.drawable.jpush_ic_richpush_actionbar_back, "first", "my_extra1");
* builder.addJPushAction(R.drawable.jpush_ic_richpush_actionbar_back, "second", "my_extra2");
* builder.addJPushAction(R.drawable.jpush_ic_richpush_actionbar_back, "third", "my_extra3");
* JPushInterface.setPushNotificationBuilder(10, builder);
* Toast.makeText(PushSetActivity.this, "AddActions Builder - 10", Toast.LENGTH_SHORT).show();
}
* </p>
*/
/**
* <p>
* Log.d(TAG, "[MyReceiver] 使用者點選了通知欄按鈕");
String nActionExtra = intent.getExtras().getString(JPushInterface.EXTRA_NOTIFICATION_ACTION_EXTRA);
//開發者根據不同 Action 攜帶的 extra 欄位來分配不同的動作。
if(nActionExtra==null){
Log.d(TAG,"ACTION_NOTIFICATION_CLICK_ACTION nActionExtra is null");
return;
}
if (nActionExtra.equals("my_extra1")) {
Log.d(TAG, "[MyReceiver] 使用者點選通知欄按鈕一");
} else if (nActionExtra.equals("my_extra2")) {
Log.d(TAG, "[MyReceiver] 使用者點選通知欄按鈕二");
} else if (nActionExtra.equals("my_extra3")) {
Log.d(TAG, "[MyReceiver] 使用者點選通知欄按鈕三");
} else {
Log.d(TAG, "[MyReceiver] 使用者點選通知欄按鈕未定義");
}
* </p>
*/
} else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
//JPush 服務的連線狀態發生變化。(注:不是指 Android 系統的網路連線狀態。)
LogUtil.w(TAG, "JPush服務連線狀態發生改變");
boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
} else {
LogUtil.d(TAG, "未處理的Intent");
}
} 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 (TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_EXTRA))) {
LogUtil.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();
sb.append("\nkey:" + key + ", value: [" +
myKey + " - " + json.optString(myKey) + "]");
}
} catch (JSONException e) {
LogUtil.e(TAG, "Get message extra JSON error!");
}
} else {
sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
}
}
return sb.toString();
}
}
總結
極光推送Android的接入並不是很難。按照官網一步一步走下來即可。具體詳細的介紹文件,可以進入極光官網,檢視Android 接入指南。很詳細,很清晰。如果讀者接入的過程中遇到問題,可以在官網檢視問題的描述,以及相關的解決方案。