小米推送通道是由小米官方提供的系统级推送通道。在小米手机上,推送消息能通过小米的系统通道抵达终端,并且无需打开应用就能够收到推送。
注意:
- 在测试小米通道推送消息时,应尽量避免使用“test”、“测试”等字眼,否则可能会被小米拦截进入“非重要消息”中。
前往 小米开放平台 >推送运营平台,开启应用的消息推送服务:
AS 开发建议使用 jcenter 依赖接入。引入小米推送的 jar 包。
implementation 'com.tencent.tpns:xiaomi:[VERSION]-release'//小米推送 [VERSION] 为当前 SDK 版本号,版本号可在 Android SDK 发布动态查看
说明:小米推送 [VERSION] 为当前 SDK 版本号,版本号可在 Android SDK 发布动态 查看。
下载 SDK 安装包。
打开 Other-Push-jar 文件夹, 导入小米推送相关 jar 包,将 xm4tpns1.1.2.1.jar 导入项目工程中。
在配置好移动推送的基础上 ,新增小米推送的配置:
<application>
<activity
android:name="com.xiaomi.mipush.sdk.NotificationClickedActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:launchMode="singleInstance"
android:exported="true"
android:enabled="true">
</activity>
<service
android:name="com.xiaomi.push.service.XMPushService"
android:enabled="true"
android:process=":pushservice" />
<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:name="com.xiaomi.mipush.sdk.PushMessageHandler"
android:enabled="true"
android:exported="true" />
<service
android:name="com.xiaomi.mipush.sdk.MessageHandleService"
android:enabled="true" />
<!-- 注:此service必须在2.2.5版本以后(包括2.2.5版本)加入 -->
<receiver
android:name="com.xiaomi.push.service.receivers.NetworkStatusReceiver"
android:exported="true" >
</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>
</application>
<!-- 注:小米push 需要的权限 begin -->
<permission
android:name="应用包名.permission.MIPUSH_RECEIVE"
android:protectionLevel="signature" />
<!-- 这里 应用包名 改成app的包名 -->
<uses-permission android:name="应用包名.permission.MIPUSH_RECEIVE" />
<!-- 这里 应用包名 改成app的包名 -->
<!-- 注:小米push 需要的权限 end -->
在 AndroidManifest.xml
增加 Receiver
,配置如下:
<receiver
android:exported="true"
android:name="com.tencent.android.mipush.XMPushMessageReceiver">
<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>
设置小米 AppID 和 AppKey。
XGPushConfig.setMiPushAppId(getApplicationContext(), "APPID");
XGPushConfig.setMiPushAppKey(getApplicationContext(), "APPKEY");
//打开第三方推送
XGPushConfig.enableOtherPush(getApplicationContext(), true);
//注册成功的日志如下
I/TPush: [OtherPushClient] handleUpdateToken other push token is : 3CvDLfyPRArAGnv****dvQ7rYko+OthWo90rW+Edeqn53RUudp6U1dhySpV35 other push type: xiaomi
I/TPush: [PushServiceBroadcastHandler] >> bind OtherPushToken success ack with [accId = 1500001048 , rsp = 0] token = 03be2036762f******33bce72d40eb5e677a otherPushType = xiaomi otherPushToken = 3CvDLfyPRArAGnv****dvQ7rYko+OthWo90rW+Edeqn53RUudp6U1dhySpV35G
如需通过点击回调获取参数或者跳转自定义页面,可以通过使用 Intent 来实现,详情请参见 Android 常见问题。
-keep class com.xiaomi.**{*;}
-keep public class * extends com.xiaomi.mipush.sdk.PushMessageReceiver
说明:混淆规则需要放在 App 项目级别的 proguard-rules.pro 文件中。
若您观察到如下类似日志则说明小米厂商通道注册失败,开发者可以通过以下方式获取小米推送注册错误码:
[OtherPushClient] handleUpdateToken other push token is : other push type: xiaomi
推送服务 debug 模式下,过滤关键字 “OtherPush”,查看相关返回码日志(例如 [OtherPush_XG_MI] register failed, errorCode: 22022, reason: Invalid package name: com.xxx.xxx
),并前往 厂商通道注册失败排查指南 查找对应原因,获取解决办法。
本页内容是否解决了您的问题?