NET+EF+MVC实现百度云消息推送

笔者最近开发app时,需要用上消息推送,比较了一下市面上的推送服务商,百度消息推送是免费的,所以团队就选用百度云消息推送,笔者是NET+EF+MVC开发api接口,(java版的很完善)但是C#版的百度推送没有官方实例(提供的一个例子是github上的还是10年前的,只有一些参考价值,但最终不能解决问题),下面是笔者通过官方API以及搜集网上的代码再整合集成到自己的项目中来,彻底解决了问题,下面是部分代码,希望给大家提供一个思路:


NET+EF+MVC实现百度云消息推送

注意: IOS需要配置证 书:


NET+EF+MVC实现百度云消息推送

安卓也需要设置



NET+EF+MVC实现百度云消息推送


static List> BaiduKeys = new List>()

{

new KeyValuePair("XyRnNStlURL7YLuldzvhNHGy","2Wk677hGUa78T5uGIV4p56CMQwtM8xvi"), //安卓 0

new KeyValuePair("fkqIGLCg0VQqdzTb55eXBiT6","kfwVegrpGF4IOnwVur1kGPbBAYhPIgG6"),//ios家长端1

};

static void Main(string[] args)

{

//单台安卓机

// string channelId = "4381875329619642658";

string channelId = "4413887959694064256"; //张总

PushSingleDeviceTest(channelId);

// PushSingleDeviceTestWithIOS(); //单台ios测试通过

// PushAllTest(); //批量推送测试通过

// ReportStatisticDeviceTest();

// ReportQueryMsgStatusTest("[\"4381875329619642658\",\"4677160737384896054\"]");

Console.ReadLine();

}

static async void PushSingleDeviceTestWithIOS()

{

PushSingleDeviceRequest request = new PushSingleDeviceRequest();

request.ApiKey = "fkqIGLCg0VQqdzTb55eXBiT6";

request.SecretKey = "kfwVegrpGF4IOnwVur1kGPbBAYhPIgG6";

request.DeviceType = DeviceType.IOS;

request.ChannelId = "4677160737384896054";

request.MsgType = MsgType.Notice;

dynamic aps = #34;大家好,ios单台代码推送测试! { request.ChannelId}";

request.Msg = JsonConvert.SerializeObject(IOSNotice.GetStandardNotice(aps));

request.DeployStatus = 1;

PushSingleDevice push = new PushSingleDevice();

try

{

var response = await push.Call(request);

Console.WriteLine(#34;PushSingleDevice Response MsgId:{response.ResponseParams.MsgId}");

}

catch (Exception ex)

{

Console.WriteLine(ex.Message);

}

}

static async void PushSingleDeviceTest(string channelId)

{

PushSingleDeviceRequest request = new PushSingleDeviceRequest();

FillKey(request);

request.ChannelId = channelId;

request.Msg = #34;飞哥测试 {channelId}";

PushSingleDevice push = new PushSingleDevice();

try

{

var response = await push.Call(request);

Console.WriteLine(#34;PushSingleDevice Response MsgId:{response.ResponseParams.MsgId}");

}

catch (Exception ex)

{

Console.WriteLine(ex.Message);

}

}

static void FillKey(Request request, int idx = 0) //idx:0是安卓,1是ios家长端

{

request.ApiKey = BaiduKeys[idx].Key;

request.SecretKey = BaiduKeys[idx].Value;

request.DeviceType = DeviceType.Android;


// request.DeviceType = DeviceType.IOS;


request.Expires = DateTime.Now.AddHours(1).ToUnixTimestamp();

}

static async void PushAllTest()

{

PushAllRequest request = new PushAllRequest();

FillKey(request);

// request.Msg = #34;Test Msg Send To All Devices";

request.MsgType = MsgType.Notice;

dynamic aps = #34;大家上午好,安卓批量推送测试!";

request.Msg = JsonConvert.SerializeObject(IOSNotice.GetStandardNotice(aps));

request.DeployStatus = 1;

PushAll push = new PushAll();

try

{

var response = await push.Call(request);

Console.WriteLine(#34;该条消息id:{response.ResponseParams.MsgId}");

}

catch (Exception ex)

{

Console.WriteLine(ex.Message);

}

}

static async void ReportStatisticDeviceTest()

{

ReportStatisticDeviceRequest request = new ReportStatisticDeviceRequest();

FillKey(request);

ReportStatisticDevice push = new ReportStatisticDevice();

try

{//当没有任何设备时,api返回的数据就是个坑,返回的是"result":[],而不是"result":{}

push.HttpMethod = "GET";

var response = await push.Call(request);

Console.WriteLine(#34;ReportStatisticDevice TotalNum :{response.ResponseParams.TotalNum}");

}

catch (Exception ex)

{

Console.WriteLine(ex.Message);

}

}

static async void ReportQueryMsgStatusTest(string msgId = null)

{

//4138210314396792811

ReportQueryMsgStatusRequest request = new ReportQueryMsgStatusRequest();

request.MsgId = msgId;

FillKey(request);

ReportQueryMsgStatus push = new ReportQueryMsgStatus();

try

{//推送状态有坑,api说明上没有-1的,实际却可以查出来

var response = await push.Call(request);

Console.WriteLine(#34;ReportQueryMsgStatus TotalNum :{response.ResponseParams.TotalNum}");

}

catch (Exception ex)

{

Console.WriteLine(ex.Message);

}

}


NET+EF+MVC实现百度云消息推送


用以上方法,需要引用一个dll,下载地址可私信,以上是实现消息推送的完整方法,如果你有更好的思路,可以一起探讨。

消息   NET   EF
发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章