前置步骤:
在[小米开发者站](http://developer.xiaomi.com/)开通小米开发者账号。
创建应用,开发者账号审核通过后你就可以在开发者站创建你的应用。
开启应用的推送服务。
下载SDK、文档和DEMO。
完成1、2、3步骤之后,在小米平台的界面你会看到跟下图类似的界面:
image.png
然后就可以到你的项目中开始配置了。
①配置AndroidManifest.xml
增加权限:
android:protectionLevel="signature" />
推送服务需要配置的service和receiver:
自定义一个BroadcastReceiver类:
public class XiaoMiMessageReceiver extends PushMessageReceiver {
private String mRegId;
private String mTopic;
private String mAlias;
private String mAccount;
private String mStartTime;
private String mEndTime;
/**
* 用来接收服务器发送的透传消息
*
* @param context
* @param miPushMessage
*/
@Override
public void onReceivePassThroughMessage(Context context, MiPushMessage miPushMessage) {
super.onReceivePassThroughMessage(context, miPushMessage);
if (!TextUtils.isEmpty(miPushMessage.getTopic())) {
mTopic = miPushMessage.getTopic();
} else if (!TextUtils.isEmpty(miPushMessage.getAlias())) {
mAlias = miPushMessage.getAlias();
}
Message msg = Message.obtain();
}
/**
* 用来接收服务器发来的通知栏消息(用户点击通知栏时触发)
*
* @param context
* @param miPushMessage
*/
@Override
public void onNotificationMessageClicked(Context context, MiPushMessage miPushMessage) {
super.onNotificationMessageClicked(context, miPushMessage);
String url = "";
url = miPushMessage.getExtra().get(MessageEntity.Link);
Intent intentGo = new Intent();
intentGo.putExtra("link", url);
intentGo.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
intentGo.setClass(context, XiaoMiPushMessageActivity.class);
context.startActivity(intentGo);
}
/**
* 用来接收服务器发来的通知栏消息(消息到达客户端时触发,并且可以接收应用在前台时不弹出通知的通知消息)
*
* @param context
* @param miPushMessage
*/
@Override
public void onNotificationMessageArrived(Context context, MiPushMessage miPushMessage) {
super.onNotificationMessageArrived(context, miPushMessage);
}
/**
* 用来接受客户端向服务器发送注册命令消息后返回的响应。
*
* @param context
* @param miPushCommandMessage
*/
@Override
public void onReceiveRegisterResult(Context context, MiPushCommandMessage miPushCommandMessage) {
super.onReceiveRegisterResult(context, miPushCommandMessage);
String command = miPushCommandMessage.getCommand();
List arguments = miPushCommandMessage.getCommandArguments();
String cmdArg1 = ((arguments != null && arguments.size() > 0) ? arguments.get(0) : null);
if (MiPushClient.COMMAND_REGISTER.equals(command)) {
if (miPushCommandMessage.getResultCode() == ErrorCode.SUCCESS) {
mRegId = cmdArg1;
Log.d("XiaoMiMessageReceiver", "xiaomi push id:" + mRegId);
//在这里就可以保存mRegId了,传给后台服务器
}
}
}
/**
* 用来接收客户端向服务器发送命令消息后返回的响应
*
* @param context
* @param miPushCommandMessage
*/
@Override
public void onCommandResult(Context context, MiPushCommandMessage miPushCommandMessage) {
super.onCommandResult(context, miPushCommandMessage);
}
}
上面的代码中,需要注意这一个方法:
/**
* 用来接收服务器发来的通知栏消息(用户点击通知栏时触发)
*
* @param context
* @param miPushMessage
*/
@Override
public void onNotificationMessageClicked(Context context, MiPushMessage miPushMessage) {
super.onNotificationMessageClicked(context, miPushMessage);
String url = "";
url = miPushMessage.getExtra().get(MessageEntity.Link);
Intent intentGo = new Intent();
intentGo.putExtra("link", url);
intentGo.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
intentGo.setClass(context, XiaoMiPushMessageActivity.class);
context.startActivity(intentGo);
}
如果在onNotificationMessageClicked方法中执行跳转,会发现有时候跳转不了,或者根本无响应,不知是因为上面方法都是非UI线程,还是小米的系统机制问题,(APP退到后台时间长,或者手动完全退出APP之后,点击消息通知也无法重新唤醒APP)在跳转时加入
intentGo.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
然后为了方便分离逻辑,我新建了一个空白的activity,小米的推送服务先跳转到这个空白的activity,然后在activity中处理自己的逻辑(例如APP是否已经正在使用,是就直接提示不重启,没在使用APP就重新唤起APP)
public class XiaoMiPushMessageActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
int nativeRedirect = intent.getIntExtra("redirect", 0);
String url = intent.getStringExtra("link");
int pageType = intent.getIntExtra("pageType", 0);
if (TnaotApplication.Companion.instance().isExistMainActivity()) {
//app已经启动
ActivityUtils.startTagActivityForNotification(this, nativeRedirect, url, pageType, openMethod, newsType, newsId, title, title, content, thumb, false);
} else {
//APP重新唤醒
Intent startIntent = PushUtil.initIntentData(nativeRedirect, url, pageType, newsType, newsId, thumb, title, content);
startIntent.setClass(this, MainActivity.class);
startActivity(startIntent);
}
finish();
}
}
在AndroidManifest.xml中注册这个activity:
android:name=".mctpush.mipush.XiaoMiPushMessageActivity"
android:configChanges="keyboardHidden|screenSize|orientation"
android:screenOrientation="portrait"
android:theme="@style/SplashTheme" />
最后就是application中初始化小米推送:
public static void initMiPush(Context context) {
if (shouldInit(context)) {
MiPushClient.registerPush(context, MI_APP_ID, MI_APP_KEY);
}
}
到这里就完成了小米推送的集成了。