123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using Aliyun.Acs.Push.Model.V20160801;
- using Aliyun.Acs.Push.Transform.V20160801;
- using Aliyun.Acs.Core;
- using Aliyun.Acs.Core.Exceptions;
- using Aliyun.Acs.Core.Profile;
- //using MySystem.BsModels;
- using Library;
- namespace MySystem
- {
- public class AliyunPush
- {
- private string AccessKeyID = "LTAImYRe25JQ5nr8";
- private string AccessKeySecret = "znOWxVGLnKl8KHlPSIKruYC3C1HYIy";
- public void PushMessageToAndroid()
- {
- IClientProfile clientProfile = DefaultProfile.GetProfile("cn-hangzhou", AccessKeyID, AccessKeySecret);
- DefaultAcsClient client = new DefaultAcsClient(clientProfile);
- PushMessageToAndroidRequest request = new PushMessageToAndroidRequest();
- request.AppKey = 24566887;
- request.Target = "ALL";//推送目标: DEVICE:按设备推送 ALIAS : 按别名推送 ACCOUNT:按帐号推送 TAG:按标签推送; ALL: 广播推送
- request.TargetValue = "All";////根据Target来设定,如Target=DEVICE, 则对应的值为 设备id1,设备id2. 多个值使用逗号分隔.(帐号与设备一次最多100)
- request.Title = "android title";
- request.Body = "android body";
- try
- {
- PushMessageToAndroidResponse response = client.GetAcsResponse(request);
- Console.WriteLine("RequestId:" + response.RequestId);
- Console.WriteLine("ResponseId:" + response.MessageId);
- Console.ReadLine();
- }
- catch (ServerException e)
- {
- Console.WriteLine(e.ErrorCode);
- Console.WriteLine(e.ErrorMessage);
- Console.ReadLine();
- }
- catch (ClientException e)
- {
- Console.WriteLine(e.ErrorCode);
- Console.WriteLine(e.ErrorMessage);
- Console.ReadLine();
- }
- }
- public void PushMessageToiOS()
- {
- IClientProfile clientProfile = DefaultProfile.GetProfile("cn-hangzhou", AccessKeyID, AccessKeySecret);
- DefaultAcsClient client = new DefaultAcsClient(clientProfile);
- PushMessageToiOSRequest request = new PushMessageToiOSRequest();
- request.AppKey = 24566887;
- request.Target = "ALL";//推送目标: DEVICE:按设备推送 ALIAS : 按别名推送 ACCOUNT:按帐号推送 TAG:按标签推送; ALL: 广播推送
- request.TargetValue = "All";//根据Target来设定,如Target=DEVICE, 则对应的值为 设备id1,设备id2. 多个值使用逗号分隔.(帐号与设备一次最多100个)
- request.Title = "iOS Title";
- request.Body = "iOS Body";
- try
- {
- PushMessageToiOSResponse response = client.GetAcsResponse(request);
- Console.WriteLine("RequestId:" + response.RequestId);
- Console.WriteLine("ResponseId:" + response.MessageId);
- Console.ReadLine();
- }
- catch (ServerException e)
- {
- Console.WriteLine(e.ErrorCode);
- Console.WriteLine(e.ErrorMessage);
- Console.ReadLine();
- }
- catch (ClientException e)
- {
- Console.WriteLine(e.ErrorCode);
- Console.WriteLine(e.ErrorMessage);
- Console.ReadLine();
- }
- }
- public void PushNoticeToAndroid()
- {
- IClientProfile clientProfile = DefaultProfile.GetProfile("cn-hangzhou", AccessKeyID, AccessKeySecret);
- DefaultAcsClient client = new DefaultAcsClient(clientProfile);
- PushNoticeToAndroidRequest request = new PushNoticeToAndroidRequest();
- request.AppKey = 24566887;
- request.Target = "ALL";//推送目标: DEVICE:按设备推送 ALIAS : 按别名推送 ACCOUNT:按帐号推送 TAG:按标签推送; ALL: 广播推送
- request.TargetValue = "all";//根据Target来设定,如Target=DEVICE, 则对应的值为 设备id1,设备id2. 多个值使用逗号分隔.(帐号与设备一次最多100个)
- request.Title = "android notice title";
- request.Body = "android notice body";
- request.ExtParameters = "{\"Url\":\"http://www.yunmuit.com/\"}";
- try
- {
- PushNoticeToAndroidResponse response = client.GetAcsResponse(request);
- Console.WriteLine("RequestId:" + response.RequestId);
- Console.WriteLine("ResponseId:" + response.MessageId);
- Console.ReadLine();
- }
- catch (ServerException e)
- {
- Console.WriteLine(e.ErrorCode);
- Console.WriteLine(e.ErrorMessage);
- Console.ReadLine();
- }
- catch (ClientException e)
- {
- Console.WriteLine(e.ErrorCode);
- Console.WriteLine(e.ErrorMessage);
- Console.ReadLine();
- }
- }
- public void PushNoticeToiOS()
- {
- IClientProfile clientProfile = DefaultProfile.GetProfile("cn-hangzhou", AccessKeyID, AccessKeySecret);
- DefaultAcsClient client = new DefaultAcsClient(clientProfile);
- PushNoticeToiOSRequest request = new PushNoticeToiOSRequest();
- request.AppKey = 24566887;
- // iOS的通知是通过APNS中心来发送的,需要填写对应的环境信息. DEV:表示开发环境, PRODUCT: 表示生产环境
- request.ApnsEnv = "DEV";
- request.Target = "ALL";
- request.TargetValue = "ALL";
- request.Title = "test";
- request.Body = "testg";
- request.ExtParameters = "{\"Url\":\"http://www.yunmuit.com/\"}";
- try
- {
- PushNoticeToiOSResponse response = client.GetAcsResponse(request);
- Console.WriteLine("RequestId:" + response.RequestId);
- Console.WriteLine("ResponseId:" + response.MessageId);
- Console.ReadLine();
- }
- catch (ServerException e)
- {
- Console.WriteLine(e.ErrorCode);
- Console.WriteLine(e.ErrorMessage);
- Console.ReadLine();
- }
- catch (ClientException e)
- {
- Console.WriteLine(e.ErrorCode);
- Console.WriteLine(e.ErrorMessage);
- Console.ReadLine();
- }
- }
- public string Push(string Title, string Body, string Url, string PushType = "NOTICE", string DeviceType = "ALL", string DeviceList = "", bool Silent = false, String pushTime = "")
- {
- IClientProfile clientProfile = DefaultProfile.GetProfile("cn-hangzhou", AccessKeyID, AccessKeySecret);
- DefaultAcsClient client = new DefaultAcsClient(clientProfile);
- PushRequest request = new PushRequest();
- // 推送目标
- if (DeviceType == "ANDROID")
- {
- request.AppKey = 27532904;
- }
- else if (DeviceType == "iOS")
- {
- request.AppKey = 27536906;
- }
- if (string.IsNullOrEmpty(DeviceList))
- {
- //推送目标: DEVICE:按设备推送 ALIAS : 按别名推送 ACCOUNT:按帐号推送 TAG:按标签推送; ALL: 广播推送
- request.Target = "ALL";
- //根据Target来设定,如Target=DEVICE, 则对应的值为 设备id1,设备id2. 多个值使用逗号分隔.(帐号与设备有一次最多100个的限制)
- request.TargetValue = "All";
- }
- else
- {
- request.Target = "DEVICE";
- request.TargetValue = DeviceList;
- }
- //消息类型 MESSAGE NOTICE
- request.PushType = PushType;
- //设备类型 ANDROID iOS ALL.
- request.DeviceType = DeviceType;
- // 消息的标题
- request.Title = Title;
- // 消息的内容
- request.Body = Body;
- // 推送配置: iOS
- // iOS应用图标右上角角标
- //request.IOSBadge = 50;
- //开启静默通知
- request.IOSSilentNotification = Silent;
- // iOS通知声音
- if (Silent)
- {
- request.IOSMusic = "";
- }
- else
- {
- request.IOSMusic = "default";
- }
- //iOS10通知副标题的内容
- request.IOSSubtitle = "";
- //指定iOS10通知Category
- request.IOSNotificationCategory = "iOS10 Notification Category";
- //是否允许扩展iOS通知内容
- request.IOSMutableContent = true;
- //iOS的通知是通过APNs中心来发送的,需要填写对应的环境信息。"DEV" : 表示开发环境 "PRODUCT" : 表示生产环境
- request.IOSApnsEnv = "PRODUCT";
- //消息推送时设备不在线(既与移动推送的服务端的长连接通道不通),则这条推送会做为通知,通过苹果的APNs通道送达一次。注意:离线消息转通知仅适用于生产环境
- request.IOSRemind = true;
- //iOS消息转通知时使用的iOS通知内容,仅当iOSApnsEnv=PRODUCT && iOSRemind为true时有效
- request.IOSRemindBody = Body;
- //自定义的kv结构,开发者扩展用 针对iOS设备
- string PageId = Url;
- string Params = "";
- if (Url.Contains("?"))
- {
- Params = Url.Split("?")[1];
- PageId = Url.Split("?")[0];
- }
- request.IOSExtParameters = "{\"Url\":\"" + PageId + "\",\"Params\":\"" + Params + "\",\"SoundType\":\"0\"}";
- // 推送配置: Android
- // 通知的提醒方式 "VIBRATE" : 震动 "SOUND" : 声音 "BOTH" : 声音和震动 NONE : 静音
- if (Silent)
- {
- request.AndroidNotifyType = "NONE";
- }
- else
- {
- request.AndroidNotifyType = "SOUND";
- }
- //通知栏自定义样式0-100
- request.AndroidNotificationBarType = 3;
- //通知栏显示优先级
- request.AndroidNotificationBarPriority = 0;
- //点击通知后动作 "APPLICATION" : 打开应用 "ACTIVITY" : 打开AndroidActivity "URL" : 打开URL "NONE" : 无跳转
- request.AndroidOpenType = "APPLICATION";
- //Android收到推送后打开对应的url,仅当AndroidOpenType="URL"有效
- //request.AndroidOpenUrl = "http://www.baidu.com";
- //设定通知打开的activity,仅当AndroidOpenType="Activity"有效
- //request.AndroidActivity = "com.alibaba.push2.demo.XiaoMiPushActivity";
- request.AndroidRemind = true;
- request.AndroidPopupActivity = "com.yunmuapp.yunmuapp.activity.WaitActivity";
- request.AndroidPopupTitle = "创业帮";
- request.AndroidPopupBody = Body;
- request.AndroidNotificationChannel = "1";
- //Android通知音乐
- request.AndroidMusic = "default";
- //设置该参数后启动小米托管弹窗功能,此处指定通知点击后跳转的Activity(托管弹窗的前提条件:1. 继承小米辅助通道;2. storeOffline设为true)
- request.AndroidXiaoMiActivity = "com.yunmuapp.yunmuapp.activity.WaitActivity";
- //小米弹窗标题
- request.AndroidXiaoMiNotifyTitle = "创业帮";
- //小米弹窗内容
- request.AndroidXiaoMiNotifyBody = Body;
- // 设定android类型设备通知的扩展属性
- request.AndroidExtParameters = "{\"Url\":\"" + PageId + "\",\"Params\":\"" + Params + "\",\"SoundType\":\"0\"}";
- // 推送控制
- //String pushTime = DateTime.UtcNow.AddSeconds(3).ToString("yyyy-MM-ddTHH\\:mm\\:ssZ");
- if (!string.IsNullOrEmpty(pushTime))
- {
- TimeSpan ts = DateTime.Parse(pushTime) - DateTime.Now;
- request.PushTime = DateTime.UtcNow.Add(ts).ToString("yyyy-MM-ddTHH\\:mm\\:ssZ");//延迟3秒发送
- }
- String expireTime = DateTime.UtcNow.AddDays(1).ToString("yyyy-MM-ddTHH\\:mm\\:ssZ");
- request.ExpireTime = expireTime;//设置过期时间为2天
- request.StoreOffline = true;
- string result = "";
- try
- {
- PushResponse response = client.GetAcsResponse(request);
- Console.WriteLine("RequestId:" + response.RequestId);
- Console.WriteLine("ResponseId:" + response.MessageId);
- Console.ReadLine();
- result = "1|" + response.MessageId;
- }
- catch (ServerException e)
- {
- Console.WriteLine(e.ErrorCode);
- Console.WriteLine(e.ErrorMessage);
- Console.ReadLine();
- function.WriteLog(e.ErrorCode, "阿里云推送异常");
- function.WriteLog(e.ErrorMessage, "阿里云推送异常");
- }
- catch (ClientException e)
- {
- Console.WriteLine(e.ErrorCode);
- Console.WriteLine(e.ErrorMessage);
- Console.ReadLine();
- function.WriteLog(e.ErrorCode, "阿里云推送异常");
- function.WriteLog(e.ErrorMessage, "阿里云推送异常");
- }
- return result;
- }
- public void CancelPush(string msgid)
- {
- IClientProfile clientProfile = DefaultProfile.GetProfile("cn-hangzhou", AccessKeyID, AccessKeySecret);
- DefaultAcsClient client = new DefaultAcsClient(clientProfile);
- CancelPushRequest request = new CancelPushRequest();
- request.AppKey = 24566887;
- request.MessageId = msgid;
- try
- {
- CancelPushResponse response = client.GetAcsResponse(request);
- Console.WriteLine("RequestId:" + response.RequestId);
- Console.ReadLine();
- }
- catch (ServerException e)
- {
- Console.WriteLine(e.ErrorCode);
- Console.WriteLine(e.ErrorMessage);
- Console.ReadLine();
- }
- catch (ClientException e)
- {
- Console.WriteLine(e.ErrorCode);
- Console.WriteLine(e.ErrorMessage);
- Console.ReadLine();
- }
- }
- }
- }
|