复制代码
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396using System; using System.Collections.Generic; using System.Linq; using System.Text; using Google.ProtocolBuffers; using com.gexin.rp.sdk.dto; using com.igetui.api.openservice; using com.igetui.api.openservice.igetui; using com.igetui.api.openservice.igetui.template; using com.igetui.api.openservice.payload; using System.Net; /** * * 说明: * 此工程是一个测试工程,所用的相关.dll文件,都已经存在protobuffer文件里,需要加载到References里。 * 工程中还有用到一个System.Web.Extensions文件,这个文件是用到Framework里V4.0版本的, * 一般路径如下:C:Program FilesReference AssembliesMicrosoftFramework.NETFrameworkv4.0, * 或如下路径:C:Program FilesReference AssembliesMicrosoftFrameworkv3.5没有的也可以在protobuffer * 文件夹里找到。如再有问题,请直接联系技术客服,谢谢! * GetuiServerApiSDK:此.dll文件为个推C#版本的SDK文件 * Google.ProtocolBuffers:此.dll文件为Google的数据交换格式文件 * 注: * 新增一个连接超时时间设置,通过在环境变量--用户变量中增加名为:GETUI_TIMEOUT 的变量(修改环境变量, 电脑重启后才能生效),值则是超时时间,如不设定,则默认为20秒。 **/ namespace GetuiServerApiSDKDemo { public class demo { //参数设置 <-----参数需要重新设置-----> //http的域名 private static String HOST = "http://sdk.open.api.igexin.com/apiex.htm"; //https的域名 //private static String HOST = "https://api.getui.com/apiex.htm"; //定义常量, appId、appKey、masterSecret 采用本文档 "第二步 获取访问凭证 "中获得的应用配置 private static String APPID = "S7lV1O12Rm5Fe2DV5ON4i9"; private static String APPKEY = "MZFiAYuK329I3pUxVthln1"; private static String MASTERSECRET = "pg12WgRibz7Np1oOzYvkF5"; private static String ALIAS = "1214"; //private static String CLIENTID = "b654f1e1c4026f4bbacd886ecfcba086"; private static String CLIENTID = "76c43bb7471b755274668adbe5431a0d"; //private static String APPID = "0Onhw45g6F8OqfgzguH203"; //private static String APPKEY = "Cef29BKlxi7VIM1pjgppk8"; //private static String MASTERSECRET = "RlMtDeu8aWfghwf8OTIVBn9"; //private static String CLIENTID = "4e137123eef4badbafg93f223512e0e0"; //private static String DeviceToken = ""; //填写IOS系统的DeviceToken static void Main(string[] args) { //toList接口每个用户状态返回是否开启,可选 Console.OutputEncoding = Encoding.GetEncoding(936); Environment.SetEnvironmentVariable("needDetails", "true"); //下为消息推送的四种方式,单独使用时,请注释掉另外三种方法 //单个用户推送 PushMessageToSingle(); Console.WriteLine("finish"); //对指定环境用户推送 //pushMessageToApp(); //对指定列表用户推送 //PushMessageToList(); //对指定应用群推送 //pushMessageToApp(); //批量单推 //singleBatchDemo(); } private static void PushMessageToSingle() { IGtPush push = new IGtPush(HOST, APPKEY, MASTERSECRET); //消息模版:TransmissionTemplate:透传模板 TransmissionTemplate template = TransmissionTemplateDemo1(); //通知透传模板 //NotificationTemplate template = NotificationTemplateDemo(); // 单推消息模型 SingleMessage message = new SingleMessage(); message.IsOffline = true; // 用户当前不在线时,是否离线存储,可选 message.OfflineExpireTime = 1000 * 3600 * 12; // 离线有效时间,单位为毫秒,可选 message.Data = template; //判断是否客户端是否wifi环境下推送,2为4G/3G/2G,1为在WIFI环境下,0为不限制环境 //message.PushNetWorkType = 1; com.igetui.api.openservice.igetui.Target target = new com.igetui.api.openservice.igetui.Target(); target.appId = APPID; target.clientId = CLIENTID; //target.alias = ALIAS; try { String pushResult = push.pushMessageToSingle(message, target); Console.WriteLine("-----------------------------------------------"); Console.WriteLine("-----------------------------------------------"); Console.WriteLine("----------------服务端返回结果:" + pushResult); } catch (RequestException e) { String requestId = e.RequestId; //发送失败后的重发 String pushResult = push.pushMessageToSingle(message, target, requestId); Console.WriteLine("-----------------------------------------------"); Console.WriteLine("-----------------------------------------------"); Console.WriteLine("----------------服务端返回结果:" + pushResult); } } //PushMessageToList接口测试代码 private static void PushMessageToList() { // 推送主类(方式1,不可与方式2共存) IGtPush push = new IGtPush(HOST, APPKEY, MASTERSECRET); // 推送主类(方式2,不可与方式1共存)此方式可通过获取服务端地址列表判断最快域名后进行消息推送,每10分钟检查一次最快域名 //IGtPush push = new IGtPush("",APPKEY,MASTERSECRET); ListMessage message = new ListMessage(); NotificationTemplate template = NotificationTemplateDemo(); // 用户当前不在线时,是否离线存储,可选 message.IsOffline = true; // 离线有效时间,单位为毫秒,可选 message.OfflineExpireTime = 1000 * 3600 * 12; message.Data = template; //message.PushNetWorkType = 0; //判断是否客户端是否wifi环境下推送,1为在WIFI环境下,0为不限制网络环境。 //设置接收者 List<com.igetui.api.openservice.igetui.Target> targetList = new List<com.igetui.api.openservice.igetui.Target>(); com.igetui.api.openservice.igetui.Target target1 = new com.igetui.api.openservice.igetui.Target(); target1.appId = APPID; target1.clientId = CLIENTID; // 如需要,可以设置多个接收者 //com.igetui.api.openservice.igetui.Target target2 = new com.igetui.api.openservice.igetui.Target(); //target2.AppId = APPID; //target2.ClientId = "ddf730f6cabfa02ebabf06e0c7fc8da0"; targetList.Add(target1); //targetList.Add(target2); String contentId = push.getContentId(message); String pushResult = push.pushMessageToList(contentId, targetList); System.Console.WriteLine("-----------------------------------------------"); System.Console.WriteLine("服务端返回结果:" + pushResult); } /// <summary> /// pushMessageToApp接口测试代码 /// </summary> private static void pushMessageToApp() { // 推送主类(方式1,不可与方式2共存) IGtPush push = new IGtPush(HOST, APPKEY, MASTERSECRET); // 推送主类(方式2,不可与方式1共存)此方式可通过获取服务端地址列表判断最快域名后进行消息推送,每10分钟检查一次最快域名 //IGtPush push = new IGtPush("",APPKEY,MASTERSECRET); AppMessage message = new AppMessage(); // 设置群推接口的推送速度,单位为条/秒,仅对pushMessageToApp(对指定应用群推接口)有效 message.Speed = 100; TransmissionTemplate template = TransmissionTemplateDemo(); // 用户当前不在线时,是否离线存储,可选 message.IsOffline = false; // 离线有效时间,单位为毫秒,可选 message.OfflineExpireTime = 1000 * 3600 * 12; message.Data = template; //message.PushNetWorkType = 0; //判断是否客户端是否wifi环境下推送,1为在WIFI环境下,0为不限制网络环境。 List<String> appIdList = new List<string>(); appIdList.Add(APPID); //通知接收者的手机操作系统类型 List<String> phoneTypeList = new List<string>(); //phoneTypeList.Add("ANDROID"); //phoneTypeList.Add("IOS"); //通知接收者所在省份 List<String> provinceList = new List<string>(); //provinceList.Add("浙江"); //provinceList.Add("上海"); //provinceList.Add("北京"); List<String> tagList = new List<string>(); //tagList.Add("开心"); message.AppIdList = appIdList; message.PhoneTypeList = phoneTypeList; message.ProvinceList = provinceList; message.TagList = tagList; String pushResult = push.pushMessageToApp(message); System.Console.WriteLine("-----------------------------------------------"); System.Console.WriteLine("服务端返回结果:" + pushResult); } /// <summary> /// TransmissionTemplate:透传模板 /// </summary> public static void singleBatchDemo() { IGtPush push = new IGtPush(HOST, APPKEY, MASTERSECRET); IBatch batch = new BatchImpl(APPKEY, push); //消息模版:TransmissionTemplate:透传模板 TransmissionTemplate templateTrans = TransmissionTemplateDemo(); // 单推消息模型 SingleMessage messageTrans = new SingleMessage(); messageTrans.IsOffline = true; // 用户当前不在线时,是否离线存储,可选 messageTrans.OfflineExpireTime = 1000 * 3600 * 12; // 离线有效时间,单位为毫秒,可选 messageTrans.Data = templateTrans; //判断是否客户端是否wifi环境下推送,2为4G/3G/2G,1为在WIFI环境下,0为不限制环境 //messageTrans.PushNetWorkType = 1; com.igetui.api.openservice.igetui.Target targetTrans = new com.igetui.api.openservice.igetui.Target(); targetTrans.appId = APPID; targetTrans.clientId = CLIENTID; batch.add(messageTrans, targetTrans); TransmissionTemplate templateNoti = null;//NotificationTemplateDemo(); // 单推消息模型 SingleMessage messageNoti = new SingleMessage(); messageNoti.IsOffline = true; // 用户当前不在线时,是否离线存储,可选 messageNoti.OfflineExpireTime = 1000 * 3600 * 12; // 离线有效时间,单位为毫秒,可选 messageNoti.Data = templateNoti; //判断是否客户端是否wifi环境下推送,2为4G/3G/2G,1为在WIFI环境下,0为不限制环境 //messageNoti.PushNetWorkType = 1; com.igetui.api.openservice.igetui.Target targetNoti = new com.igetui.api.openservice.igetui.Target(); targetNoti.appId = APPID; targetNoti.clientId = CLIENTID; batch.add(messageNoti, targetNoti); try { batch.submit(); } catch(Exception e) { batch.retry(); } } /// <summary> /// 透传模板动作内容 /// </summary> /// <returns></returns> public static TransmissionTemplate TransmissionTemplateDemo1() { TransmissionTemplate template = new TransmissionTemplate(); template.AppId = APPID; template.AppKey = APPKEY; //应用启动类型,1:强制应用启动 2:等待应用启动 template.TransmissionType = "2"; //透传内容 // android body 显示用这个 template.TransmissionContent = "1111"; //设置通知定时展示时间,结束时间与开始时间相差需大于6分钟,消息推送后,客户端将在指定时间差内展示消息(误差6分钟) //String begin = "2017-03-13 00:36:10"; //String end = "2017-03-13 23:46:20"; //template.setDuration(begin, end); //APN高级推送 开始 APNPayload apnpayload = new APNPayload(); DictionaryAlertMsg alertMsg = new DictionaryAlertMsg(); // IOS 的body用这个 alertMsg.Body = "Body"; alertMsg.ActionLocKey = "ActionLocKey"; alertMsg.LocKey = "LocKey"; alertMsg.addLocArg("LocArg"); alertMsg.LaunchImage = "LaunchImage"; //iOS8.2支持字段 alertMsg.Title = "Title"; alertMsg.TitleLocKey = "TitleLocKey"; alertMsg.addTitleLocArg("TitleLocArg"); apnpayload.AlertMsg = alertMsg; apnpayload.Badge = 1; apnpayload.ContentAvailable = 1; //apnpayload.Category = ""; apnpayload.Sound = "test1.wav"; apnpayload.addCustomMsg("payload", "{"type":"url","url":"http://WWW.BAIDU.COM"}"); template.setAPNInfo(apnpayload); //APN高级推送 结束 return template; } /// <summary> /// 通知透传模板动作内容 /// </summary> /// <returns></returns> public static NotificationTemplate NotificationTemplateDemo() { NotificationTemplate template = new NotificationTemplate(); template.AppId = APPID; template.AppKey = APPKEY; //通知栏标题 template.Title = "这是一个消息通知"; //通知栏内容 template.Text = "消息通知正文"; //通知栏显示本地图片 template.Logo = ""; //通知栏显示网络图标 template.LogoURL = ""; //应用启动类型,1:强制应用启动 2:等待应用启动 template.TransmissionType = "2"; //透传内容 template.TransmissionContent = "{"type":"url","http://WWW.BAIDU.COM"}"; //接收到消息是否响铃,true:响铃 false:不响铃 template.IsRing = true; //接收到消息是否震动,true:震动 false:不震动 template.IsVibrate = true; //接收到消息是否可清除,true:可清除 false:不可清除 template.IsClearable = true; //设置通知定时展示时间,结束时间与开始时间相差需大于6分钟,消息推送后,客户端将在指定时间差内展示消息(误差6分钟) //String begin = "2015-03-06 14:36:10"; //String end = "2015-03-06 14:46:20"; //template.setDuration(begin, end); return template; } /// <summary> /// 透传模板动作内容 /// </summary> /// <returns></returns> public static TransmissionTemplate TransmissionTemplateDemo() { TransmissionTemplate template = new TransmissionTemplate(); template.AppId = APPID; template.AppKey = APPKEY; //应用启动类型,1:强制应用启动 2:等待应用启动 template.TransmissionType = "1"; //透传内容 template.TransmissionContent = "透传内容"; //设置通知定时展示时间,结束时间与开始时间相差需大于6分钟,消息推送后,客户端将在指定时间差内展示消息(误差6分钟) //String begin = "2015-03-06 14:36:10"; //String end = "2015-03-06 14:46:20"; //template.setDuration(begin, end); return template; } /// <summary> /// 网页模板内容 /// </summary> /// <returns></returns> public static LinkTemplate LinkTemplateDemo() { LinkTemplate template = new LinkTemplate(); template.AppId = APPID; template.AppKey = APPKEY; //通知栏标题 template.Title = "请填写通知标题"; //通知栏内容 template.Text = "请填写通知内容"; //通知栏显示本地图片 template.Logo = ""; //通知栏显示网络图标,如无法读取,则显示本地默认图标,可为空 template.LogoURL = ""; //打开的链接地址 template.Url = "http://www.baidu.com"; //接收到消息是否响铃,true:响铃 false:不响铃 template.IsRing = true; //接收到消息是否震动,true:震动 false:不震动 template.IsVibrate = true; //接收到消息是否可清除,true:可清除 false:不可清除 template.IsClearable = true; return template; } /// <summary> /// 通知栏弹框下载模板 /// </summary> /// <returns></returns> public static NotyPopLoadTemplate NotyPopLoadTemplateDemo() { NotyPopLoadTemplate template = new NotyPopLoadTemplate(); template.AppId = APPID; template.AppKey = APPKEY; //通知栏标题 template.NotyTitle = "请填写通知标题"; //通知栏内容 template.NotyContent = "请填写通知内容"; //通知栏显示本地图片 template.NotyIcon = "icon.png"; //通知栏显示网络图标 template.LogoURL = "http://www-igexin.qiniudn.com/wp-content/uploads/2013/08/logo_getui1.png"; //弹框显示标题 template.PopTitle = "弹框标题"; //弹框显示内容 template.PopContent = "弹框内容"; //弹框显示图片 template.PopImage = ""; //弹框左边按钮显示文本 template.PopButton1 = "下载"; //弹框右边按钮显示文本 template.PopButton2 = "取消"; //通知栏显示下载标题 template.LoadTitle = "下载标题"; //通知栏显示下载图标,可为空 template.LoadIcon = "file://push.png"; //下载地址,不可为空 template.LoadUrl = "http://www.appchina.com/market/d/425201/cop.baidu_0/com.gexin.im.apk "; //应用安装完成后,是否自动启动 template.IsActived = true; //下载应用完成后,是否弹出安装界面,true:弹出安装界面,false:手动点击弹出安装界面 template.IsAutoInstall = true; //接收到消息是否响铃,true:响铃 false:不响铃 template.IsBelled = true; //接收到消息是否震动,true:震动 false:不震动 template.IsVibrationed = true; //接收到消息是否可清除,true:可清除 false:不可清除 template.IsCleared = true; return template; } } }
最后
以上就是瘦瘦绿茶最近收集整理的关于c#,利用个推实现APP消息推送的全部内容,更多相关c#内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复