展开
折叠
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
{
/// <summary>
/// 标题
/// </summary>
public string Title;
/// <summary>
/// 描述,描写;类型;说明书
/// </summary>
public string Desc;
/// <summary>
/// 发送时间
/// </summary>
public string Key = PushUtils.GetCurrentTimeMillis(DateTime.Now).ToString();
/// <summary>
/// 自定义内容
/// </summary>
public Dictionary<string, object> CustomContent;
}
public enum DeviceType : uint
{
/// <summary>
/// 浏览器设备
/// </summary>
Browser = 1,
/// <summary>
/// PC设备
/// </summary>
PC = 2,
/// <summary>
/// Andriod设备
/// </summary>
Andriod = 3,
iOS = 4,
WindowsPhone = 5,
}
/// <summary>
/// 百度云推送类
/// </summary>
public static class BaiDuPushHelper
{
/// <summary>
/// IOS状态 1 开发状态 2 生产状态
/// </summary>
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 List<QueryBindItem> GetBindList(string user_id)
{
var req = new QueryBindlistRequest();
req.UserId = user_id;
var res = PushClient.Execute(req);
return res.IsError ? new List<QueryBindItem>() : res.ResponseParams.Binds;
}
#region tag
public static List<Tag> GetTags(string name = "")
{
var req = new FetchTagRequest() { Name = name };
var res = PushClient.Execute(req);
return res.IsError ? new List<Tag>() : 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<Tag> QueryUserTags(string user_id)
{
var req = new QueryUserTagsRequest() { UserId = user_id };
var res = PushClient.Execute(req);
return res.IsError ? new List<Tag>() : 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;
}
/// <summary>
/// 推送消息
/// </summary>
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;
}
/// <summary>
/// 推送通知
/// </summary>
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
/// <summary>
/// 推送通知给所有人
/// </summary>
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;
}
/// <summary>
/// 推送消息给所有人
/// </summary>
//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, Dictionary<string, object> dic)
{
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<string, object>();
if (!string.IsNullOrEmpty(title)) dic["title"] = title;
dic["description"] = desc;
return PushJsonParser.Serialize(dic);
}
}
#endregion
}
}
调用方法
/// <summary>
/// 推送通知给所有人
/// </summary>
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;
}
参考:https://baidupush.codeplex.com/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具