1. 消息監(jiān)聽接口
MobPushReceiver:消息監(jiān)聽接口(包含接收自定義消息、通知消息伟骨、通知欄點擊事件燃异、別名和標(biāo)簽變更操作等)
MobPush.addPushReceiver(MobPushReceiver
receiver): 設(shè)置消息監(jiān)聽
MobPush.removePushReceiver(MobPushReceiver
receiver): 移除消息監(jiān)聽
2. 推送開關(guān)控制接口
MobPush.stopPush():停止推送(停止后將不會收到推送消息,僅可通過restartPush重新打開)
MobPush.restartPush():重新打開推送服務(wù)
MobPush.isPushStopped():判斷推送服務(wù)是否已經(jīng)停止
3. 推送選項接口
MobPush.setSilenceTime(int
startHour, int startMinute, int endHour, int endMinute): 設(shè)置通知靜音時段(開始時間小時和分鐘逛腿、結(jié)束時間小時和分鐘)
MobPush.setCustomNotification(MobPushCustomNotification
customNotification): 設(shè)置自定義通知樣式
4. 業(yè)務(wù)接口
MobPush.getRegistrationId(MobPushCallback<String>
callback):獲取注冊id(可與用戶id綁定,實現(xiàn)向指定用戶推送消息)
別名操作:(同時只能設(shè)置一個別名碘举,可用來標(biāo)識一個用戶)
MobPush.setAlias(String
alias):設(shè)置別名
MobPush.getAlias():獲取當(dāng)前設(shè)置的別名
MobPush.deleteAlias():刪除別名
標(biāo)簽操作:(同時可設(shè)置多個標(biāo)簽雕凹,可用于多用戶訂閱標(biāo)簽的方式政冻,批量推送消息)
MobPush.addTags(String[]
tags):添加標(biāo)簽
MobPush.getTags():獲取所有已添加的標(biāo)簽
MobPush.deleteTags(String[]
tags):刪除標(biāo)簽
MobPush.cleanTags():清除所有已添加的標(biāo)簽
MobPushCustomeMessage:自定義消息實體類
MobPushNotifyMessage:通知消息實體類
5. 本地通知
MobPush.addLocalNotification(MobPushLocalNotification
notification):添加本地通知
MobPush.removeLocalNotification(int
notificationId):移除本地通知
MobPush.clearLocalNotifications():清空本地通知
MobPushLocalNotification:本地通知消息實體類明场,繼承MobPushNotifyMessage
6. API錯誤碼
API返回的錯誤碼說明如下:(詳見MobPushErrorCode.java說明)
-1? 網(wǎng)絡(luò)請求失敗
-2? 請求錯誤
前言:此功能僅僅是針對push的一些使用場景而進(jìn)行自定義設(shè)定。比如逼泣,通知被點擊的時候:
方式一舟舒、通過界面uri進(jìn)行l(wèi)ink跳轉(zhuǎn)
首先現(xiàn)在Manifest文件中進(jìn)行目標(biāo)Activity的uri設(shè)置,如下:
activity
? ? android:name=".LinkActivity">
? ? <intent-filter>
? ? ? ? <action android:name="android.intent.action.VIEW" />
? ? ? ? <category android:name="android.intent.category.DEFAULT" />
? ? ? ? <data
? ? ? ? ? ? android:host="com.mob.mobpush.link"
? ? ? ? ? ? android:scheme="mlink" />
? ? </intent-filter>
</activity>
在Mob后臺進(jìn)行推送時氏仗,通過scheme://host的格式夺鲜,例如mlink://com.mob.mobpush.link,如下位置填入:
配置好之后慷蠕,推送就App就可以接收到推送直接打開指定的Activity界面了食呻。
方式二、當(dāng)app顯示在前臺的時候浪感,會觸發(fā)MobPushReceiver的onNotifyMessageOpenedReceive方法饼问,MobPushNotifyMessage參數(shù)則是回調(diào)的通知詳情,可以根據(jù)回調(diào)參數(shù)進(jìn)行處理(不建議使用峻堰,當(dāng)進(jìn)程被殺掉的情況下,啟動應(yīng)用后可能無法執(zhí)行到回調(diào)方法旦万,因為此時可能還執(zhí)行到未添加監(jiān)聽的代碼)镶蹋;
方式三、不管app進(jìn)程是否被殺掉淆两,當(dāng)點擊通知后拉起應(yīng)用的啟動頁面拂酣,會觸發(fā)啟動Activity的OnCreate或OnNewIntent方法,通過getIntent方法拿到回傳的Intent剑勾,遍歷getExtras赵颅,可以拿到通知詳情(建議使用);
根據(jù)方式二饺谬,MobPush以兩個場景為例子:
場景一商蕴、通過擴(kuò)展參數(shù)實現(xiàn)頁面的自定義跳轉(zhuǎn):
//自定義擴(kuò)展字段的key,下發(fā)通知的時候绪商,在擴(kuò)展字段使用這個key
private final static String MOB_PUSH_DEMO_INTENT = "intent";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dealPushResponse(getIntent());
}
protected void onNewIntent(Intent intent) {
dealPushResponse(intent);
//需要調(diào)用setIntent方法格郁,不然后面獲取到的getIntent都是上一次傳的數(shù)據(jù)
setIntent(intent);
}
//OnCreate和OnNewIntent方法都要同時處理這個邏輯
private void dealPushResponse(Intent intent) {
? ?Bundle bundle = null;
? ?if (intent != null) {
? ? ? bundle = intent.getExtras();
? ? ? if (bundle != null) {
? ? ? ? ?Set<String> keySet = bundle.keySet();
? ? ? ? ?for (String key : keySet) {
? ? ? ? ? ? if (key.equals("msg")) {
? ? ? ? ? ? ? ?MobPushNotifyMessage notifyMessage = (MobPushNotifyMessage) bundle.get(key);
? ? ? ? ? ? ? ?HashMap<String, String> params = notifyMessage.getExtrasMap();
? ? ? ? ? ? ? ?if(params != null && params.containsKey(MOB_PUSH_DEMO_INTENT)){
? ? ? ? ? ? ? ? ? //此處跳轉(zhuǎn)到指定頁面
? ? ? ? ? ? ? ? ? openPage(params);
? ? ? ? ? ? ? ?}
? ? ? ? ? ? }
? ? ? ? ?}
? ? ? }
? ?}
}
private void openPage(HashMap<String, String> params){
Intent intent = new Intent(this, JumpActivity.class);
intent.putExtra("key1", "value1");
intent.putExtra("key2", "value2");
intent.putExtra("key3", "value3");
//如上Intent例书,在intent.toURI();之后得到的String,如下所示决采,可利用這個方法識別Intent傳的參數(shù),
//下發(fā)的參數(shù)可以按照下面的格式傳拇厢,客戶端接收后再轉(zhuǎn)成Intent,若添加action等其他參數(shù),可自行打印看Srting結(jié)構(gòu)體访敌;
//#Intent;component=com.mob.demo.mobpush/.JumpActivity;S.key1=value1;S.key2=value2;S.key3=value3;end
String uri;
if(!TextUtils.isEmpty(params.get(MOB_PUSH_DEMO_INTENT))) {
uri = params.get(MOB_PUSH_DEMO_INTENT);
try {
startActivity(Intent.parseUri(uri, 0));
} catch (Throwable t){
t.printStackTrace();
}
}
}
場景二衣盾、通過擴(kuò)展參數(shù)實現(xiàn)web界面的跳轉(zhuǎn):
代碼同場景一一樣势决,跳轉(zhuǎn)頁面的方法改成跳轉(zhuǎn)webview頁面就可以,通過參數(shù)識別徽龟,拿到需要跳轉(zhuǎn)的Url鏈接
private final static String MOB_PUSH_DEMO_URL = "url";
//OnCreate和OnNewIntent方法都要同時處理這個邏輯
private void dealPushResponse(Intent intent) {
? ?Bundle bundle = null;
? ?if (intent != null) {
? ? ? bundle = intent.getExtras();
? ? ? if (bundle != null) {
? ? ? ? ?Set<String> keySet = bundle.keySet();
? ? ? ? ?for (String key : keySet) {
? ? ? ? ? ? if (key.equals("msg")) {
? ? ? ? ? ? ? ?MobPushNotifyMessage notifyMessage = (MobPushNotifyMessage) bundle.get(key);
? ? ? ? ? ? ? ?HashMap<String, String> params = notifyMessage.getExtrasMap();
? ? ? ? ? ? ? ?if(params != null && params.containsKey(MOB_PUSH_DEMO_URL)){
? ? ? ? ? ? ? ? ? //此處跳轉(zhuǎn)到webview頁面
? ? ? ? ? ? ? ? ? openUrl(params);
? ? ? ? ? ? ? ?}
? ? ? ? ? ? }
? ? ? ? ?}
? ? ? }
? ?}
}
private void openUrl(HashMap<String, String> params){
? ?String url;
? ?if(!TextUtils.isEmpty(params.get(MOB_PUSH_DEMO_URL))) {
? ? ? url = params.get(MOB_PUSH_DEMO_URL);
? ?} else {
? ? ? url = "http://m.mob.com";
? ?}
? ?if(!url.startsWith("http://") && !url.startsWith("https://")){
? ? ? url = "http://" + url;
? ?}
? ?System.out.println("url:" + url);
? ?//以下代碼為開發(fā)者自定義跳轉(zhuǎn)webview頁面据悔,粘貼使用會找不到相關(guān)類
? ?WebViewPage webViewPage = new WebViewPage();
? ?webViewPage.setJumpUrl(url);
? ?webViewPage.show(this, null);
}
上面兩個場景的使用示例代碼极颓,可以參考官方demo