.net实现手机推送和界面推送

来源:互联网 发布:nginx 自定义变量 编辑:程序博客网 时间:2024/06/09 19:00

最近做项目用到手机实时推送和界面实时推送两种功能,分别用了Fleck和极光推送。

Fleck:

Fleck是 C# 实现的 WebSocket 服务器。比WebSoket更容易配置,开发更为简单。
官方地址:https://github.com/statianzo/Fleck
客户端代码:

var noSupportMessage = "您的浏览器不支持消息提醒功能!";var support = "MozWebSocket" in window ? 'MozWebSocket' : ('WebSocket' in window ? 'WebSocket' : null);if (support == null) {    alert(noSupportMessage);}else {    var wsImpl = window.WebSocket || window.MozWebSocket;    var userid = '@LeaRun.Utilities.ManageProvider.Provider.Current().UserId';    var ip = '@LeaRun.Business.MessageServe.ServerIP';    // create a new websocket and connect    window.ws = new wsImpl(ip + userid);    ws.onmessage = function (evt) {    //confirmDialog(‘Title’, evt.data);    };         }

服务器端代码:

public static class MessageServe{    public static string ServerIP {        get        {            return ConfigurationManager.AppSettings["MessageServer"];        }    }    public static List<IWebSocketConnection> allSockets= new List<IWebSocketConnection>();    public static WebSocketServer server = new WebSocketServer(ServerIP);    static MessageServe()    {        try        {            FleckLog.Level = LogLevel.Debug;            server.Start(socket =>            {                socket.OnOpen = () =>                {                                     allSockets.Add(socket);                };                socket.OnClose = () =>                {                    allSockets.Remove(socket);                };                socket.OnMessage = message =>                {                    allSockets.ToList().ForEach(s => s.Send(message));                };            });        }                catch        {        }    }    /// <summary>    /// 页面消息提醒    /// </summary>    /// <param name="ListUerid">UserID列表</param>    /// <param name="Message">发送内容</param>    public static void SendMessage(List<string> ListUerid, string Message)    {        MessageServe.allSockets.ToList().ForEach(s =>        {            if (ListUerid.Contains(s.ConnectionInfo.Path.Substring(1)))                s.Send(Message);        });    }}

极光推送:

使得开发者可以即时地向其应用程序的用户推送通知或者消息,与用户保持互动,从而有效地提高留存率,提升用户体验。平台提供整合了Android推送、iOS推送的统一推送服务。
官方网址:
https://www.jpush.cn/common/products
服务端代码:(包含网页和手机推送)

   /// <summary>   /// 发送手机推送信息   /// </summary>   /// <param name="Users">用户列表</param>   /// <param name="Title">标题</param>   /// <param name="Alert">提醒内容</param>   /// <param name="MessageID">消息ID</param>   /// <param name="Type">消息类型</param>   /// <returns>异常信息,空字符串为发送成功</returns>   public static string SendMobileMessage(List<string> Users, String Title, String Alert, String MessageID, string Type, String MessageState)   {       try       {           MessageServe.allSockets.ToList().ForEach(s =>               {                   if (Users.Contains(s.ConnectionInfo.Path.Replace('-', '_').Substring(1)))                       s.Send(Title + ":<br/>" + Alert);               });           var appKey = ConfigurationManager.AppSettings["appKey"];           var masterSecret = ConfigurationManager.AppSettings["masterSecret"];           Dictionary<string, string> customizedValues = new Dictionary<string, string>();           customizedValues.Add("MessageID", MessageID);           customizedValues.Add("Type", Type);           customizedValues.Add("MessageState", MessageState);           JPushClientV3 client = new JPushClientV3(appKey, masterSecret);           Audience audience = new Audience();           audience.Add(PushTypeV3.ByAlias, new List<string>(Users.ToArray()));           Notification notification = new Notification           {               AndroidNotification = new AndroidNotificationParameters               {                   Title = Title,                   Alert = Alert,                   CustomizedValues = customizedValues               },           };           var response = client.SendPushMessage(new PushMessageRequestV3           {               Audience = audience,               Platform = PushPlatform.Android,               IsTestEnvironment = true,               AppMessage = new AppMessage               {                   Content = "",                   CustomizedValue = customizedValues               },               Notification = notification           });           if (response.ResponseCode.ToString() == "Succeed")               return "";           else               return response.ResponseCode.ToString();       }       catch (Exception ex)       {           return ex.Message;       }   }

个推:

最后提到个推,是因为HTML5+使用的是个推进行消息推送,使用HTML5手机开发框架,最近项目使用到Hbuilder国产框架,其中可以使用个推插件,推送使用和极光推送类似,详见其官网:
http://docs.getui.com/

0 0
原创粉丝点击