由于项目需要给安卓、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. linux配置虚拟机网络环境(老师要求的host-only)

    我这个人就是懒,这TMD是全天下最坑爹的缺点了,当然爆粗口也是缺点,让我发泄一下吧.T^T 从n久之前,开了hadoop课的一天,我就想着要配置好,结果两次课连眼镜都忘了带,可想而知是什么陪我度过了那 ...

  2. cuda环境下安装opencv出现nvcc warning : The 'compute_11'

    警告打印: nvcc warning : The 'compute_11', 'compute_12', 'compute_13', 'sm_11', 'sm_12', and 'sm_13' arc ...

  3. 《python核心编程》--读书笔记 第21章 数据库编程

    准备:今天拿笔记本装了mysql,这样就能在不同地方用其他电脑远程访问同一个数据库了. python安装MySQLdb模块:http://www.codegood.com/downloads. 21. ...

  4. python递归读取目录列表

    import os def listdirs(base): for line in os.listdir(base): fullpath = os.path.join(base,line) if os ...

  5. ACM3371超时问题

    这的确也是个大坑: 其实在这是到很简单的最小生成树的题目,但是数据量却很大: 用G++提交会超时,用C++不会超时,而且速度超快: 又长见识了.可惜长得不是做题的能力,而是知道它到底有多坑. #inc ...

  6. jq 正则

    if(_each_this_type_name == 'post_num'){ var patrn = /^[a-zA-Z0-9]{3,12}$/; if(!patrn.test(_each_this ...

  7. maven中jar包的maven地址查询

    在网站 https://mvnrepository.com/ 中查找.

  8. ZooKeeper屏障和队列的指南(七)

    引言 在这个指南中,使用展示了使用ZooKeeper实现的屏障和生产-消费队列.我们分别称这些类为Barrier和Queue.这些例子假定你至少有一个运行的ZooKeeper服务. 两个原语都使用下面 ...

  9. Small Multiple

    题目描述 Find the smallest possible sum of the digits in the decimal notation of a positive multiple of ...

  10. 2015/8/28 Python基础(2):对象

    Python用对象模型来存储数据.构造任何类型的值都是一个对象.Python对象都有是三个特性:身份,类型和值 身份是每个对象的唯一身份标识.任何对象都可以用内建函数id()来得到身份.如: > ...