由于项目需要给安卓、ios提供提送消息服务。找到了umeng这个平台,官方竟然没有提供.net版本的SDK,同时项目需要就拿出来和大家分享一下需要的同学们可以做个参考,建议官方提供.net版本。

这里就提供、单播、组播和广播模式

1.接口声明

  public interface IMsgService
{
/// <summary>
/// 单播
/// </summary>
/// <param name="msg"></param>
MsgDTO UniCast(string msg, string deviceId); /// <summary>
/// 组播
/// </summary>
/// <param name="msg"></param>
MsgDTO GroupCast(string msg, int operatorId); /// <summary>
/// 广播
/// </summary>
/// <param name="msg"></param>
MsgDTO BroadCast(string msg);
}

2.安卓服务实现

 public class AndroidMsgService : MsgServiceBase, IMsgService
{ protected static readonly string App_Master_Secret = AppSettingHelper.DomainSetting.GetAttribute<string>("Api/UmengMsgList/Android", "appmastersecret"); protected static readonly string App_Key = AppSettingHelper.DomainSetting.GetAttribute<string>("Api/UmengMsgList/Android", "appkey"); private AndroidMsgService()
{ } public static readonly AndroidMsgService Instance =new AndroidMsgService(); protected override string GetSign(string post_body)
{
return (String.Concat("POST", SendUrl, post_body, App_Master_Secret)).MD5().ToLower();
} public MsgDTO BroadCast(string msg)
{
var sendData = new
{
appkey = App_Key,
timestamp = GetTimeStamp,
type = "broadcast",
payload = new
{
display_type = "notification",// 通知,notification
body = new
{
//custom = msg
ticker =msg,
title = msg,
text = msg,
after_open = "go_custom",
custom=""
},
extra = new
{
key1 = "key1",
key2 = "key2"
}
},
policy = new
{
// start_time = "2013-10-29 12:00:00", //定时发送
expire_time = DateTime.Now.AddDays().ToString("yyyy-MM-dd HH:mm:ss")
},
production_mode = ProductMode,
description = "测试广播通知-android"
}; //var result = this.GetResult(sendData, GetSign(sendData.ToJson()));
//return result; var result = this.GetResult(sendData);
return result;
} public MsgDTO GroupCast(string msg, int operatorId)
{
var sendData = new
{
appkey = App_Key,
timestamp = GetTimeStamp,
type = "groupcast",
filter = new
{
where = new
{
and = new[]
{
new {tag=operatorId.ToString()}
}
}
},
payload = new
{
display_type = "notification", // 通知,notification
body = new
{
//custom = msg
ticker = msg,
title = msg,
text = msg,
after_open = "go_custom",
custom = ""
},
extra = new
{
key1 = "key1",
key2 = "key2"
}
},
policy = new
{
expire_time = DateTime.Now.AddDays().ToString("yyyy-MM-dd HH:mm:ss")
},
production_mode = ProductMode,
description = "测试组播通知-Android"
};
var result = base.GetResult(sendData);
return result;
} public MsgDTO UniCast(string msg, string deviceId)
{ var sendData = new
{
appkey = App_Key,
timestamp = GetTimeStamp,
type = "unicast",
device_tokens = deviceId,
payload = new
{
display_type = "notification", // 消息,message
body = new
{
//custom = msg
ticker = msg,
title = msg,
text = msg,
after_open = "go_custom",
custom = ""
},
extra = new
{
key1= "key1",
key2= "key2"
}
},
production_mode= ProductMode,
description = "测试单播"
};
var result = base.GetResult(sendData);
return result; } }

