Android 对接小米PUSH

Android 对接小米PUSH

在使用小米推送服务前,开发者需要先登录小米开发者网站https://dev.mi.com/console/ ,创建app,在更多服务中获取AppId, AppKey, AppSecret。

配置AndroidManifest.xml文件

推送服务需要的权限列表:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<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的包名-->

推送服务需要配置的service和receiver:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<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>

自定义一个BroadcastReceiver类

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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<String> 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<String> 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文件中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<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>

注册推送服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*
*
*
*/
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(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (shouldInit()) {
MiPushClient.registerPush(this, APP_ID, APP_KEY);
}
}

到此已经你已经完成了一个简单的消息推送服务。利用

前往小米消息推送平台PUSH你的第一个消息吧~

Enjoy~

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×