1. 程式人生 > >.net實現手機推送和介面推送

.net實現手機推送和介面推送

最近做專案用到手機實時推送和介面實時推送兩種功能,分別用了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/