C#----极光推送SDK的简单应用

准备条件:在NuGet 下载极光的SDK 搜索:

cn.jpush.api

C#----极光推送SDK的简单应用_第1张图片

直接上代码,创建一个方法类,写入下面方法:

 /// 
        /// 推送信息
        /// 
        /// -1-全部 3-Android 4-iOS
        /// 是否发送全部设备ID 1-是 0-否
        /// 设备注册ID数组集合 All为全部
        /// 标题
        /// 内容
        /// 
        public static int PushMessage(int DeviceType, int IsPushAllRegID, string[] RegIDAry, string Title, string Message)
        {
            int ret = 0;

            string appKey = ConfigurationManager.AppSettings["AppKey"].ToString();
            string master_Secret = ConfigurationManager.AppSettings["Master_Secret"].ToString();

            JPushClient client = new JPushClient(appKey, master_Secret);
            PushPayload payload = Get_AndroidPushPayload(DeviceType, IsPushAllRegID, RegIDAry, Title, Message);
            try
            {
                var result = client.SendPush(payload);
                var apiResult = client.getReceivedApi(result.msg_id.ToString());
                var apiResultv3 = client.getReceivedApi_v3(result.msg_id.ToString());
                var queryResultWithV2 = client.getReceivedApi("1739302794");
                var querResultWithV3 = client.getReceivedApi_v3("1739302794");
            }
            catch (APIRequestException e)
            {
                throw e;
            }
            catch (APIConnectionException e)
            {
                return 1;
            }
            ret = 1;
            return ret;
        }

        /// 
        /// 返回指定类型的推送对象
        /// 
        /// -1-全部 3-Android 4-iOS
        /// 设备注册ID数组集合 All为全部
        /// 标题
        /// 内容
        /// 
        private static PushPayload Get_AndroidPushPayload(int DeviceType, int IsPushAllRegID, string[] RegIDAry, string Title, string Message)
        {
            PushPayload pushPayload = new PushPayload();

            //根据设备类型,选择对应的推送目标
            switch (DeviceType)
            {
                case -1:
                    pushPayload.platform = Platform.all();
                    break;
                case 3:
                    pushPayload.platform = Platform.android();
                    break;
                case 4:
                    pushPayload.platform = Platform.ios();
                    break;
                default:
                    pushPayload.platform = Platform.all();
                    break;
            }
            //判断是否发送全部用户
            if (IsPushAllRegID == 1)
            {
                pushPayload.audience = Audience.all();
            }
            else
            {
                pushPayload.audience = Audience.s_registrationId(RegIDAry);
            }

            Notification nt = new Notification();

            //设置安卓推送对象
            if (DeviceType == -1 && DeviceType == 3)
            {
                AndroidNotification anf = new AndroidNotification().setTitle(Title)
                                                                  .setAlert(Message)
                                                                  .setBuilderID(3)
                                                                  .setPriority(2)
                                                                  .setStyle(2);
                nt.AndroidNotification = anf;
            }

            //设置苹果推送对象
            if (DeviceType == -1 && DeviceType == 4)
            {
                IosNotification inf = new IosNotification().setAlert(Message);
                nt.IosNotification = inf;
            }

            pushPayload.notification = nt;
            return pushPayload;
        }

 

 

你可能感兴趣的:(C#)