Android集成小米推送教程 中控易动

浏览:169 发布日期:2021/04/25 分类:技术分享 关键字: 前端开发;技术分享;
目前Android应用在后台杀死进程之后,就收不到通知了,想要接收消息推送,需要集成各个厂商的推送SDK,以小米为例

1.接入前准备
1. 登录小米开发者网站,创建App并获取AppId,AppKey和AppSecret。


2. 下载Android客户端SDK压缩包。

下载地址:dev.xiaomi.com/mipush/down… 。

压缩包中包含了Server SDK、Client SDK和android DEMO。

2. SDK接入指南
2.1. 配置AndroidManifest.xml文件
小米推送服务SDK支持的最低安卓版本为2.3。

<uses-sdk android:minSdkVersion="9"/>

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

<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.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:

<service

android:name="com.xiaomi.push.service.XMPushService"

android:enabled="true"

android:process=":pushservice" />

<!--注:此service必须在3.0.1版本以后(包括3.0.1版本)加入-->

<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.mipush.sdk.PushMessageHandler"

android:enabled="true"

android:exported="true" />

<!--注:此service必须在2.2.5版本以后(包括2.2.5版本)加入-->

<service

android:name="com.xiaomi.mipush.sdk.MessageHandleService"

android:enabled="true" />

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

这里将XMPushService和PingReceiver定义在了pushservice进程中,您也可以配置其运行在任意进程。如果没有配置android:process这个属性,那么它们将运行在应用的主进程中。

2.2. 自定义一个BroadcastReceiver类
为了接收消息,您需要自定义一个继承自PushMessageReceiver类的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<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文件中

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

2.3. 注册推送服务
通过调用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<RunningAppProcessInfo> processInfos = am.getRunningAppProcesses();

String mainProcessName = getApplicationInfo().processName;

int myPid = Process.myPid();

for (RunningAppProcessInfo info : processInfos) {

if (info.pid == myPid && mainProcessName.equals(info.processName)) {

return true;

}

}

return false;

}

}

3. 测试
创建推送


填写推送内容


把应用在后台关闭掉,点击推送,仍可以收到推送通知,就说明集成成功了。
最佳答案
评论( 相关
后面还有条评论,点击查看>>