1. 程式人生 > >百度雲推送所需要注意的點

百度雲推送所需要注意的點

最近專案整合百度雲推送,整合很簡單但是用起來和其他推送有點不一樣。

控制檯推送和後臺推送有點不一樣。

******需要我們自己自定義行為:

先看看安卓推送通知格式;

Android通知格式


{"title":"hello","description":"hello world"//必選  "notification_builder_id":0,//可選  "notification_basic_style":7,//可選  "open_type":0,//可選  "url":"http://developer.baidu.com",//可選  "pkg_content":"",//可選  "custom_content"
:{"key":"value"},}

屬性說明

  • title:通知標題,可以為空;如果為空則設為appid對應的應用名。
  • description:通知文字內容,不能為空。
  • notification_builder_id:android客戶端自定義通知樣式,如果沒有設定預設為0。
  • notification_basic_style:只有notification_builder_id為0時有效,可以設定通知的基本樣式包括(響鈴:0x04;振動:0x02;可清除:0x01;),這是一個flag整形,每一位代表一種樣式,如果想選擇任意兩種或三種通知樣式,notification_basic_style的值即為對應樣式數值相加後的值。
  • open_type:點選通知後的行為(1:開啟Url; 2:自定義行為;)。open_type =0,只回調onNotificationClicked方法,不做其他操作;open_type = 1,url != null:開啟網頁;open_type = 2,pkg_content = null:直接開啟應用;open_type = 2,pkg_content != null:自定義動作開啟應用。
  • url:需要開啟的Url地址,open_type為1時才有效。
  • pkg_content:open_type為2時才有效,Android端SDK會把pkg_content字串轉換成Android Intent,通過該Intent開啟對應app元件,所以pkg_content字串格式必須遵循Intent uri格式,最簡單的方法可以通過Intent方法toURI()獲取。
  • custom_content:自定義內容,鍵值對,Json物件形式(可選);在android客戶端,這些鍵值對將以Intent中的extra進行傳遞。

第一步,在AndroidManifest.xml配置當用戶點選要跳轉的Activity,如下所示,注意intent-filter裡面的內容,後面用得到。

<activity
android:name=".HomeActivity"
android:launchMode="singleTask"
android:screenOrientation="portrait">
    <intent-filter>
        <action android:name="com.push.HomeActivity" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

第二步,讓伺服器的人修改推送通知時候的兩個引數的值

"open_type":2, "pkg_content":“#Intent;action=com.push.HomeActivity;launchFlags=0x4000000;end” ",

open_type 值為2,pkg_content : 的值怎麼獲取呢?如下所示:

Intent intent = new Intent();
//在AndroidManifest.xml的activity裡面intent-filter配置action
intent.setAction("
com.push.HomeActivity
");intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); String pkg_content = intent.toUri(Intent.FLAG_ACTIVITY_CLEAR_TOP); 這樣伺服器傳送的通知當用戶點選後就可以跳轉到pkg_content配置的Activity啦。
@Override
public void onNotificationClicked(Context context, String title, String description, String customContentString) {
    LogUtil.i(TAG, "dianjihailaima");
    LogUtil.i(TAG, "dianjihailaima title = " + title);
    LogUtil.i(TAG, "dianjihailaima description = " + description);
    LogUtil.i(TAG, "dianjihailaima customContentString = " + customContentString);

    SharedPreferUtils.write("tui", "cub", "1");
}
其實customContentString  = cunstom_content . 點選之後會走你指定跳轉的Activity對應的onResume().這裡可以進行狀態判斷。

custom_content需要後臺定義,放在通知格式裡面,不然為空
,可以通過這個玩意來進行相關頁面的操作

整合推送的時候,把app的key以及 secret給後臺,註冊推送的時候再根據後臺傳channelId以及userId。看你們的需求再定。