yl
2025-11-17 8d46fbb3dcdc7831757800ae78f5aabaac1d4195
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using Jiguang.JPush;
using Jiguang.JPush.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VueWebCoreApi.Models;
 
namespace VueWebCoreApi.Tools
{
    public class JPushManage
    {
        public static ToMessage mes = new ToMessage(); //定义全局返回信息对象
        private static JPushClient client = new JPushClient("502772cf67ff42b7b55c8a3f", "55f2881b7afde3e08e30da58"); //"自己的key", "自己的MasterSecret"
        //List<string> registration_id, string title, string content
 
        /// <summary>
        /// 推送到个人,每次推送最多1000人    
        /// </summary>
        /// <param name="username">请求用户</param>
        /// <param name="wkshpcode">车间编码</param>
        /// <param name="eqpcode">设备编码</param>
        /// <param name="json">提交的数据</param>
        public static ToMessage ExecutePushByUserId(string username, string wkshpcode, string eqpcode, List<AnDnDate> json)
        {
            try
            {
                for (int i = 0; i < json.Count; i++)
                {
                    //List<string> registration_id = new List<string>();
                    List<string> alias = new List<string>();
                    string title = json[i].name.ToString();   //标题
                    string content = "来自用户:" + username.ToString() + "内容:车间编号:【" + wkshpcode + "】,对应设备编号:【" + eqpcode + "】,时间" + DateTime.Now.ToString("yyyy/MM/ddhh:mm:ss") + "的消息通知!"; //内容
                    //registration_id = json[i].children.Select(x => x.rid).Distinct().ToList(); //获取App集成SDK生成的Rid[ "111", "22" ] 去除重复
                    alias = json[i].children.Select(x => x.alias).Distinct().ToList(); //获取别名[ "111", "22" ] 去除重复
                    //audience : { "registration_id" : [ "111", "22" ]}  设备标识。一次推送最多 1000 个。
                    var pushPayload = new PushPayload()
                    {
                        Platform = new List<string> { "android" },  //推送平台设置(必填)
                        //Audience = "{\"registration_id\" :" + Newtonsoft.Json.JsonConvert.SerializeObject(registration_id) + "}", //推送设备指定(必填)
                        Audience = new { alias = alias.ToArray()}, //推送人员指定(必填)
                        Notification = new Notification
                        {
                            Android = new Android
                            {
                                Alert = content,
                                Title = title
                            },
                        },
                        Options = new Options
                        {
                            TimeToLive = 60//单位秒,最大值10天
                        }
                    };
                   
                    var response = client.SendPush(pushPayload);
                    mes.code = response.StatusCode.ToString();
                    mes.message = response.Content;
                    mes.data = null;
                }
            }
            catch (Exception e)
            {
                mes.code = "300";
                mes.message = e.Message;
                mes.data = null;
            }
            return mes;
        }
    }
}