Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

小米推送接入實記:從後台建項到代碼落地的完整流程

編輯:關於 Android 程式設計

公司項目需要接入推送,技術選型後我們用了小米推送。從註冊帳號到消息真正落到通知欄,中間踩了不少流程上的坑,這裡把完整步驟記錄下來。

一、後台配置

1、註冊並登入小米開放平台,選擇「消息推送」:

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

二、引入 SDK

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

三、在 Application 中初始化

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);

五、配置 AndroidManifest.xml

推送所需的權限比較多,這裡就不和原有權限混在一起列了,可以直接去 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 的類全路徑。

以上就是小米推送接入的完整過程,希望對大家的學習有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved