代码改变世界

友盟移动开发平台.NET版本SDK

2015-11-15 16:55  C#与.NET探索者  阅读(848)  评论(0编辑  收藏  举报

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

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

1.接口声明

 1  public interface IMsgService
 2     {
 3         /// <summary>
 4         /// 单播
 5         /// </summary>
 6         /// <param name="msg"></param>
 7         MsgDTO UniCast(string msg, string deviceId);
 8 
 9         /// <summary>
10         /// 组播
11         /// </summary>
12         /// <param name="msg"></param>
13         MsgDTO GroupCast(string msg, int operatorId);
14 
15         /// <summary>
16         /// 广播
17         /// </summary>
18         /// <param name="msg"></param>
19         MsgDTO BroadCast(string msg);
20     }
View Code

 

2.安卓服务实现

  1 public class AndroidMsgService : MsgServiceBase, IMsgService
  2     {
  3 
  4         protected static readonly string App_Master_Secret = AppSettingHelper.DomainSetting.GetAttribute<string>("Api/UmengMsgList/Android", "appmastersecret");
  5 
  6         protected static readonly string App_Key = AppSettingHelper.DomainSetting.GetAttribute<string>("Api/UmengMsgList/Android", "appkey");
  7 
  8 
  9         private AndroidMsgService()
 10         {
 11 
 12         }
 13 
 14         public static readonly AndroidMsgService Instance =new AndroidMsgService();
 15 
 16         protected override string GetSign(string post_body)
 17         {
 18             return (String.Concat("POST", SendUrl, post_body, App_Master_Secret)).MD5().ToLower();
 19         }
 20 
 21         public MsgDTO BroadCast(string msg)
 22         {
 23             var sendData = new
 24             {
 25                 appkey = App_Key,
 26                 timestamp = GetTimeStamp,
 27                 type = "broadcast",
 28                 payload = new
 29                 {
 30                     display_type = "notification",// 通知,notification
 31                     body = new
 32                     {
 33                         //custom = msg
 34                         ticker =msg,
 35                         title = msg,
 36                         text = msg,
 37                         after_open = "go_custom",
 38                         custom="0"
 39                     },
 40                     extra = new
 41                     {
 42                         key1 = "key1",
 43                         key2 = "key2"
 44                     }
 45                 },
 46                 policy = new
 47                 {
 48                     // start_time = "2013-10-29 12:00:00", //定时发送
 49                     expire_time = DateTime.Now.AddDays(3).ToString("yyyy-MM-dd HH:mm:ss")
 50                 },
 51                 production_mode = ProductMode,
 52                 description = "测试广播通知-android"
 53             };
 54 
 55             //var result = this.GetResult(sendData, GetSign(sendData.ToJson()));
 56             //return result;
 57 
 58             var result = this.GetResult(sendData);
 59             return result;
 60         }
 61 
 62         public MsgDTO GroupCast(string msg, int operatorId)
 63         {
 64             var sendData = new
 65             {
 66                 appkey = App_Key,
 67                 timestamp = GetTimeStamp,
 68                 type = "groupcast",
 69                 filter = new
 70                 {
 71                     where = new
 72                     {
 73                         and = new[]
 74                         {
 75                             new {tag=operatorId.ToString()}
 76                         }
 77                     }
 78                 },
 79                 payload = new
 80                 {
 81                     display_type = "notification", // 通知,notification
 82                     body = new
 83                     {
 84                         //custom = msg
 85                         ticker = msg,
 86                         title = msg,
 87                         text = msg,
 88                         after_open = "go_custom",
 89                         custom = "0"
 90                     },
 91                     extra = new
 92                     {
 93                         key1 = "key1",
 94                         key2 = "key2"
 95                     }
 96                 },
 97                 policy = new
 98                 {
 99                     expire_time = DateTime.Now.AddDays(3).ToString("yyyy-MM-dd HH:mm:ss")
100                 },
101                 production_mode = ProductMode,
102                 description = "测试组播通知-Android"
103             };
104             var result = base.GetResult(sendData);
105             return result;
106         }
107 
108         public MsgDTO UniCast(string msg, string deviceId)
109         {
110 
111             var sendData = new
112             {
113                 appkey = App_Key,
114                 timestamp = GetTimeStamp,
115                 type = "unicast",
116                 device_tokens = deviceId,
117                 payload = new
118                 {
119                     display_type = "notification", // 消息,message
120                     body = new
121                     {
122                         //custom = msg
123                         ticker = msg,
124                         title = msg,
125                         text = msg,
126                         after_open = "go_custom",
127                         custom = "0"
128                     },
129                     extra = new
130                     {
131                         key1= "key1",
132                         key2= "key2"
133                     }
134                 },
135                 production_mode= ProductMode,
136                 description = "测试单播"
137             };
138             var result = base.GetResult(sendData);
139             return result;
140 
141         }
142 
143 
144     }
View Code

