最近项目要用到小米推送,但是网上关于Xamarin 小米推送的资料真的是少之又少,可以说是没有,非常之蛋疼。在经过本人研究成功推送之后,我觉得要把经验资料分享出来!!!ps:如果是原生Android开发使用小米推送真的是非常方便,SDK Demo下载下来嵌入改一下包名之类的差不多就搞定了,Xamarin开发的话还要改成C#,算了,废话不多说。
下载网址:http://dev.xiaomi.com/mipush/downpage/
SDK包里有Eclipse和Android Studio的Demo以及MiPush_SDK_Client_3_1_2.jar。Demo只需要简单地改一些参数(类如包名、AppID、AppKey)就可以直接用了,Android原生开发就可以直接使用了,但是是没有Xamarin C#版本的。
1 这个是用来制作小米推送SDK,将Jar包转成vs可以引用的.dll
2 将小米推送SDK的jar包加进项目中的jars包下
3 将jar包属性改为EmbeddedJar
4 项目生成,即可获得我们需要的.dll,在项目对应下的mipush\bin\Debug文件夹中。
2.
推送服务需要的权限列表
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 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 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文件中
6.推送服务初始化
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 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();
}
}
}
}
}