小米推送集成

云通讯 SDK在5.4.0之后开始支持小米推送。开发者需要根据小米官网文档集成小米推送。 小米推送服务集成

1.准备工作

登录小米开发者网站,注册app,申请AppId,AppKey。

AppId和AppKey是客户端的身份标识,在客户端SDK初始化时使用。然后下载安卓sdk并导入,小米sdk下载

2.上传推送证书

注册完成后,需要在云通讯开发者后台上传推送证书,云通讯管理控制台—>IM基础功能配置—>推送证书,然后输入你在小米开放后台创建的应用的APPID、APPKey及包名。

3.配置AndroidMainfest.xml文件

(1) 小米推送服务SDK支持的最低安卓版本为2.2。

       
	//如下				
	<uses-sdk  android:minSdkVersion="8"/>
                    

(2) 推送服务需要的权限列表。

     
//必选
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.VIBRATE"/> 
<permission android:name="com.xiaomi.mipushdemo.permission.MIPUSH_RECEIVE" 
android:protectionLevel="signature" /> 
//这里com.xiaomi.mipushdemo改成app的包名  
<uses-permission android:name="com.xiaomi.mipushdemo.permission.MIPUSH_RECEIVE" /> 
//这里com.xiaomi.mipushdemo改成app的包名
                

(3) 推送服务需要配置的service和receiver。

     
//小米
<service  android:enabled="true"  android:process=":pushservice"  
android:name="com.xiaomi.push.service.XMPushService"/> 
<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必须在3.0.1版本以后(包括3.0.1版本)加入
<service   
android:enabled="true"   
android:exported="true"   android:name="com.xiaomi.mipush.sdk.PushMessageHandler" />  
<service 
android:enabled="true"   android:name="com.xiaomi.mipush.sdk.MessageHandleService" />  
//注:此service必须在2.2.5版本以后(包括2.2.5版本)加入
<receiver   
android:exported="true"   android:name="com.xiaomi.push.service.receivers.NetworkStatusReceiver" >   
<intent-filter>     
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />     
<category android:name="android.intent.category.DEFAULT" />   
</intent-filter> 
</receiver> 
<receiver   
android:exported="false"   
android:process=":pushservice"   android:name="com.xiaomi.push.service.receivers.PingReceiver" >   
<intent-filter>     
<action android:name="com.xiaomi.push.PING_TIMER" />   
</intent-filter> 
</receiver>
                    

备注:

这里将XMPushService和PingReceiver定义在了pushservice进程中,您也可以配置其运行在任意进程。如果没有配置android:process这个属性,那么它们将运行在应用的主进程中。要在调试程序时检查AndroidManifest.xml是否符合上述配置,使用3.4.15所述的API:checkManifest(Context) 。

4.注册推送服务

通过调用MiPushClient.registerPush来初始化小米推送服务。注册成功后,您可以在自定义的onCommandResult和onReceiveRegisterResult中收到注册结果,其中的regId即是当前设备上当前app的唯一标示。您可以将regId上传到自己的服务器,方便向其发消息。

为了提高push的注册率,你可以在Application的onCreate中初始化push。你也可以根据需要,在其他地方初始化push。代码如下:

