在小米推送运营平台创建应用,地址点这里, 获取到 AppID , AppKey
把从小米下载的 jar 放到 libs 下
在 AndroidManifest.xml 中添加权限
配置推送服务需要的service和receiver
android:enabled="true"
android:process=":pushservice"
android:name="com.xiaomi.push.service.XMPushService"/>
android:name="com.xiaomi.push.service.XMJobService"
android:enabled="true"
android:exported="false"
android:permission="android.permission.BIND_JOB_SERVICE"
android:process=":pushservice" />
android:enabled="true"
android:exported="true"
android:name="com.xiaomi.mipush.sdk.PushMessageHandler" />
android:name="com.xiaomi.mipush.sdk.MessageHandleService" />
android:exported="true"
android:name="com.xiaomi.push.service.receivers.NetworkStatusReceiver" >
android:exported="false"
android:process=":pushservice"
android:name="com.xiaomi.push.service.receivers.PingReceiver" >
自定义一个BroadcastReceiver类
public class MiMessageReceiver extends PushMessageReceiver {
private static final String TAG = "MiMessageReceiver";
private String mRegId;
private String mTopic;
private String mAlias;
private String mAccount;
private String mStartTime;
private String mEndTime;
@Override
public void onNotificationMessageClicked(Context context, MiPushMessage message) {
Log.v(MyApplication.TAG,
"onNotificationMessageClicked is called. " + message.toString());
if (!TextUtils.isEmpty(message.getTopic())) {
mTopic = message.getTopic();
Log.e(TAG, mTopic);
} else if (!TextUtils.isEmpty(message.getAlias())) {
mAlias = message.getAlias();
Log.e(TAG, mAlias);
}
}
@Override
public void onNotificationMessageArrived(Context context, MiPushMessage message) {
Log.v(MyApplication.TAG,
"onNotificationMessageArrived is called. " + message.toString());
String log = "Arrived a notification message. Content is " + message.getContent();
if (!TextUtils.isEmpty(message.getTopic())) {
mTopic = message.getTopic();
} else if (!TextUtils.isEmpty(message.getAlias())) {
mAlias = message.getAlias();
}
}
@Override
public void onCommandResult(Context context, MiPushCommandMessage message) {
Log.v(TAG,
"onCommandResult is called. " + message.toString());
String command = message.getCommand();
List arguments = message.getCommandArguments();
String cmdArg1 = ((arguments != null && arguments.size() > 0) ? arguments.get(0) : null);
String cmdArg2 = ((arguments != null && arguments.size() > 1) ? arguments.get(1) : null);
String log;
if (MiPushClient.COMMAND_REGISTER.equals(command)) {
if (message.getResultCode() == ErrorCode.SUCCESS) {
mRegId = cmdArg1;
Log.e(TAG, "Register push success.");
} else {
Log.e(TAG, "Register push fail.");
}
} else {
log = message.getReason();
}
}
@Override
public void onReceiveRegisterResult(Context context, MiPushCommandMessage message) {
Log.v(TAG,
"onReceiveRegisterResult is called. " + message.toString());
String command = message.getCommand();
List arguments = message.getCommandArguments();
String cmdArg1 = ((arguments != null && arguments.size() > 0) ? arguments.get(0) : null);
if (MiPushClient.COMMAND_REGISTER.equals(command)) {
if (message.getResultCode() == ErrorCode.SUCCESS) {
mRegId = cmdArg1;
Log.e(TAG, "Register push success.");
} else {
Log.e(TAG, "Register push fail.");
}
}
}
}
在 AndroidManifest.xml 中注册该广播
android:name=".MiMessageReceiver"
android:exported="true">
在 Application 中初始化推送服务
private void initMiPush() {
//初始化push推送服务
if (shouldInit()) {
MiPushClient.registerPush(this, MI_APP_ID, MI_APP_KEY);
}
//打开Log
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);
}
private boolean shouldInit() {
ActivityManager am = ((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE));
List processInfos = am.getRunningAppProcesses();
String mainProcessName = getPackageName();
int myPid = android.os.Process.myPid();
for (ActivityManager.RunningAppProcessInfo info : processInfos) {
if (info.pid == myPid && mainProcessName.equals(info.processName)) {
return true;
}
}
return false;
}