展开
折叠using Baidu.Push.Api;using Baidu.Push.Api.Domain;using Baidu.Push.Api.Parser;using Baidu.Push.Api.Request;using Baidu.Push.Api.Util;using System;using System.Collections.Generic;using System.Configuration;using System.Linq;using System.Text;namespace RB.Common{ public class PushMessage { ////// 标题 /// public string Title; ////// 描述,描写;类型;说明书 /// public string Desc; ////// 发送时间 /// public string Key = PushUtils.GetCurrentTimeMillis(DateTime.Now).ToString(); ////// 自定义内容 /// public DictionaryCustomContent; } public enum DeviceType : uint { /// /// 浏览器设备 /// Browser = 1, ////// PC设备 /// PC = 2, ////// Andriod设备 /// Andriod = 3, iOS = 4, WindowsPhone = 5, } ////// 百度云推送类 /// public static class BaiDuPushHelper { ////// IOS状态 1 开发状态 2 生产状态 /// private static readonly string _apiKey = ConfigurationManager.AppSettings["ApiKey"]; private static readonly string _secretKey = ConfigurationManager.AppSettings["SecretKey"]; //DeployStatus:IOS部署状态 private static uint DEPLOY_STATUS = uint.Parse(ConfigurationManager.AppSettings["DeployStatus"]); public static readonly IPushClient PushClient; static BaiDuPushHelper() { PushClient = new DefaultPushClient(_apiKey, _secretKey); } public static ListGetBindList(string user_id) { var req = new QueryBindlistRequest(); req.UserId = user_id; var res = PushClient.Execute(req); return res.IsError ? new List () : res.ResponseParams.Binds; } #region tag public static List GetTags(string name = "") { var req = new FetchTagRequest() { Name = name }; var res = PushClient.Execute(req); return res.IsError ? new List () : res.ResponseParams.Tags; } public static bool SetTag(string tag, string user_id = "") { var req = new SetTagRequest(); req.Tag = tag; req.UserId = user_id; var res = PushClient.Execute(req); return !res.IsError; } public static bool DeleteTag(string tag, string user_id = "") { var req = new DeleteTagRequest(); req.Tag = tag; req.UserId = user_id; var res = PushClient.Execute(req); return !res.IsError; } public static List QueryUserTags(string user_id) { var req = new QueryUserTagsRequest() { UserId = user_id }; var res = PushClient.Execute(req); return res.IsError ? new List () : res.ResponseParams.Tags; } #endregion #region push msg by tag public static int PushNotificationByTag(DeviceType deviceType, PushMessage msg, string tag) { var req = new PushMsgRequest(); req.Tag = tag; req.DeviceType = (uint)deviceType; req.PushType = 2; req.MessageType = 1; req.MsgKeys = msg.Key; req.Messages = GetNotification(deviceType, msg); if (deviceType == DeviceType.iOS) req.DeployStatus = DEPLOY_STATUS; //IOS需指定部署状态 var res = PushClient.Execute(req); return res.IsError ? 0 : res.ResponseParams.SuccessAmount; } public static int PushMessageByTag(DeviceType deviceType, PushMessage msg, string tag) { //IOS仅支持通知 if (deviceType == DeviceType.iOS) return PushNotificationByTag(deviceType, msg, tag); var req = new PushMsgRequest(); req.Tag = tag; req.DeviceType = (uint)deviceType; req.PushType = 2; req.MessageType = 0; req.MsgKeys = msg.Key; req.Messages = msg.Desc; var res = PushClient.Execute(req); return res.IsError ? 0 : res.ResponseParams.SuccessAmount; } #endregion #region push msg by user public static int PushMessage(string user_id, string msg, string key) { var user = GetBindList(user_id).FirstOrDefault(); return user != null ? PushMessage(user, msg, key) : 0; } /// /// 推送消息 /// public static int PushMessage(QueryBindItem user, string msg, string key) { //IOS仅支持通知 if (user.DeviceType == (uint)DeviceType.iOS) return PushNotification(user, msg, key); var req = new PushMsgRequest(); req.UserId = user.UserId; req.DeviceType = user.DeviceType; req.PushType = 1; req.MsgKeys = key; req.Messages = msg; if (user.DeviceType == (uint)DeviceType.Andriod) { //Android可以指定某一个特定client req.ChannelId = user.ChannelId; } var res = PushClient.Execute(req); return res.IsError ? 0 : res.ResponseParams.SuccessAmount; } public static int PushNotification(string user_id, PushMessage msg) { var user = GetBindList(user_id).FirstOrDefault(); return user != null ? PushNotification(user, msg) : 0; } ////// 推送通知 /// public static int PushNotification(QueryBindItem user, PushMessage msg) { string message = GetNotification((DeviceType)user.DeviceType, msg); return PushNotification(user, message, msg.Key); } private static int PushNotification(QueryBindItem user, string msg, string key) { var req = new PushMsgRequest(); req.UserId = user.UserId; req.DeviceType = user.DeviceType; req.PushType = 1; req.MessageType = 1; req.Messages = msg; req.MsgKeys = key; if (user.DeviceType == (uint)DeviceType.Andriod) { //Android可以指定某一个特定client req.ChannelId = user.ChannelId; } else if (user.DeviceType == (uint)DeviceType.iOS) { //IOS需指定部署状态 req.DeployStatus = DEPLOY_STATUS; } var res = PushClient.Execute(req); return res.IsError ? 0 : res.ResponseParams.SuccessAmount; } #endregion #region push all ////// 推送通知给所有人 /// public static JsonResponse PushAllNotification(DeviceType deviceType, PushMessage msg) { var req = new PushMsgRequest(); req.DeviceType = (uint)deviceType; req.PushType = 3; req.MessageType = 1; req.MsgKeys = msg.Key; req.Messages = GetNotification(deviceType, msg); if (deviceType == DeviceType.iOS) req.DeployStatus = DEPLOY_STATUS; //IOS需指定部署状态 var res = PushClient.Execute(req); JsonResponse jsonResponse = new JsonResponse(); if (res.IsError == false) { jsonResponse.status = 0; jsonResponse.message = "成功"; jsonResponse.data = res.Body; } else { jsonResponse.status = 1; jsonResponse.message = "失败"; jsonResponse.data = res.Body; } return jsonResponse; //return res.IsError ? 0 : res.ResponseParams.SuccessAmount; } ////// 推送消息给所有人 /// //public static int PushAllMessage(DeviceType deviceType, PushMessage msg) //{ // //IOS仅支持通知 // if (deviceType == DeviceType.iOS) return PushAllNotification(deviceType, msg); // var req = new PushMsgRequest(); // req.DeviceType = (uint)deviceType; // req.PushType = 3; // req.MessageType = 0; // req.MsgKeys = msg.Key; // req.Messages = msg.Desc; // var res = PushClient.Execute(req); // return res.IsError ? 0 : res.ResponseParams.SuccessAmount; //} #endregion #region GetNotificationString internal static string GetNotification(DeviceType deviceType, PushMessage msg) { return GetNotification(deviceType, msg.Title, msg.Desc, msg.CustomContent); } internal static string GetNotification(DeviceType deviceType, string title, string desc, Dictionarydic) { if (deviceType == DeviceType.Andriod) { var android = new AndroidNotification(); android.Title = title; android.Description = desc; android.NotificationBuilderId = 0; android.NotificationBasicStyle = 0x07; android.CustomContent = dic; return PushJsonParser.Serialize(android); } else { if (dic == null) dic = new Dictionary (); if (!string.IsNullOrEmpty(title)) dic["title"] = title; dic["description"] = desc; return PushJsonParser.Serialize(dic); } } #endregion }}
调用方法
////// 推送通知给所有人 /// public static JsonResponse PushAllNotification(DeviceType deviceType, PushMessage msg) { var req = new PushMsgRequest(); req.DeviceType = (uint)deviceType; req.PushType = 3; req.MessageType = 1; req.MsgKeys = msg.Key; req.Messages = GetNotification(deviceType, msg); if (deviceType == DeviceType.iOS) req.DeployStatus = DEPLOY_STATUS; //IOS需指定部署状态 var res = PushClient.Execute(req); JsonResponse jsonResponse = new JsonResponse(); if (res.IsError == false) { jsonResponse.status = 0; jsonResponse.message = "成功"; jsonResponse.data = res.Body; } else { jsonResponse.status = 1; jsonResponse.message = "失败"; jsonResponse.data = res.Body; } return jsonResponse; //return res.IsError ? 0 : res.ResponseParams.SuccessAmount; }
参考: