小言_互联网的博客

Android集成小米推送教程

459人阅读  评论(0)

前言

目前Android应用在后台杀死进程之后,就收不到通知了,想要接收消息推送,需要集成各个厂商的推送SDK,以小米为例

1.接入前准备

1. 登录小米开发者网站(https://dev.mi.com/console/appservice/),创建App并获取AppId,AppKey和AppSecret。

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

下载地址:http://dev.xiaomi.com/mipush/downpage/ 。

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

2. SDK接入指南

2.1.  配置AndroidManifest.xml文件

小米推送服务SDK支持的最低安卓版本为2.3。

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

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


  
  1. <uses-permission android:name= "android.permission.WRITE_EXTERNAL_STORAGE" />​
  2. <uses-permission android:name= "android.permission.INTERNET" /> 
  3. <uses-permission android:name= "android.permission.ACCESS_NETWORK_STATE" />
  4. <uses-permission android:name= "android.permission.ACCESS_WIFI_STATE" /> 
  5. <uses-permission android:name= "android.permission.READ_PHONE_STATE" /> 
  6. <uses-permission android:name= "android.permission.VIBRATE"/> 
  7. <permission android:name= "com.xiaomi.mipushdemo.permission.MIPUSH_RECEIVE" android:protectionLevel= "signature" /> <!--这里com.xiaomi.mipushdemo改成app的包名-->
  8. <uses-permission android:name= "com.xiaomi.mipushdemo.permission.MIPUSH_RECEIVE" /><!--这里com.xiaomi.mipushdemo改成app的包名-->

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


  
  1. <service
  2. android:name= "com.xiaomi.push.service.XMPushService"
  3. android:enabled= "true"
  4. android:process= ":pushservice" />
  5. <!--注:此service必须在 3.0. 1版本以后(包括 3.0. 1版本)加入-->
  6. <service
  7. android:name= "com.xiaomi.push.service.XMJobService"
  8. android:enabled= "true"
  9. android:exported= "false"
  10. android:permission= "android.permission.BIND_JOB_SERVICE"
  11. android:process= ":pushservice" />
  12. <service
  13. android:name= "com.xiaomi.mipush.sdk.PushMessageHandler"
  14. android:enabled= "true"
  15. android:exported= "true" />
  16. <!--注:此service必须在 2.2. 5版本以后(包括 2.2. 5版本)加入-->
  17. <service
  18. android:name= "com.xiaomi.mipush.sdk.MessageHandleService"
  19. android:enabled= "true" />
  20. <receiver
  21. android:name= "com.xiaomi.push.service.receivers.NetworkStatusReceiver"
  22. android:exported= "true">
  23. <intent-filter>
  24. <action android:name= "android.net.conn.CONNECTIVITY_CHANGE" />
  25. <category android:name= "android.intent.category.DEFAULT" />
  26. </intent-filter>
  27. </receiver>
  28. <receiver
  29. android:name= "com.xiaomi.push.service.receivers.PingReceiver"
  30. android:exported= "false"
  31. android:process= ":pushservice">
  32. <intent-filter>
  33. <action android:name= "com.xiaomi.push.PING_TIMER" />
  34. </intent-filter>
  35. </receiver>

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

2.2. 自定义一个BroadcastReceiver类

为了接收消息,您需要自定义一个继承自PushMessageReceiver类的BroadcastReceiver


  
  1. public class DemoMessageReceiver extends PushMessageReceiver {
  2.     private String mRegId;
  3.     private long mResultCode = - 1;
  4.     private String mReason;
  5.     private String mCommand;
  6.     private String mMessage;
  7.     private String mTopic;
  8.     private String mAlias;
  9. private String mUserAccount;
  10.     private String mStartTime;
  11.     private String mEndTime;
  12.     @Override
  13.     public void onReceivePassThroughMessage(Context context, MiPushMessage message) {
  14.         mMessage = message.getContent();
  15.         if(!TextUtils.isEmpty(message.getTopic())) {
  16.             mTopic=message.getTopic();
  17.         } else if(!TextUtils.isEmpty(message.getAlias())) {
  18.             mAlias=message.getAlias();
  19.         } else if(!TextUtils.isEmpty(message.getUserAccount())) {
  20.             mUserAccount=message.getUserAccount();
  21.         }
  22.     }
  23.     @Override
  24.     public void onNotificationMessageClicked(Context context, MiPushMessage message) {
  25.         mMessage = message.getContent();
  26.         if(!TextUtils.isEmpty(message.getTopic())) {
  27.             mTopic=message.getTopic();
  28.         } else if(!TextUtils.isEmpty(message.getAlias())) {
  29.             mAlias=message.getAlias();
  30.         } else if(!TextUtils.isEmpty(message.getUserAccount())) {
  31.             mUserAccount=message.getUserAccount();
  32.         }
  33.     }
  34.     @Override
  35.     public void onNotificationMessageArrived(Context context, MiPushMessage message) {
  36.         mMessage = message.getContent();
  37.         if(!TextUtils.isEmpty(message.getTopic())) {
  38.             mTopic=message.getTopic();
  39.         } else if(!TextUtils.isEmpty(message.getAlias())) {
  40.             mAlias=message.getAlias();
  41.         } else if(!TextUtils.isEmpty(message.getUserAccount())) {
  42.             mUserAccount=message.getUserAccount();
  43.         }
  44.     }
  45.      @Override
  46.     public void onCommandResult(Context context, MiPushCommandMessage message) {
  47.         String command = message.getCommand();
  48. List<String> arguments = message.getCommandArguments();
  49. String cmdArg1 = ((arguments != null && arguments.size() > 0) ? arguments.get( 0) : null);
  50. String cmdArg2 = ((arguments != null && arguments.size() > 1) ? arguments.get( 1) : null);
  51. if (MiPushClient.COMMAND_REGISTER.equals(command)) {
  52. if (message.getResultCode() == ErrorCode.SUCCESS) {
  53. mRegId = cmdArg1;
  54. }
  55. } else if (MiPushClient.COMMAND_SET_ALIAS.equals(command)) {
  56. if (message.getResultCode() == ErrorCode.SUCCESS) {
  57. mAlias = cmdArg1;
  58. }
  59. } else if (MiPushClient.COMMAND_UNSET_ALIAS.equals(command)) {
  60. if (message.getResultCode() == ErrorCode.SUCCESS) {
  61. mAlias = cmdArg1;
  62. }
  63. } else if (MiPushClient.COMMAND_SUBSCRIBE_TOPIC.equals(command)) {
  64. if (message.getResultCode() == ErrorCode.SUCCESS) {
  65. mTopic = cmdArg1;
  66. }
  67. } else if (MiPushClient.COMMAND_UNSUBSCRIBE_TOPIC.equals(command)) {
  68. if (message.getResultCode() == ErrorCode.SUCCESS) {
  69. mTopic = cmdArg1;
  70. }
  71. } else if (MiPushClient.COMMAND_SET_ACCEPT_TIME.equals(command)) {
  72. if (message.getResultCode() == ErrorCode.SUCCESS) {
  73. mStartTime = cmdArg1;
  74. mEndTime = cmdArg2;
  75. }
  76. }
  77.     }
  78.      @Override
  79.     public void onReceiveRegisterResult(Context context, MiPushCommandMessage message) {
  80.         String command = message.getCommand();
  81. List<String> arguments = message.getCommandArguments();
  82. String cmdArg1 = ((arguments != null && arguments.size() > 0) ? arguments.get( 0) : null);
  83. String cmdArg2 = ((arguments != null && arguments.size() > 1) ? arguments.get( 1) : null);
  84. if (MiPushClient.COMMAND_REGISTER.equals(command)) {
  85. if (message.getResultCode() == ErrorCode.SUCCESS) {
  86. mRegId = cmdArg1;
  87. }
  88. }
  89.     }
  90. }

将自定义的BroadcastReceiver注册到AndroidManifest.xml文件中


  
  1. <receiver
  2. android:exported= "true"
  3. android:name= "com.xiaomi.mipushdemo.DemoMessageReceiver">
  4. <!--这里com.xiaomi.mipushdemo.DemoMessageRreceiver改成app中定义的完整类名-->
  5. <intent-filter>
  6. <action android:name= "com.xiaomi.mipush.RECEIVE_MESSAGE" />
  7. </intent-filter>
  8. <intent-filter>
  9. <action android:name= "com.xiaomi.mipush.MESSAGE_ARRIVED" />
  10. </intent-filter>
  11. <intent-filter>
  12. <action android:name= "com.xiaomi.mipush.ERROR" />
  13. </intent-filter>
  14. </receiver>

2.3. 注册推送服务

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

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


  
  1. public class DemoApplication extends Application {
  2. public static final String APP_ID = "your appid";
  3. public static final String APP_KEY = "your appkey";
  4. public static final String TAG = "your packagename";
  5. @Override
  6. public void onCreate() {
  7. super.onCreate();
  8. //初始化push推送服务
  9. if(shouldInit()) {
  10. MiPushClient.registerPush( this, APP_ID, APP_KEY);
  11. }
  12. //打开Log
  13. LoggerInterface newLogger = new LoggerInterface() {
  14. @Override
  15. public void setTag(String tag) {
  16. // ignore
  17. }
  18. @Override
  19. public void log(String content, Throwable t) {
  20. Log.d(TAG, content, t);
  21. }
  22. @Override
  23. public void log(String content) {
  24. Log.d(TAG, content);
  25. }
  26. };
  27. Logger.setLogger( this, newLogger);
  28. }
  29. private boolean shouldInit() {
  30. ActivityManager am = ((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE));
  31. List<RunningAppProcessInfo> processInfos = am.getRunningAppProcesses();
  32. String mainProcessName = getApplicationInfo().processName;
  33. int myPid = Process.myPid();
  34. for (RunningAppProcessInfo info : processInfos) {
  35. if (info.pid == myPid && mainProcessName.equals(info.processName)) {
  36. return true;
  37. }
  38. }
  39. return false;
  40. }
  41. }

3. 测试

创建推送

填写推送内容

把应用在后台关闭掉,点击推送,仍可以收到推送通知,就说明集成成功了。


转载:https://blog.csdn.net/baidu_40202612/article/details/113942756
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场