极光推送整理笔记

1、.net调用极光方式

 public static class CommonUpgradeMessagePush
    {
        /*移动端推送APP代码 */
        private static string all_app_key = "f94a93bdb61717b91693388d";
        private static string all_master_secret = "cd46805ca3b845ecf3b4a484";

        /// <summary>
        /// 通用推送方法
        /// </summary>
        /// <param name="messageContentPushRecordType">推送类型</param>
        /// <param name="messageContentPushRecordTargetIDList">推送集合</param>
        /// <param name="messageContentText">消息内容</param>
        /// <param name="eventType">事件类型</param>
        /// <returns></returns>
        public static int MessagePush(int messageContentPushRecordType,List<string> messageContentPushRecordTargetIDList, string messageContentText, int eventType, out string returnMessageID)
        {
            returnMessageID = "";

            #region 参数校验
            if (string.IsNullOrEmpty(messageContentText))
            {
                return UpgradeMessageEnum.MessagePushResult.PUSH_CONTENT_ERROR;
            }
            if (messageContentPushRecordTargetIDList.Count == 0)
            {
                return UpgradeMessageEnum.MessagePushResult.TARGET_ID_ERROR;
            }
            #endregion
            
            try
            {
                JPushClient client = new JPushClient(all_app_key, all_master_secret);
                PushPayload payload = new PushPayload();

                /*推送平台*/
                payload.platform = Platform.all();
                /*控制IOS是否发送生产环境, true为生产环境,false为开发环境*/

                payload.options.apns_production = false;
                /*推送对象*/
                if (UpgradeMessageEnum.MessageContentPushRecordType.LABEL == messageContentPushRecordType)
                {
                    /*TODO 鉴于目前的标签存储方式,标签推送都为tag_and ,即取交集的方式 */
                    var audience = Audience.s_tag_and(messageContentPushRecordTargetIDList.ToArray());
                    payload.audience = audience;
                }
                else if (UpgradeMessageEnum.MessageContentPushRecordType.USERIDENTITY == messageContentPushRecordType)
                {
                    /*身份id小写*/
                    List<string> convertMessageContentPushRecordTargetIDList = messageContentPushRecordTargetIDList.Select(p => p.Replace('-', '_').ToLower()).ToList();
                    var audience = Audience.s_alias(convertMessageContentPushRecordTargetIDList.ToArray());
                    payload.audience = audience;
                }
                else
                {
                    return UpgradeMessageEnum.MessagePushResult.PUSH_TYPE_ERROR;
                }


                /*工具栏提示?*/
                var notification = new Notification().setAlert(messageContentText);
                AndroidNotification androidnotification = new AndroidNotification();
                androidnotification.setStyle(1);
                androidnotification.setBig_text(messageContentText);
                androidnotification.setAlert(messageContentText);
                androidnotification.AddExtra("eventtype", eventType);
                notification.AndroidNotification = androidnotification;
                notification.IosNotification = new IosNotification();
                notification.IosNotification.incrBadge(1).setAlert(messageContentText).AddExtra("eventtype", eventType);
                payload.notification = notification.Check();
                /*推送内容*/
                payload.message = Message.content(messageContentText);

                var result = client.SendPush(payload);
                if (result.isResultOK())
                {
                    returnMessageID = result.msg_id.ToString();
                    //client.getReceivedApi(returnMessageID);
                    //client.getReceivedApi_v3(returnMessageID);
                 
                    return UpgradeMessageEnum.MessagePushResult.SUCCESS;
                }
                return UpgradeMessageEnum.MessagePushResult.FAIL;
            }
            catch (Exception ex)
            {
        //注意:推送不成功,直接报异常。所以需要try catch
                Log4NetUtility.Error("极光推送出现错误", ex);
                return UpgradeMessageEnum.MessagePushResult.FAIL;
            }
        }
    }

 

 

 

2、注意事项

1)内容限制

2)推送频率

免费用户仅仅只是限制的api的调用频率:600次/min,超频api调用会有错误提示。

3)极光提供的推送方式有:别名、标签、registrationID和广播

一次推送 标签不得超过20个;别名不得超过1000个

其中:registrationID 是客户端传递给服务端的

客户端每次登录都获取一下,考虑到可能一个设备登录不同的账号,以及iOS9之后,卸载重装registrationID会改变

 

 

3、常见问题

 1)推送消息后,查看消息具体被哪些用户收到

只能在客户端收到消息后将信息上报给你们服务器,服务端没有提供API去直接获取「具体谁收到了」

调 Report API 进行统计查询https://community.jiguang.cn/t/topic/28411/6

--未完待续--

posted on 2018-08-08 15:15  喵喵学程序  阅读(230)  评论(0编辑  收藏  举报

导航