最近项目要用到小米推送,但是网上关于Xamarin 小米推送的资料真的是少之又少,可以说是没有,非常之蛋疼。在经过本人研究成功推送之后,我觉得要把经验资料分享出来!!!ps:如果是原生Android开发使用小米推送真的是非常方便,SDK Demo下载下来嵌入改一下包名之类的差不多就搞定了,Xamarin开发的话还要改成C#,算了,废话不多说。
1.下载小米推送客户端SDK。
下载网址:http://dev.xiaomi.com/mipush/downpage/
SDK包里有Eclipse和Android Studio的Demo以及MiPush_SDK_Client_3_1_2.jar。Demo只需要简单地改一些参数(类如包名、AppID、AppKey)就可以直接用了,Android原生开发就可以直接使用了,但是是没有Xamarin C#版本的。
2.新建一个Xamarin.Android项目,Bindings Library(Android)。
1 这个是用来制作小米推送SDK,将Jar包转成vs可以引用的.dll
2 将小米推送SDK的jar包加进项目中的jars包下
3 将jar包属性改为EmbeddedJar
4 项目生成,即可获得我们需要的.dll,在项目对应下的mipush\bin\Debug文件夹中。
3.新建一个Android项目。
1 新建一个Android项目,过程省略; 2 添加对mipush.dll的引用;
4.配置AndroidManifest.xml
1.小米推送服务SDK支持的最低安卓版本为2.2。
<uses-sdk android:minSdkVersion="8" />
2.
推送服务需要的权限列表
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" />
<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" />
<!-- the following 2 com.xiaomi.mipushdemo should be changed to your package name -->
<permission android:name="com.Gary.mipushDemo.permission.MIPUSH_RECEIVE" android:protectionLevel="signature" />
<uses-permission android:name="com.Gary.mipushDemo.permission.MIPUSH_RECEIVE" />
<uses-permission android:name="android.permission.VIBRATE" />
3. 推送服务需要配置的service和receiver
<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.push.service.XMPushService" android:enabled="true" android:process=":pushservice" />
<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" />
<receiver android:name="com.gary.mipushdemo.DemoMessageReceiver" android:exported="true">
<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>
<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>
4.自定义BroadCastReceiver类 创建一个类并继承PushMessageReceiver类,并重写父类的onCommandResult和onReceiveMessage方法,然后把该receiver注册到AndroidManifest.xml文件中。
a)onCommandResult服务端接收客户端请求后返回的响应消息
b)onReceiveMessage用来接收服务器向客户端发送的即时消息
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Com.Xiaomi.Mipush.Sdk;
using Java.Text;
using Android.Util;
using Android.Text;
namespace Gary.mipushDemo
{
public class DemoMessageReceiver : PushMessageReceiver
{
private string mRegId;
private string mTopic;
private string mAlias;
private string mAccount;
private string mStartTime;
private string mEndTime;
public override void OnReceivePassThroughMessage(Context context, MiPushMessage message)
{
if (!TextUtils.IsEmpty(message.Topic))
{
mTopic = message.Topic;
}
else if (!TextUtils.IsEmpty(message.Alias))
{
mAlias = message.Alias;
}
}
public override void OnNotificationMessageClicked(Context context, MiPushMessage message)
{
if (!TextUtils.IsEmpty(message.Topic))
{
mTopic = message.Topic;
}
else if (!TextUtils.IsEmpty(message.Alias))
{
mAlias = message.Alias;
}
}
public override void OnNotificationMessageArrived(Context context, MiPushMessage message)
{
if (!TextUtils.IsEmpty(message.Topic))
{
mTopic = message.Topic;
}
else if (!TextUtils.IsEmpty(message.Alias))
{
mAlias = message.Alias;
}
}
// 用来接收客户端向服务器发送命令消息后返回的响应
public override void OnCommandResult(Context context, MiPushCommandMessage message)
{
string command = message.Command;
IList<string> arguments = message.CommandArguments;
string cmdArg1 = ((arguments != null && arguments.Count() > 0) ? arguments[0] : null);
string cmdArg2 = ((arguments != null && arguments.Count() > 1) ? arguments[1] : null);
string log;
if (MiPushClient.CommandRegister.Equals(command))
{
if (message.ResultCode == ErrorCode.Success)
{
mRegId = cmdArg1;
}
else
{
}
}
else if (MiPushClient.CommandSetAlias.Equals(command))
{
if (message.ResultCode == ErrorCode.Success)
{
mAlias = cmdArg1;
}
else
{
}
}
else if (MiPushClient.CommandUnsetAlias.Equals(command))
{
if (message.ResultCode == ErrorCode.Success)
{
mAlias = cmdArg1;
}
else
{
}
}
else if (MiPushClient.CommandSetAccount.Equals(command))
{
if (message.ResultCode == ErrorCode.Success)
{
mAccount = cmdArg1;
}
else
{
}
}
else if (MiPushClient.CommandUnsetAccount.Equals(command))
{
if (message.ResultCode == ErrorCode.Success)
{
mAccount = cmdArg1;
}
else
{
}
}
else if (MiPushClient.CommandSubscribeTopic.Equals(command))
{
if (message.ResultCode == ErrorCode.Success)
{
mTopic = cmdArg1;
}
else
{
}
}
else if (MiPushClient.CommandUnsubscribeTopic.Equals(command))
{
if (message.ResultCode == ErrorCode.Success)
{
mTopic = cmdArg1;
}
else
{
}
}
else if (MiPushClient.CommandSetAcceptTime.Equals(command))
{
if (message.ResultCode == ErrorCode.Success)
{
mStartTime = cmdArg1;
mEndTime = cmdArg2;
}
else
{
}
}
else
{
log = message.Reason;
}
}
public override void OnReceiveRegisterResult(Context context, MiPushCommandMessage message)
{
string command = message.Command;
IList<string> arguments = message.CommandArguments;
string cmdArg1 = ((arguments != null && arguments.Count() > 0) ? arguments[0] : null);
string log;
if (MiPushClient.CommandRegister.Equals(command))
{
if (message.ResultCode == ErrorCode.Success)
{
mRegId = cmdArg1;
}
else
{
}
}
else
{
log = message.Reason;
}
}
}
}
5.将自定义的BroadcastReceiver注册到AndroidManifest.xml文件中
<receiver android:name="com.gary.mipushdemo.DemoMessageReceiver" android:exported="true">
<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>
6.推送服务初始化
通过调用MiPushClient.registerPush来初始化小米推送服务。笔者建议在应用的Application类的onCreate()方法内进行注册,这样注册成功后只要应用进程不被结束,那么推送服务就是一直可用的。不建议在某一个Activity中进行注册,因为随着Activity生命周期的结束,推送服务很可能会失效。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Com.Xiaomi.Mipush.Sdk;
using Android.Text;
using static Android.App.ActivityManager;
using Com.Xiaomi.Channel.Commonutils.Logger;
using Android.Util;
using Java.Lang;
namespace Gary.mipushDemo
{
public class DemoApplication : Application
{
// user your appid the key.
private static string APP_ID = "2882303761517520746";
// user your appid the key.
private static string APP_KEY = "5731752028746";
// 此TAG在adb logcat中检索自己所需要的信息, 只需在命令行终端输入 adb logcat | grep
// com.xiaomi.mipushdemo
public static string TAG = "com.Gary.mipushDemo";
private static DemoHandler sHandler = null;
public override void OnCreate()
{
base.OnCreate();
// 注册push服务,注册成功后会向DemoMessageReceiver发送广播
// 可以从DemoMessageReceiver的onCommandResult方法中MiPushCommandMessage对象参数中获取注册信息
if (shouldInit())
{
MiPushClient.RegisterPush(this, APP_ID, APP_KEY);
}
if (sHandler == null)
{
sHandler = new DemoHandler(this);
}
}
private bool shouldInit()
{
ActivityManager am = ((ActivityManager)GetSystemService(ActivityService));
IList<RunningAppProcessInfo> processInfos = am.RunningAppProcesses;
string mainProcessName = PackageName;
int myPid = Android.OS.Process.MyPid();
foreach (RunningAppProcessInfo info in processInfos)
{
if (info.Pid == myPid && mainProcessName.Equals(info.ProcessName))
{
return true;
}
}
return false;
}
public static DemoHandler getHandler()
{
return sHandler;
}
public class DemoHandler : Handler
{
private Context context;
public DemoHandler(Context context)
{
this.context = context;
}
public override void HandleMessage(Message msg)
{
string s = (string)msg.Obj;
if (!TextUtils.IsEmpty(s))
{
Toast.MakeText(context, s, ToastLength.Long).Show();
}
}
}
}
}
5.测试截图
1.登录上小米开发者站(需要注册小米账号并且申请开发者); 2.发送推送;
3.手机接收到推送。(小米推送在MIUI的小米手机上有比较好的体验,APP退出后仍然可以接收到推送,其他牌子手机可能就接收不到)