public class DemoApplication extends Application {      
public static final String APP_ID = "your appid";     
public static final String APP_KEY = "your appkey";     
public static final String TAG = "your packagename";      
@Override     
public void onCreate() {         
super.onCreate();//初始化push推送服务         
if(shouldInit()) {             
MiPushClient.registerPush(this, APP_ID, 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 = Process.myPid();         
for (RunningAppProcessInfo info : processInfos) {             
if (info.pid == myPid && mainProcessName.equals(info.processName)) {                 
return true;             
  }         
}         
return false;     
  } 
}
			   

4.自定义一个BroadcastReceiver类

为了接收消息,您需要自定义一个继承自PushMessageReceiver类的BroadcastReceiver, 实现其中的onReceivePassThroughMessage,onNotificationMessageClicked,onNotificationMessageArrived, onCommandResult和onReceiveRegisterResult方法,然后把该receiver注册到AndroidManifest.xml文件中。 onReceivePassThroughMessage用来接收服务器发送的透传消息,onNotificationMessageClicked用来接收服务器发来的通知栏消息(用户点击通知栏时触发), onNotificationMessageArrived用来接收服务器发来的通知栏消息(消息到达客户端时触发,并且可以接收应用在前台时不弹出通知的通知消息), onCommandResult用来接收客户端向服务器发送命令消息后返回的响应,onReceiveRegisterResult用来接受客户端向服务器发送注册命令消息后返回的响应。

例如:自定义的BroadcastReceiver

public class DemoMessageReceiver extends PushMessageReceiver {     
private String mRegId;     
private long mResultCode = -1;     
private String mReason;     
private String mCommand;     
private String mMessage;     
private String mTopic;     
private String mAlias;     
private String mUserAccount;     
private String mStartTime;     
private String mEndTime;     
@Override     
public void onReceivePassThroughMessage(Context context, MiPushMessage message) {         
mMessage = message.getContent();         
if(!TextUtils.isEmpty(message.getTopic())) {             
mTopic=message.getTopic();         
} else if(!TextUtils.isEmpty(message.getAlias())) {             
mAlias=message.getAlias();         
} else if(!TextUtils.isEmpty(message.getUserAccount())) {             
mUserAccount=message.getUserAccount();         
}     
}     
@Override     
public void onNotificationMessageClicked(Context context, MiPushMessage message) {         
mMessage = message.getContent();         
if(!TextUtils.isEmpty(message.getTopic())) {             
mTopic=message.getTopic();         
} else if(!TextUtils.isEmpty(message.getAlias())) {             
mAlias=message.getAlias();         
} else if(!TextUtils.isEmpty(message.getUserAccount())) {             
mUserAccount=message.getUserAccount();         
}     
}     
@Override     
public void onNotificationMessageArrived(Context context, MiPushMessage message) {         
mMessage = message.getContent();         
if(!TextUtils.isEmpty(message.getTopic())) {             
mTopic=message.getTopic();         
} else if(!TextUtils.isEmpty(message.getAlias())) {             
mAlias=message.getAlias();         
} else if(!TextUtils.isEmpty(message.getUserAccount())) {             
mUserAccount=message.getUserAccount();         
}     
}     
@Override     
public void onCommandResult(Context context, MiPushCommandMessage message) {         
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);         
if (MiPushClient.COMMAND_REGISTER.equals(command)) {             
if (message.getResultCode() == ErrorCode.SUCCESS) {                 
mRegId = cmdArg1;             
}         
} else if (MiPushClient.COMMAND_SET_ALIAS.equals(command)) {             
if (message.getResultCode() == ErrorCode.SUCCESS) {                 
mAlias = cmdArg1;             
}         
} else if (MiPushClient.COMMAND_UNSET_ALIAS.equals(command)) {             
if (message.getResultCode() == ErrorCode.SUCCESS) {                 
mAlias = cmdArg1;             
}         
} else if (MiPushClient.COMMAND_SUBSCRIBE_TOPIC.equals(command)) {             
if (message.getResultCode() == ErrorCode.SUCCESS) {                 
mTopic = cmdArg1;             
}         
} else if (MiPushClient.COMMAND_UNSUBSCRIBE_TOPIC.equals(command)) {             
if (message.getResultCode() == ErrorCode.SUCCESS) {                 
mTopic = cmdArg1;             
}         
} else if (MiPushClient.COMMAND_SET_ACCEPT_TIME.equals(command)) {             
if (message.getResultCode() == ErrorCode.SUCCESS) {                 
mStartTime = cmdArg1;                 
mEndTime = cmdArg2;             
}         
}      
}     
@Override     
public void onReceiveRegisterResult(Context context, MiPushCommandMessage message) {         
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);         
if (MiPushClient.COMMAND_REGISTER.equals(command)) {             
if (message.getResultCode() == ErrorCode.SUCCESS) {                 
mRegId = cmdArg1;             
}         
}      
} }
将自定义的BroadcastReceiver注册到AndroidManifest.xml文件中
<receiver   android:exported="true"   android:name="com.xiaomi.mipushdemo.DemoMessageReceiver">           
//这里com.xiaomi.mipushdemo.DemoMessageRreceiver改成app中定义的完整类名  <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>

			   

5.上报小米Token

将获取到的Token,上报云通讯SDK。在云通讯SDK初始化登录成功之后调用。

//上报小米Token
ECDevice.reportXiaoMiToken(token);
		   

文档更新时间:2017年9月21日