3.IOS服务实现

 public class IosMsgService : MsgServiceBase, IMsgService
{
protected static readonly string App_Master_Secret = AppSettingHelper.DomainSetting.GetAttribute<string>("Api/UmengMsgList/IOS", "appmastersecret"); protected static readonly string App_Key =
AppSettingHelper.DomainSetting.GetAttribute<string>("Api/UmengMsgList/IOS", "appkey"); public static readonly IosMsgService Instance = new IosMsgService(); private IosMsgService()
{ } protected override string GetSign(string post_body)
{
return (String.Concat("POST", SendUrl, post_body, App_Master_Secret)).MD5().ToLower();
} public MsgDTO BroadCast(string msg)
{
var sendData = new
{
appkey = App_Key,
timestamp = GetTimeStamp,
type = "broadcast",
payload = new
{
aps = new { alert = msg } // 苹果必填字段
},
policy = new
{
// start_time = "2013-10-29 12:00:00", //定时发送
expire_time = DateTime.Now.AddDays().ToString("yyyy-MM-dd HH:mm:ss")
},
production_mode = ProductMode,
description = "测试广播通知-iOS"
}; //var result = RequestHelper.HttpUtil<MsgDTO>(String.Concat(SendUrl, $"?sign={GetSign(sendData.ToJson())}"), sendData);
//return result; var result = this.GetResult(sendData);
return result;
} public MsgDTO GroupCast(string msg,int operatorId)
{
var sendData = new
{
appkey = App_Key,
timestamp = GetTimeStamp,
type = "groupcast",
filter = new
{
where = new
{
and = new []
{
new {tag=operatorId.ToString()}
}
}
},
payload = new
{
aps = new { alert = msg } // 苹果必填字段
},
policy = new
{
expire_time = DateTime.Now.AddDays().ToString("yyyy-MM-dd HH:mm:ss")
},
production_mode = ProductMode,
description = "测试组播通知-IOS"
};
var result = base.GetResult(sendData);
return result;
//var result = RequestHelper.HttpUtil<MsgDTO>(String.Concat(SendUrl, $"?sign={GetSign(sendData.ToJson())}"), sendData);
//return result;
} public MsgDTO UniCast(string msg, string deviceId)
{
var sendData = new
{
appkey = App_Key,
timestamp = GetTimeStamp,
type = "unicast",
device_tokens = deviceId,
payload = new
{
aps = new
{
alert = msg
} // 苹果必填字段
},
policy = new
{
expire_time = DateTime.Now.AddDays().ToString("yyyy-MM-dd HH:mm:ss")
},
production_mode= ProductMode,
description = "测试单播消息"
};
var result = base.GetResult(sendData);
return result;
//var result = RequestHelper.HttpUtil<MsgDTO>(String.Concat(SendUrl, $"?sign={GetSign(sendData.ToJson())}"), sendData);
//return result;
}
}

4.MsgServiceBase服务基类

 public abstract class MsgServiceBase
{
protected static readonly string SendUrl = AppSettingHelper.DomainSetting.GetValue("Api/UmengMsgList/RequestUrl"); protected static readonly string ProductMode = AppSettingHelper.DomainSetting.GetAttribute<string>("Api/UmengMsgList", "productmode"); protected abstract string GetSign(string sendData); protected string GetTimeStamp => (DateTime.UtcNow - new DateTime(, , , , , , DateTimeKind.Utc)).TotalMilliseconds.ToString(
"#"); protected MsgDTO GetResult(dynamic sendData)
{ var result = RequestHelper.HttpUtil<MsgDTO>(String.Concat(SendUrl, $"?sign={this.GetSign(JsonConvert.SerializeObject(sendData))}"), sendData);
return result;
}
}

5.HttpUtil

 public static T HttpUtil<T>(string url, dynamic data) where T :new()
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "application/x-www-form-urlencoded"; //设置HTTP头
request.Method = "POST"; //byte[] postdata = Encoding.UTF8.GetBytes(data);
byte[] postdata = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data));
request.ContentLength = postdata.Length; Stream newStream = request.GetRequestStream();
newStream.Write(postdata, , postdata.Length);
newStream.Close(); HttpWebResponse myResponse = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
return (reader.ReadToEnd()).FromJson<T>();//得到结果
}

贴上源码:http://files.cnblogs.com/files/zpc870921/Common.rar

