Android集成小米推送教程
目前Android应用在后台杀死进程之后,就收不到通知了,想要接收消息推送,需要集成各个厂商的推送SDK,以小米为例。
1.接入前准备
1.1 登录小米开发者网站(
https://dev.mi.com/console/appservice/),创建App并获取AppId,AppKey和AppSecret。
1.2 下载Android客户端SDK压缩包。
下载地址:
http://dev.xiaomi.com/mipush/downpage/ 。
压缩包中包含了Server SDK、Client SDK和android DEMO。
2. SDK接入指南
2.1 配置AndroidManifest.xml文件
小米推送服务SDK支持的最低安卓版本为2.3。
推送服务需要的权限列表:
推送服务需要配置的service和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(); Listarguments = 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(); Listarguments = 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文件中
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)); ListprocessInfos = 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. 测试
3.1 创建推送
3.2 填写推送内容
把应用在后台关闭掉,点击推送,仍可以收到推送通知,就说明集成成功了。