友盟移动开发平台.NET版本SDK
由于项目需要给安卓、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的更多相关文章
- 友盟分享各平台URL设置
首先,想要进项友盟分享,需要到各平台去申请ID和KEY 比如想进行微信分享,就到微信开发者平台去创建应用,拿到对应的id和appScreat,然后进行设置: 参考资料
- 商业创新不能等?用友低代码开发平台YonBuilder为您加速!
随着云计算.人工智能.物联网.大数据.5G等新一代技术的快速发展,越来越多的企业希望借助技术的力量加速数智化转型,期许通过更加敏捷和强大的应用系统推动企业的商业创新速度.但传统软件开发周期长.开发成本 ...
- 用友低代码开发平台YonBuilder首次亮相DevRun开发者沙龙
2020年的今天,没有人会再质疑企业上云的必要性与价值所在.从高科技行业到传统领域,大大小小的企业都希望走在变革道路前列,通过企业云加快业务数字化转型,更好地维护和管理企业数据. 然而,大多数企业都很 ...
- UmengAppDemo【友盟统计SDK集成以及多渠道打包配置,基于V7.5.3版本】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 这里只是记录下集成友盟统计SDK以及简单配置多渠道打包的步骤.所以1.该Demo不能运行:2.配置多渠道打包只是一种简单的写法,具体 ...
- 友盟推送 .NET (C#) 服务端 SDK rest api 调用库
友盟推送 .NET SDK rest api 介绍 该版本是基于友盟推送2.3版本封装的,网上查询了下发现没有.NET版本的调用库,官方也没有封装.NET的版本,只有python.java.php版本 ...
- iOS开发-友盟分享(3)
iOS 友盟分享 这个主要是提到如何通过友盟去自定义分享的步骤: 一.肯定要去友盟官网下载最新的SDK包,然后将SDK导入到你的工程文件夹里面去: 二.注册友盟账号,将你的APP添加到你的账号里面然后 ...
- android开发系列之友盟统计集成
相比大家都遇到这种情况,当我们的app上线之后,我们想要实时的跟踪了解到app里面的bug情况.新增用户情况.用户相关的行为属性情况等.但是如果自己在app里面去开发集成这些功能,一方面开发工作量还挺 ...
- Android中用友盟实现QQ的第三方登录
//首先应该去友盟的官网注册你的账号,创建一个应用,获得它的APPkey,也可以用它的API上的appkey,下载SDK,下面根据API文档一步步实现就行了. //下面是友盟的APi文档 1. 产品 ...
- Android 友盟分享详细集成过程及所遇问题解决
最近项目需要针对微信.朋友圈.QQ.QQ空间集成友盟分享的功能,说实话,我也是第一次做,期间碰到过很多问题,这篇随笔就来写一下我是怎么集成友盟分享的,还有碰到哪些问题,都是怎样解决的! 其实集成友盟并 ...
随机推荐
- Codeforces Round #342 (Div. 2) A
A. Guest From the Past time limit per test 1 second memory limit per test 256 megabytes input standa ...
- jedis在线文档网址
jedis在线文档网址:http://tool.oschina.net/apidocs/apidoc?api=jedis-2.1.0
- 【设计模式】 模式PK:命令模式VS策略模式
1.概述 命令模式和策略模式的类图确实很相似,只是命令模式多了一个接收者(Receiver)角色.它们虽然同为行为类模式,但是两者的区别还是很明显的.策略模式的意图是封装算法,它认为“算法”已经是一个 ...
- 「模板」「讲解」Treap名次树
Treap实现名次树 前言 学平衡树的过程可以说是相当艰难.浏览Blog的过程中看到大量指针版平衡树,不擅长指针操作的我已经接近崩溃.于是,我想着一定要写一篇非指针实现的Treap的Blog. 具体如 ...
- cookie与session的区别与应用
通常我们所说的浏览器自动保存密码,下次不用登陆,提示一次就不再出现的内容,大部分通过cookie或者session来实现的. cookie的概念 cookie是浏览器(User Agent)访问一些网 ...
- js数组的误解
js数组实际是个残废货,没有关联数组这一说,要实现真正意义上的关联数组只能用对象,那你肯定不服气了,说怎么没有关联数组,我来给你写一个: var arr = []; arr['a'] = 1; arr ...
- 超详细的Java面试题总结(二)之Java基础知识篇
多线程和Java虚拟机 创建线程有几种不同的方式?你喜欢哪一种?为什么? 继承Thread类 实现Runnable接口 应用程序可以使用Executor框架来创建线程池 实现Callable接口. 我 ...
- 微信小程序提示框
一.wx.showToast 如上图所示,showToast会显示一个弹窗,在指定的时间之后消失.中间的图标默认只有加载中和成功两种,也可以用image参数自定义图标 wx.showToast({ t ...
- 转一篇sublime必备的一些插件
Package Control 功能:安装包管理 简介:sublime插件控制台,提供添加.删除.禁用.查找插件等功能 使用:https://sublime.wbond.net/installatio ...
- 网络应用框架Netty快速入门
一 初遇Netty Netty是什么? Netty 是一个提供 asynchronous event-driven (异步事件驱动)的网络应用框架,是一个用以快速开发高性能.可扩展协议的服务器和客户端 ...