推送MobPush-API說明

1. 消息監聽接口

MobPushReceiver: 消息監聽接口(包含接收自定義消息、通知消息、通知欄點擊事件、別名和標籤變更操作等)

MobPush.addPushReceiver(MobPushReceiver receiver): 設置消息監聽

MobPush.removePushReceiver(MobPushReceiver receiver): 移除消息監聽

2. 推送開關控制接口

MobPush.stopPush(): 停止推送(停止後將不會收到推送消息,僅可通過restartPush重新打開)

MobPush.restartPush(): 重新打開推送服務

MobPush.isPushStopped(): 判斷推送服務是否已經停止

3. 推送選項接口

MobPush.setSilenceTime(int startHour, int startMinute, int endHour, int endMinute): 設置通知靜音時段(開始時間小時和分鐘、結束時間小時和分鐘)

MobPush.setCustomNotification(MobPushCustomNotification customNotification): 設置自定義通知樣式

4. 業務接口

MobPush.getRegistrationId(MobPushCallback callback):獲取註冊id(可與用戶id綁定,實現向指定用戶推送消息)

別名操作:(同時只能設置一個別名,可用來標識一個用戶)

MobPush.setAlias(String alias):設置別名

MobPush.getAlias():獲取當前設置的別名

MobPush.deleteAlias():刪除別名

標籤操作:(同時可設置多個標籤,可用於多用戶訂閱標籤的方式,批量推送消息)

MobPush.addTags(String[] tags):添加標籤

MobPush.getTags():獲取所有已添加的標籤

MobPush.deleteTags(String[] tags):刪除標籤

MobPush.cleanTags():清除所有已添加的標籤

MobPushCustomeMessage: 自定義消息實體類

MobPushNotifyMessage: 通知消息實體類

5. 本地通知

MobPush.addLocalNotification(MobPushLocalNotification notification):添加本地通知

MobPush.removeLocalNotification(int notificationId):移除本地通知

MobPush.clearLocalNotifications():清空本地通知

MobPushLocalNotification:本地通知消息實體類,繼承MobPushNotifyMessage

6. API錯誤碼

API返回的錯誤碼說明如下:(詳見MobPushErrorCode.java說明)

-1 網絡請求失敗

-2 請求錯誤

功能自定義和擴展

前言:此功能僅僅是針對push的一些使用場景而進行自定義設定。比如,通知被點擊的時候:

方式一、通過界面uri進行link跳轉

首先現在Manifest文件中進行目標Activity的uri設置,如下:

activity

android:name=".LinkActivity">

android:host="com.mob.mobpush.link"

android:scheme="mlink" />

在Mob後臺進行推送時,通過scheme://host的格式,例如mlink://com.mob.mobpush.link,如下位置填入:

推送MobPush-API說明

配置好之後,推送就App就可以接收到推送直接打開指定的Activity界面了。

方式二、當app顯示在前臺的時候,會觸發MobPushReceiver的onNotifyMessageOpenedReceive方法,MobPushNotifyMessage參數則是回調的通知詳情,可以根據回調參數進行處理(不建議使用,當進程被殺掉的情況下,啟動應用後可能無法執行到回調方法,因為此時可能還執行到未添加監聽的代碼);

方式三、不管app進程是否被殺掉,當點擊通知後拉起應用的啟動頁面,會觸發啟動Activity的OnCreate或OnNewIntent方法,通過getIntent方法拿到回傳的Intent,遍歷getExtras,可以拿到通知詳情(建議使用);

根據方式二,MobPush以兩個場景為例子:

場景一、通過擴展參數實現頁面的自定義跳轉:

//自定義擴展字段的key,下發通知的時候,在擴展字段使用這個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);

//需要調用setIntent方法,不然後面獲取到的getIntent都是上一次傳的數據

setIntent(intent);

}

//OnCreate和OnNewIntent方法都要同時處理這個邏輯

private void dealPushResponse(Intent intent) {

Bundle bundle = null;

if (intent != null) {

bundle = intent.getExtras();

if (bundle != null) {

Set keySet = bundle.keySet();

for (String key : keySet) {

if (key.equals("msg")) {

MobPushNotifyMessage notifyMessage = (MobPushNotifyMessage) bundle.get(key);

HashMap params = notifyMessage.getExtrasMap();

if(params != null && params.containsKey(MOB_PUSH_DEMO_INTENT)){

//此處跳轉到指定頁面

openPage(params);

}

}

}

}

}

}

private void openPage(HashMap 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傳的參數,

//下發的參數可以按照下面的格式傳,客戶端接收後再轉成Intent,若添加action等其他參數,可自行打印看Srting結構體;

//#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();

}

}

}

場景二、通過擴展參數實現web界面的跳轉:

代碼同場景一一樣,跳轉頁面的方法改成跳轉webview頁面就可以,通過參數識別,拿到需要跳轉的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 keySet = bundle.keySet();

for (String key : keySet) {

if (key.equals("msg")) {

MobPushNotifyMessage notifyMessage = (MobPushNotifyMessage) bundle.get(key);

HashMap params = notifyMessage.getExtrasMap();

if(params != null && params.containsKey(MOB_PUSH_DEMO_URL)){

//此處跳轉到webview頁面

openUrl(params);

}

}

}

}

}

}

private void openUrl(HashMap 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);

//以下代碼為開發者自定義跳轉webview頁面,粘貼使用會找不到相關類

WebViewPage webViewPage = new WebViewPage();

webViewPage.setJumpUrl(url);

webViewPage.show(this, null);

}

上面兩個場景的使用示例代碼,可以參考官方demo

https://github.com/MobClub/MobPush-for-Android


推送MobPush-API說明


[ShareSDK] 輕鬆實現社會化功能 強大的社交分享

[SMSSDK] 快速集成短信驗證 聯結通訊錄社交圈

[MobLink] 打破App孤島 實現Web與App無縫鏈接

[MobPush] 快速集成推送服務 應對多樣化推送場景

[AnalySDK] 精準化行為分析 + 多維數據模型 + 匹配全網標籤 + 垂直行業分析顧問

BBSSDK | ShareREC | MobAPI | MobPay | ShopSDK | MobIM | App工廠

截止2018 年4 月,Mob 開發者服務平臺全球設備覆蓋超過84 億,SDK下載量超過3,300,000+次,服務超過380,000+款移動應用


分享到:


相關文章: