极光推送踩坑 【一】

坑1:关于网上推荐使用 个推 而不建议使用极光,原因是会被第三方封号;众说纷纭,各持己见,不做评论,开发者自己斟酌。

坑2:如果是.net后台,使用nuget引入包jpush.net时候,会强制引入 newtonsoft并且指定了具体的版本,如果项目中之前引入其他的版本,,此时运行时报错,(这种强依赖太恶心)

  同时,使用的 jpush.net的所以对象现在的版本后面都需要有一个 V3,有别于之前版本,官方解释是即便是用了之前本本依旧会强制转换为V3

坑3:推送方式有三:根据 设备注册ID推送(点对点),标识推送(点对点或点对多或广播),别名推送(点对点或点对多或广播),虽然说的是三种方式,但是实际应用却又支持多种方式组合推送,需要根据自己的业务而定,没有绝对的组合方式。

坑4:光放应用配置 中配置的 android和IOS的应用包名,和移动应用打包时候,一定要对应上,否则出现推送成功,却接受不到推送内容的问题。

-------------------------------------------------------------------

使用参考:

客户端:

1.客户端 引入jpush sdk

2.(别名或者标签推送),注册别名或者标签 jPushPlugin.setTagsWithAlias(tags, alias);

此处要注意:注册别名或者标签之前,一定要先 调用jpushPlugin的init去先初始化jpush对象,

