編輯:關於 Android 程式設計
公司項目需要接入推送,技術選型後我們用了小米推送。從註冊帳號到消息真正落到通知欄,中間踩了不少流程上的坑,這裡把完整步驟記錄下來。
1、註冊並登入小米開放平台,選擇「消息推送」:

2、進入後創建應用,按引導填完信息即可:

後台配置完成後開始寫代碼。第一次接入時我直接下載官方 Demo,把其中有用的部分複製進自己的項目。第一步是把 jar 包放進工程:

APP_ID 與 APP_KEY 都在小米開放平台創建項目時獲得:
private static final String APP_ID = "2882303761517483058"; // use your appid the key. private static final String APP_KEY = "5951748376058"; // 此 TAG 在 adb logcat 中檢索自己所需要的信息, // 只需在命令行終端輸入 adb logcat | grep // com.xiaomi.mipushdemo public static final String TAG = "com.dodonew.epapp";
接著在 Application 的 onCreate() 中加入註冊邏輯:
if (shouldInit()) {
MiPushClient.registerPush(this, APP_ID, APP_KEY);
}
LoggerInterface newLogger = new LoggerInterface() {
@Override
public void setTag(String tag) {
// ignore
}
@Override
public void log(String content, Throwable t) {
Log.d(TAG, content, t);
}
@Override
public void log(String content) {
Log.d(TAG, content);
}
};
Logger.setLogger(this, newLogger);
if (sHandler == null) {
sHandler = new DemoHandler(getApplicationContext());
}
其中 shouldInit() 與 Handler 的實現如下:
private boolean shouldInit() {
ActivityManager am = ((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE));
List<RunningAppProcessInfo> processInfos = am.getRunningAppProcesses();
String mainProcessName = getPackageName();
int myPid = Process.myPid();
for (RunningAppProcessInfo info : processInfos) {
if (info.pid == myPid && mainProcessName.equals(info.processName)) {
return true;
}
}
return false;
}
public static DemoHandler getHandler() {
return sHandler;
}
public static class DemoHandler extends Handler {
private Context context;
public DemoHandler(Context context) {
this.context = context;
}
@Override
public void handleMessage(Message msg) {
String s = (String) msg.obj;
if (sMainActivity != null) {
sMainActivity.refreshLogInfo();
}
if (!TextUtils.isEmpty(s)) {
// Toast.makeText(context, s, Toast.LENGTH_LONG).show();
}
}
}
老實說 DemoHandler 的作用有限,主要就是彈個 Toast 提示,個人不太喜歡這種打擾,所以把 Toast 註釋掉了。與之配套的 MainActivity 刷新方法:
public void refreshLogInfo() {
String AllLog = "";
for (String log : logList) {
AllLog = AllLog + log + "\n\n";
}
System.out.println("mainActivity中消息推送::::::::" + AllLog);
}
Demo 裡有一個繼承 PushMessageReceiver 的廣播類,內容基本一致,直接複製即可。其中最重要的方法是:
onNotificationMessageClicked(Context context, MiPushMessage message)
它的作用是:當消息推送到手機後,用戶在通知欄點擊該條消息時,你可以在這個方法裡通過 message 拿到消息內容。
如果你希望點擊通知後跳轉到 App 內指定頁面,也在這個方法中加一段代碼就行:
Intent intent = new Intent(context, 指定的Activity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent);
推送所需的權限比較多,這裡就不和原有權限混在一起列了,可以直接去 Demo 中對照。權限之後加入:
<permission
android:name="com.dodonew.epapp.permission.MIPUSH_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.dodonew.epapp.permission.MIPUSH_RECEIVE" />
<uses-permission android:name="android.permission.VIBRATE" />
然後在 <Application/> 節點內添加服務與接收器:
<service
android:name="com.xiaomi.push.service.XMJobService"
android:enabled="true"
android:exported="false"
android:permission="android.permission.BIND_JOB_SERVICE"
android:process=":pushservice" />
<service
android:name="com.xiaomi.push.service.XMPushService"
android:enabled="true"
android:process=":pushservice" />
<service
android:name="com.xiaomi.mipush.sdk.PushMessageHandler"
android:enabled="true"
android:exported="true" />
<service
android:name="com.xiaomi.mipush.sdk.MessageHandleService"
android:enabled="true" />
<receiver
android:name="com.dodonew.epapp.control.receiver.XiaoMiMessageReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.xiaomi.mipush.RECEIVE_MESSAGE" />
</intent-filter>
<intent-filter>
<action android:name="com.xiaomi.mipush.MESSAGE_ARRIVED" />
</intent-filter>
<intent-filter>
<action android:name="com.xiaomi.mipush.ERROR" />
</intent-filter>
</receiver>
<receiver
android:name="com.xiaomi.push.service.receivers.NetworkStatusReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<receiver
android:name="com.xiaomi.push.service.receivers.PingReceiver"
android:exported="false"
android:process=":pushservice">
<intent-filter>
<action android:name="com.xiaomi.push.PING_TIMER" />
</intent-filter>
</receiver>
這樣接入就完成了。注意 receiver 的 android:name 要換成你自己項目中那個繼承 PushMessageReceiver 的類全路徑。
以上就是小米推送接入的完整過程,希望對大家的學習有所幫助。
ListView 使用與性能優化:從基礎寫法到分批加載
一、ListView 的標準使用步驟 1. 定制實體類,把要填進列表的字段定義好,配齊 set / get 方法; 2. 自定義 item 布局,按數據需要排版; 3.
深入 dex 與 odex:文件結構、驗證與優化流程剖析
一、dex 文件結構 從邏輯上可把 dex 檔切成三個區塊:頭部(header)、索引區與數據區。索引區中以 ids 為後綴的字段,是 identifiers 的縮寫。
AppWidget 原始碼分析:API 類與跨進程更新
API 類總覽 AppWidget 是 Android 桌面小工具的重要機制。開發者若要實作一個可被桌面使用的元件,通常會接觸到 AppWidgetProvider、Ap
TextView 實現圖文混合編排的方法
在部分頁面中,我們希望文字與圖片能自然混排,而不是額外使用多個 ImageView 拼接。Android 的 TextView 可透過 HTML 風格標籤與 ImageG