友盟移动开发平台.NET版本SDK的更多相关文章

  1. 友盟分享各平台URL设置

    首先,想要进项友盟分享,需要到各平台去申请ID和KEY 比如想进行微信分享,就到微信开发者平台去创建应用,拿到对应的id和appScreat,然后进行设置: 参考资料

  2. 商业创新不能等?用友低代码开发平台YonBuilder为您加速!

    随着云计算.人工智能.物联网.大数据.5G等新一代技术的快速发展,越来越多的企业希望借助技术的力量加速数智化转型,期许通过更加敏捷和强大的应用系统推动企业的商业创新速度.但传统软件开发周期长.开发成本 ...

  3. 用友低代码开发平台YonBuilder首次亮相DevRun开发者沙龙

    2020年的今天,没有人会再质疑企业上云的必要性与价值所在.从高科技行业到传统领域,大大小小的企业都希望走在变革道路前列,通过企业云加快业务数字化转型,更好地维护和管理企业数据. 然而,大多数企业都很 ...

  4. UmengAppDemo【友盟统计SDK集成以及多渠道打包配置,基于V7.5.3版本】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 这里只是记录下集成友盟统计SDK以及简单配置多渠道打包的步骤.所以1.该Demo不能运行:2.配置多渠道打包只是一种简单的写法,具体 ...

  5. 友盟推送 .NET (C#) 服务端 SDK rest api 调用库

    友盟推送 .NET SDK rest api 介绍 该版本是基于友盟推送2.3版本封装的,网上查询了下发现没有.NET版本的调用库,官方也没有封装.NET的版本,只有python.java.php版本 ...

  6. iOS开发-友盟分享(3)

    iOS 友盟分享 这个主要是提到如何通过友盟去自定义分享的步骤: 一.肯定要去友盟官网下载最新的SDK包,然后将SDK导入到你的工程文件夹里面去: 二.注册友盟账号,将你的APP添加到你的账号里面然后 ...

  7. android开发系列之友盟统计集成

    相比大家都遇到这种情况,当我们的app上线之后,我们想要实时的跟踪了解到app里面的bug情况.新增用户情况.用户相关的行为属性情况等.但是如果自己在app里面去开发集成这些功能,一方面开发工作量还挺 ...

  8. Android中用友盟实现QQ的第三方登录

    //首先应该去友盟的官网注册你的账号,创建一个应用,获得它的APPkey,也可以用它的API上的appkey,下载SDK,下面根据API文档一步步实现就行了. //下面是友盟的APi文档 1.  产品 ...

  9. Android 友盟分享详细集成过程及所遇问题解决

    最近项目需要针对微信.朋友圈.QQ.QQ空间集成友盟分享的功能,说实话,我也是第一次做,期间碰到过很多问题,这篇随笔就来写一下我是怎么集成友盟分享的,还有碰到哪些问题,都是怎样解决的! 其实集成友盟并 ...

随机推荐

  1. Codeforces Round #396 (Div. 2) A B C D 水 trick dp 并查集

    A. Mahmoud and Longest Uncommon Subsequence time limit per test 2 seconds memory limit per test 256 ...

  2. jquery多组图片层次切换的焦点图

    效果:

  3. LightOJ 1135 - Count the Multiples of 3 线段树

    http://www.lightoj.com/volume_showproblem.php?problem=1135 题意:给定两个操作,一个对区间所有元素加1,一个询问区间能被3整除的数有多少个. ...

  4. c# Stream to File的知识点

    个人倾向使用File.WriteAllByte写入文件: //Stream to File MemoryStream ms=...Stream; ms.Position = ; byte[] buff ...

  5. 【Foreign】Walk [暴力]

    Walk Time Limit: 20 Sec  Memory Limit: 256 MB Description Input Output Sample Input 3 1 2 3 1 3 9 Sa ...

  6. jsp之jstl核心标签库

    JSTL核心标签库技术 1. JSTL介绍 在JSP页面中即可书写html,也可以书写Java代码,导致页面混乱,维护,修改,升级难度加大,于是国际上不同的公司在实际应用中,根据页面的需求将Java代 ...

  7. 如何入门 Python 爬虫?

    作者:谢科   来源:知乎链接:https://www.zhihu.com/question/20899988/answer/24923424 著作权归作者所有.商业转载请联系作者获得授权,非商业转载 ...

  8. 11个让你吃惊的linux命令

    我已经用了十年的Linux了,通过今天这篇文章我将向大家展示一系列的命令.工具和技巧,我希望一开始就有人告诉我这些,而不是曾在我成长道路上绊住我. AD: 我已经用了十年的Linux了,通过今天这篇文 ...

  9. 第一章:read/sysread/print/syswrite区别

    use strict; use warnings; #将读入的内容添加到原字符串后面 my $buffer='START:'; , length($buffer)); #my $byts = read ...

  10. CreateProcess中的部分参数理解

    函数原型,这里写Unicode版本 WINBASEAPIBOOLWINAPICreateProcessW( _In_opt_ LPCWSTR lpApplicationName, //可执行文件名字 ...