服务端(简单参考哦):

  1 using Beyova.JPush;
  2 using Beyova.JPush.V3;
  3 using CQ.Infrastructure.Utils.Data;
  4 using CQ.Infrastructure.Utils.Extensions;
  5 using System;
  6 using System.Collections.Generic;
  7 using System.Configuration;
  8 using System.Linq;
  9 using System.Text;
 10 using System.Threading.Tasks;
 11 
 12 namespace CQ.Infrastructure.Utils.Push
 13 {
 14     /// <summary>
 15     ///     三种推送方式
 16     ///         1.根据在极光官方注册的ID推送
 17     ///         2.根据别名推送
 18     ///         3.根据标签或者标签组合推送
 19     /// </summary>
 20     public class JPushHelper
 21     {
 22         #region fields
 23         private static JPushClientV3 jpushClient;
 24         private static JPushInfo jpushInfo = null;
 25         #endregion
 26         #region ctor
 27         static JPushHelper()
 28         {
 29             jpushInfo = new Push.JPushInfo
 30             {
 31                 appKey = ConfigurationManager.AppSettings["jpush_key"],
 32                 masterSecret = ConfigurationManager.AppSettings["jpush_secret"]
 33             };
 34         }
 35         private static void Init()
 36         {
 37             jpushClient = new JPushClientV3(jpushInfo.appKey, jpushInfo.masterSecret);
 38         }
 39         #endregion
 40 
 41         #region methods
 42         /// <summary>
 43         ///     根据多个指定 注册ID推送
 44         ///          [ "4312kjklfds2", "8914afd2", "45fdsa31" ]
 45         /// </summary>
 46         /// <param name="registrationId">极光官网注册ID数组</param>
 47         /// <param name="notification"></param>
 48         /// <param name="customizedValues"></param>
 49         /// <returns></returns>
 50         public static OperationResult DoPushByRegistrationId(
 51             List<string> registrationIds,
 52             NotificationInfo notification)
 53         {
 54             if (jpushClient == null)
 55                 Init();
 56 
 57             Audience audience = new Audience();
 58             audience.Add(PushTypeV3.ByRegistrationId, registrationIds);
 59 
 60             var response = jpushClient.SendPushMessage(new PushMessageRequestV3
 61             {
 62                 Audience = audience,
 63                 Platform = PushPlatform.AndroidAndiOS,
 64                 IsTestEnvironment = true,
 65                 AppMessage = new AppMessage
 66                 {
 67                     Content = notification.Alert,
 68                     CustomizedValue = notification.CustomizedValues,
 69                     Title = notification.Title
 70                 },
 71                 Notification = new Notification
 72                 {
 73                     AndroidNotification = new AndroidNotificationParameters
 74                     {
 75                         Title = notification.Title,
 76                         Alert = notification.Alert,
 77                         CustomizedValues = notification.CustomizedValues
 78                     },
 79                     iOSNotification = new iOSNotificationParameters
 80                     {
 81                         Badge = 1,
 82                         Alert = notification.Alert,
 83                         //Sound = "YourSound",//default
 84                         CustomizedValues = notification.CustomizedValues
 85                     }
 86                 }
 87             });
 88             var res = response.ResponseCode.ToString();
 89             if (res == "Succeed")
 90             {
 91                 //发送成功
 92                 List<string> idToCheck = new List<string>();
 93                 idToCheck.Add(response.MessageId);
 94 
 95                 var statusList = jpushClient.QueryPushMessageStatus(idToCheck);
 96                 if (statusList != null)
 97                 {
 98                     //foreach (var one in statusList)
 99                     //{
100                     //    Console.WriteLine(string.Format("Id: {0}, Android: {1}, iOS: {2}", one.MessageId, one.AndroidDeliveredCount, one.ApplePushNotificationDeliveredCount));
101                     //}
102                     return new OperationResult(OperationResultType.Success, "推送成功", statusList);
103                 }
104                 return new OperationResult(OperationResultType.Success, "推送成功", null);
105             }
106             return new OperationResult(OperationResultType.Success, "推送失败", null);
107         }
108 
109         /// <summary>
110         ///     根据多个别名推送
111         ///         [ "4314", "892", "4531" ]
112         /// </summary>
113         /// <param name="alias">别名数组</param>
114         /// <param name="notification"></param>
115         /// <param name="customizedValues"></param>
116         /// <returns></returns>
117         public static OperationResult DoPushByAlias(
118             List<string> alias,
119             NotificationInfo notification)
120         {
121             if (jpushClient == null)
122                 Init();
123 
124             Audience audience = new Audience();
125             audience.Add(PushTypeV3.ByTagWithinOr, alias);
126             audience.Add(PushTypeV3.ByAlias, alias);
127 
128             var response = jpushClient.SendPushMessage(new PushMessageRequestV3
129             {
130                 Audience = audience,
131                 Platform = PushPlatform.All,
132                 //AppMessage = new AppMessage
133                 //{
134                 //    Content = notification.Alert,
135                 //    CustomizedValue = notification.CustomizedValues,
136                 //    Title = notification.Title
137                 //},
138                 Notification = new Notification
139                 {
140                     AndroidNotification = new AndroidNotificationParameters
141                     {
142                         Title = notification.Title,
143                         Alert = notification.Alert,
144                         CustomizedValues = notification.CustomizedValues
145                     },
146                     iOSNotification = new iOSNotificationParameters
147                     {
148                         Badge = 1,
149                         Alert = notification.Alert,
150                         //Sound = "YourSound",//default
151                         CustomizedValues = notification.CustomizedValues
152                     }
153                 },
154                 LifeTime = 86400,
155                 IsTestEnvironment = true
156             });
157             var res = response.ResponseCode.ToString();
158             if (res == "Succeed")
159             {
160                 //发送成功
161                 List<string> idToCheck = new List<string>();
162                 int parseVal;
163                 if (Int32.TryParse(response.MessageId, out parseVal))
164                 {
165                     idToCheck.Add(response.MessageId);
166                 }
167                 else {
168                     return new OperationResult(OperationResultType.Success, "推送成功", null);
169                 }
170 
171                 var statusList = jpushClient.QueryPushMessageStatus(idToCheck);
172                 if (statusList != null)
173                 {
174                     //foreach (var one in statusList)
175                     //{
176                     //    Console.WriteLine(string.Format("Id: {0}, Android: {1}, iOS: {2}", one.MessageId, one.AndroidDeliveredCount, one.ApplePushNotificationDeliveredCount));
177                     //}
178                     return new OperationResult(OperationResultType.Success, "推送成功", statusList);
179                 }
180                 return new OperationResult(OperationResultType.Success, "推送成功", null);
181             }
182             return new OperationResult(OperationResultType.Success, "推送失败", null);
183         }
184 
185         /// <summary>
186         ///     根据多个标签推送(or关系)
187         ///         "tag" : [ "深圳", "广州", "北京" ]
188         /// </summary>
189         /// <param name="tags">and标签数组</param>
190         /// <param name="notification"></param>
191         /// <param name="customizedValues"></param>
192         /// <returns></returns>
193         public static OperationResult DoPushByTagOr(
194             List<string> tags,
195             NotificationInfo notification)
196         {
197             if (jpushClient == null)
198                 Init();
199 
200             Audience audience = new Audience();
201             audience.Add(PushTypeV3.ByTagWithinOr, tags);
202 
203             var response = jpushClient.SendPushMessage(new PushMessageRequestV3
204             {
205                 Audience = audience,
206                 Platform = PushPlatform.AndroidAndiOS,
207                 IsTestEnvironment = true,
208                 AppMessage = new AppMessage
209                 {
210                     Content = notification.Alert,
211                     CustomizedValue = notification.CustomizedValues,
212                     Title = notification.Title
213                 },
214                 Notification = new Notification
215                 {
216                     AndroidNotification = new AndroidNotificationParameters
217                     {
218                         Title = notification.Title,
219                         Alert = notification.Alert,
220                         CustomizedValues = notification.CustomizedValues
221                     },
222                     iOSNotification = new iOSNotificationParameters
223                     {
224                         Badge = 1,
225                         Alert = notification.Alert,
226                         //Sound = "YourSound",//default
227                         CustomizedValues = notification.CustomizedValues
228                     }
229                 }
230             });
231             var res = response.ResponseCode.ToString();
232             if (res == "Succeed")
233             {
234                 //发送成功
235                 List<string> idToCheck = new List<string>();
236                 idToCheck.Add(response.MessageId);
237 
238                 var statusList = jpushClient.QueryPushMessageStatus(idToCheck);
239                 if (statusList != null)
240                 {
241                     //foreach (var one in statusList)
242                     //{
243                     //    Console.WriteLine(string.Format("Id: {0}, Android: {1}, iOS: {2}", one.MessageId, one.AndroidDeliveredCount, one.ApplePushNotificationDeliveredCount));
244                     //}
245                     return new OperationResult(OperationResultType.Success, "推送成功", statusList);
246                 }
247                 return new OperationResult(OperationResultType.Success, "推送成功", null);
248             }
249             return new OperationResult(OperationResultType.Success, "推送失败", null);
250         }
251 
252 
253         /// <summary>
254         ///     根据多个标签推送(And关系)
255         ///         可同时推送指定多类推送目标:在深圳或者广州,并且是 “女” “会员”
256         ///         "tag" : [ "深圳", "广州" ]
257         //          "tag_and" : [ "女", "会员"]
258         /// </summary>
259         /// <param name="alias">or标签数组</param>
260         /// <param name="tagsAnd">and 标签数组</param>
261         /// <param name="notification">推送notif信息</param>
262         /// <param name="customizedValues"></param>
263         /// <returns></returns>
264         public static OperationResult DoPushByTagAndTag(
265             List<string> tags,
266             List<string> tagsAnd,
267             NotificationInfo notification)
268         {
269             if (jpushClient == null)
270                 Init();
271 
272             Audience audience = new Audience();
273             audience.Add(PushTypeV3.ByTagWithinOr, tags);
274             audience.Add(PushTypeV3.ByTagWithinAnd, tagsAnd);
275 
276             var response = jpushClient.SendPushMessage(new PushMessageRequestV3
277             {
278                 Audience = audience,
279                 Platform = PushPlatform.AndroidAndiOS,
280                 IsTestEnvironment = true,
281                 AppMessage = new AppMessage
282                 {
283                     Content = notification.Alert,
284                     CustomizedValue = notification.CustomizedValues,
285                     Title = notification.Title
286                 },
287                 Notification = new Notification
288                 {
289                     AndroidNotification = new AndroidNotificationParameters
290                     {
291                         Title = notification.Title,
292                         Alert = notification.Alert,
293                         CustomizedValues = notification.CustomizedValues
294                     },
295                     iOSNotification = new iOSNotificationParameters
296                     {
297                         Badge = 1,
298                         Alert = notification.Alert,
299                         //Sound = "YourSound",//default
300                         CustomizedValues = notification.CustomizedValues
301                     }
302                 }
303             });
304             var res = response.ResponseCode.ToString();
305             if (res == "Succeed")
306             {
307                 //发送成功
308                 List<string> idToCheck = new List<string>();
309 
310                 idToCheck.Add(response.MessageId);
311 
312                 var statusList = jpushClient.QueryPushMessageStatus(idToCheck);
313                 if (statusList != null)
314                 {
315                     //foreach (var one in statusList)
316                     //{
317                     //    Console.WriteLine(string.Format("Id: {0}, Android: {1}, iOS: {2}", one.MessageId, one.AndroidDeliveredCount, one.ApplePushNotificationDeliveredCount));
318                     //}
319                     return new OperationResult(OperationResultType.Success, "推送成功", statusList);
320                 }
321                 return new OperationResult(OperationResultType.Success, "推送成功", null);
322             }
323             return new OperationResult(OperationResultType.Success, "推送失败", null);
324         }
325 
326 
327         /// <summary>
328         ///     推送给多个标签
329         ///     (需要同时在多个标签范围内):在深圳并且是“女”
330         ///         "tag_and" : [ "深圳", "女" ]
331         /// </summary>
332         /// <param name="tags">and标签数组</param>
333         /// <param name="notification"></param>
334         /// <param name="customizedValues"></param>
335         /// <returns></returns>
336         public static OperationResult DoPushByTagAnd(
337             List<string> tags,
338             NotificationInfo notification)
339         {
340             if (jpushClient == null)
341                 Init();
342 
343             Audience audience = new Audience();
344             audience.Add(PushTypeV3.ByTagWithinAnd, tags);
345 
346             var response = jpushClient.SendPushMessage(new PushMessageRequestV3
347             {
348                 Audience = audience,
349                 Platform = PushPlatform.AndroidAndiOS,
350                 IsTestEnvironment = true,
351                 AppMessage = new AppMessage
352                 {
353                     Content = notification.Alert,
354                     CustomizedValue = notification.CustomizedValues,
355                     Title = notification.Title
356                 },
357                 Notification = new Notification
358                 {
359                     AndroidNotification = new AndroidNotificationParameters
360                     {
361                         Title = notification.Title,
362                         Alert = notification.Alert,
363                         CustomizedValues = notification.CustomizedValues
364                     },
365                     iOSNotification = new iOSNotificationParameters
366                     {
367                         Badge = 1,
368                         Alert = notification.Alert,
369                         //Sound = "YourSound",//default
370                         CustomizedValues = notification.CustomizedValues
371                     }
372                 }
373             });
374             var res = response.ResponseCode.ToString();
375             if (res == "Succeed")
376             {
377                 //发送成功
378                 List<string> idToCheck = new List<string>();
379 
380                 idToCheck.Add(response.MessageId);
381 
382                 var statusList = jpushClient.QueryPushMessageStatus(idToCheck);
383                 if (statusList != null)
384                 {
385                     return new OperationResult(OperationResultType.Success, "推送成功", statusList);
386                 }
387                 return new OperationResult(OperationResultType.Success, "推送成功", null);
388             }
389             return new OperationResult(OperationResultType.Success, "推送失败", null);
390         }
391         #endregion
392 
393     }
394 
395 
396     /// <summary>
397     ///     提示信息
398     /// </summary>
399     public class NotificationInfo
400     {
401         public string Title { get; set; }
402         public string Alert { get; set; }
403         public Dictionary<string, object> CustomizedValues { get; set; }
404         //customizedValues.Add("CK1", "CV1");
405         //    customizedValues.Add("CK2", "CV2");
406 
407 
408     }
409 
410     /// <summary>
411     ///     极光appKey和secret
412     /// </summary>
413     public class JPushInfo
414     {
415         public string appKey { get; set; }
416         public string masterSecret { get; set; }
417     }
418 
419 }
View Code
原文地址:https://www.cnblogs.com/Tmc-Blog/p/6877644.html