3.IOS服务实现

  1 public class IosMsgService : MsgServiceBase, IMsgService
  2     {
  3         protected static readonly string App_Master_Secret = AppSettingHelper.DomainSetting.GetAttribute<string>("Api/UmengMsgList/IOS", "appmastersecret");
  4 
  5         protected static readonly string App_Key =
  6             AppSettingHelper.DomainSetting.GetAttribute<string>("Api/UmengMsgList/IOS", "appkey");
  7 
  8         public static readonly IosMsgService Instance = new IosMsgService();
  9 
 10         private IosMsgService()
 11         {
 12             
 13         }
 14 
 15         protected override string GetSign(string post_body)
 16         {
 17             return (String.Concat("POST", SendUrl, post_body, App_Master_Secret)).MD5().ToLower();
 18         }
 19 
 20         public MsgDTO BroadCast(string msg)
 21         {
 22             var sendData = new
 23             {
 24                 appkey = App_Key,
 25                 timestamp = GetTimeStamp,
 26                 type = "broadcast",
 27                 payload = new
 28                 {
 29                     aps = new { alert = msg } // 苹果必填字段
 30                 },
 31                 policy = new
 32                 {
 33                     // start_time = "2013-10-29 12:00:00", //定时发送
 34                     expire_time = DateTime.Now.AddDays(3).ToString("yyyy-MM-dd HH:mm:ss")
 35                 },
 36                 production_mode = ProductMode,
 37                 description = "测试广播通知-iOS"
 38             };
 39 
 40             //var result = RequestHelper.HttpUtil<MsgDTO>(String.Concat(SendUrl, $"?sign={GetSign(sendData.ToJson())}"), sendData);
 41             //return result;
 42 
 43             var result = this.GetResult(sendData);
 44             return result;
 45         }
 46 
 47         public MsgDTO GroupCast(string msg,int operatorId)
 48         {
 49             var sendData = new
 50             {
 51                 appkey = App_Key,
 52                 timestamp = GetTimeStamp,
 53                 type = "groupcast",
 54                 filter = new
 55                 {
 56                     where = new
 57                     {
 58                         and = new []
 59                         {
 60                             new {tag=operatorId.ToString()}
 61                         }
 62                     }
 63                 },
 64                 payload = new
 65                 {
 66                     aps = new { alert = msg } // 苹果必填字段
 67                 },
 68                 policy = new
 69                 {
 70                     expire_time = DateTime.Now.AddDays(3).ToString("yyyy-MM-dd HH:mm:ss")
 71                 },
 72                 production_mode = ProductMode,
 73                 description = "测试组播通知-IOS"
 74             };
 75             var result = base.GetResult(sendData);
 76             return result;
 77             //var result = RequestHelper.HttpUtil<MsgDTO>(String.Concat(SendUrl, $"?sign={GetSign(sendData.ToJson())}"), sendData);
 78             //return result;
 79         }
 80 
 81         public MsgDTO UniCast(string msg, string deviceId)
 82         {
 83             var sendData = new
 84             {
 85                 appkey = App_Key,
 86                 timestamp = GetTimeStamp,
 87                 type = "unicast",
 88                 device_tokens = deviceId,
 89                 payload = new
 90                 {
 91                     aps = new
 92                     {
 93                         alert = msg
 94                     } // 苹果必填字段
 95                 },
 96                 policy = new
 97                 {
 98                     expire_time = DateTime.Now.AddDays(3).ToString("yyyy-MM-dd HH:mm:ss")
 99                 },
100                 production_mode= ProductMode,
101                 description = "测试单播消息"
102             };
103             var result = base.GetResult(sendData);
104             return result;
105             //var result = RequestHelper.HttpUtil<MsgDTO>(String.Concat(SendUrl, $"?sign={GetSign(sendData.ToJson())}"), sendData);
106             //return result;
107         }
108     }
View Code

4.MsgServiceBase服务基类

 1 public abstract class MsgServiceBase
 2     {
 3         protected static readonly string SendUrl = AppSettingHelper.DomainSetting.GetValue("Api/UmengMsgList/RequestUrl");
 4 
 5         protected static readonly string ProductMode = AppSettingHelper.DomainSetting.GetAttribute<string>("Api/UmengMsgList", "productmode");
 6 
 7         protected abstract string GetSign(string sendData);
 8 
 9         protected string GetTimeStamp => (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds.ToString(
10             "#");
11 
12         protected MsgDTO GetResult(dynamic sendData)
13         {
14             
15             var result = RequestHelper.HttpUtil<MsgDTO>(String.Concat(SendUrl, $"?sign={this.GetSign(JsonConvert.SerializeObject(sendData))}"), sendData);
16             return result;
17         }
18     }
View Code

5.HttpUtil

 1 public static T HttpUtil<T>(string url, dynamic data) where T :new()
 2         {
 3             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
 4             request.ContentType = "application/x-www-form-urlencoded"; //设置HTTP头
 5             request.Method = "POST";
 6 
 7             //byte[] postdata = Encoding.UTF8.GetBytes(data);
 8             byte[] postdata = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data));
 9             request.ContentLength = postdata.Length;
10 
11             Stream newStream = request.GetRequestStream();
12             newStream.Write(postdata, 0, postdata.Length);
13             newStream.Close();
14             
15 
16 
17             HttpWebResponse myResponse = (HttpWebResponse)request.GetResponse();
18             StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
19            return  (reader.ReadToEnd()).FromJson<T>();//得到结果
20         }
View Code

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