예제 #1
0
        public void sendMessageContentAndTitle()
        {
            //PushPayload payload = PushPayload.newBuilder()
            //        .setAudience(Audience.all())
            //        .setPlatform(Platform.all())
            //        .setMessage(Message.newBuilder()
            //                .setTitle("message title")
            //                .setContentType("content type")
            //                .setMsgContent(MSG_CONTENT).build())
            //        .build();
            PushPayload payload = new PushPayload();

            payload.platform = Platform.all();
            payload.audience = Audience.all();
            payload.message  = Message.content(MSG_CONTENT).setTitle("message title").setContentType("ontent type");

            var result = _client.SendPush(payload);

            Assert.IsTrue(result.isResultOK());
        }
예제 #2
0
        public void invalidParams_notification_ios()
        {
            JObject payload = new JObject();

            payload.Add("platform", JToken.FromObject(JsonConvert.SerializeObject(Platform.all(), new PlatformConverter())));
            payload.Add("audience", JToken.FromObject(JsonConvert.SerializeObject(Audience.all(), new AudienceConverter())));

            JObject notification = new JObject();

            notification.Add("ios", JToken.FromObject(ALERT));
            payload.Add("notification", notification);

            Console.WriteLine("json string: " + payload.ToString());

            try {
                _client.SendPush(payload.ToString());
            } catch (APIRequestException e) {
                Assert.AreEqual(INVALID_PARAMS, e.ErrorCode);
            }
        }
예제 #3
0
        public static SimpleResultModel SendToAll(string msg)
        {
            PushPayload pushPayload = new PushPayload();

            pushPayload.platform     = Platform.android();
            pushPayload.audience     = Audience.all();
            pushPayload.notification = new Notification().setAlert(msg);
            AndroidNotification androidnotification = new AndroidNotification();

            androidnotification.setTitle(TITLE);
            pushPayload.notification.AndroidNotification = androidnotification;


            try {
                var result = client.SendPush(pushPayload);
                //由于统计数据并非非是即时的,所以等待一小段时间再执行下面的获取结果方法
                //System.Threading.Thread.Sleep(10000);
                //如需查询上次推送结果执行下面的代码
                //var apiResult = client.getReceivedApi(result.msg_id.ToString());
                //var apiResultv3 = client.getReceivedApi_v3(result.msg_id.ToString());
                //如需查询某个messageid的推送结果执行下面的代码
                //var queryResultWithV2 = client.getReceivedApi("1739302794");
                //var querResultWithV3 = client.getReceivedApi_v3("1739302794");
                return(new SimpleResultModel()
                {
                    suc = true, msg = result.msg_id.ToString()
                });
            }
            catch (APIRequestException e) {
                return(new SimpleResultModel()
                {
                    suc = false, msg = "HTTP Status: " + e.Status + ";Error Code: " + e.ErrorCode + ";Error Message: " + e.ErrorMessage
                });
            }
            catch (APIConnectionException e) {
                return(new SimpleResultModel()
                {
                    suc = false, msg = e.message
                });
            }
        }
예제 #4
0
        /// <summary>
        /// IOS文档消息推送
        /// </summary>
        /// <param name="message"></param>
        /// <param name="notificationType"></param>
        /// <param name="tag"></param>
        /// <param name="updateDocumentTypeId"></param>
        /// <param name="updateCity"></param>
        /// <param name="updateDocumentType"></param>
        /// <param name="updateCityId"></param>
        /// <returns></returns>
        private static PushPayload PushAndroid_IosDocument(string message, int notificationType, string tag, int updateDocumentTypeId, string updateCity, string updateDocumentType, int updateCityId)
        {
            var pushPayload = new PushPayload
            {
                platform = Platform.android_ios(),
                audience = tag == "all" ? Audience.all() : Audience.s_tag(tag)
            };

            var notification = new Notification().setAlert(message);

            notification.AndroidNotification =
                new AndroidNotification().setAlert(message)
                .AddExtra("UpdateDocumentTypeID", updateDocumentTypeId)
                .AddExtra("NotificationType", notificationType)
                .AddExtra("message", message);
            notification.IosNotification =
                new IosNotification().setAlert(message)
                .AddExtra("UpdateDocumentTypeID", updateDocumentTypeId)
                .AddExtra("NotificationType", notificationType)
                .AddExtra("message", message)
                .setSound("default")
                .setContentAvailable(true);

            if (!string.IsNullOrEmpty(updateCity))
            {
                notification.IosNotification.AddExtra("UpdateCity", updateCity);
                notification.AndroidNotification.AddExtra("UpdateCity", updateCity);
            }
            if (!string.IsNullOrEmpty(updateDocumentType))
            {
                notification.IosNotification.AddExtra("UpdateDocumentType", updateDocumentType);
                notification.AndroidNotification.AddExtra("UpdateDocumentType", updateDocumentType);
            }
            if (updateCityId > 0)
            {
                notification.IosNotification.AddExtra("UpdateCityId", updateCityId);
                notification.AndroidNotification.AddExtra("UpdateCityId", updateCityId);
            }
            pushPayload.notification = notification;
            return(pushPayload);
        }
예제 #5
0
        public void lackOfParams_platform()
        {
            JsonSerializer jsonSerializer = new JsonSerializer();

            jsonSerializer.DefaultValueHandling = DefaultValueHandling.Ignore;
            JObject payload = new JObject();

            payload.Add("audience", JToken.FromObject(JsonConvert.SerializeObject(Audience.all(), new AudienceConverter())));

            payload.Add("notification", JToken.FromObject(new Notification().setAlert(ALERT), jsonSerializer));

            Console.WriteLine("json string: " + payload.ToString());
            try
            {
                _client.SendPush(payload.ToString());
            }
            catch (APIRequestException e)
            {
                Assert.AreEqual(LACK_OF_PARAMS, e.ErrorCode);
            }
        }
        public static PushPayload PushObject_ios_alert_json()
        {
            PushPayload pushPayload = new PushPayload();

            pushPayload.platform = Platform.all();
            pushPayload.audience = Audience.all();
            var       notification = new Notification();
            Hashtable alert        = new Hashtable();

            alert["title"]               = "JPush Title";
            alert["subtitle"]            = "JPush Subtitle";
            alert["body"]                = "JPush Body";
            notification.IosNotification = new IosNotification()
                                           .setAlert(alert)
                                           .setBadge(5)
                                           .setSound("happy")
                                           .AddExtra("from", "JPush");
            pushPayload.notification = notification;
            pushPayload.message      = Message.content(MSG_CONTENT);
            return(pushPayload);
        }
예제 #7
0
        public static PushPayload PushObject_All_All_Alert(string alert, string title, string alias, string activity = "")
        {
            string[] aliass = alias.Split(',');

            PushPayload pushPayload = new PushPayload();

            pushPayload.platform = Platform.all();
            if (alias == "")
            {
                pushPayload.audience = Audience.all();
            }
            else
            {
                pushPayload.audience = Audience.s_alias(aliass);
            }

            pushPayload.notification = Notification.android(alert, title);
            pushPayload.notification.AndroidNotification.AddExtra("activity", activity);
            pushPayload.notification.AndroidNotification.setBuilderID(2);
            return(pushPayload);
        }
예제 #8
0
        private PushPayload PushSendSmsMessage(string msg, string smsMsg, string[] alias = null)
        {
            var pushPayload = new PushPayload();

            pushPayload.platform = Platform.all();
            if (alias != null)
            {
                pushPayload.audience = Audience.s_alias(alias);
            }
            else
            {
                pushPayload.audience = Audience.all();
            }
            pushPayload.notification = new Notification().setAlert(msg);
            SmsMessage sms_message = new SmsMessage();

            sms_message.setContent(smsMsg);
            sms_message.setDelayTime(DELAY_TIME);
            pushPayload.sms_message = sms_message;
            return(pushPayload);
        }
        public void testNotification()
        {
            int number = ServiceHelper.generateSendno();

            PushPayload payload = new PushPayload();

            payload.platform = Platform.all();
            payload.audience = Audience.all();
            payload.options  = new Options()
            {
                sendno = number
            };
            payload.notification = new Notification().setAlert("alert");
            payload.Check();

            JObject json = new JObject();

            json.Add("platform", JToken.FromObject("all"));
            json.Add("audience", JToken.FromObject("all"));


            JObject noti = new JObject();

            noti.Add("alert", JToken.FromObject("alert"));
            json.Add("notification", noti);

            JObject options = new JObject();

            options.Add("sendno", JToken.FromObject(number));
            options.Add("apns_production", JToken.FromObject(false));
            json.Add("options", options);

            var jSetting = new JsonSerializerSettings();

            jSetting.DefaultValueHandling = DefaultValueHandling.Ignore;
            var jsonString = JsonConvert.SerializeObject(payload, jSetting);
            var jsonObject = json.ToString(Formatting.None);

            Assert.AreEqual(jsonObject, jsonString);
        }
예제 #10
0
        public static void Main(string[] args)
        {
            //init a pushpayload
            PushPayload pushPayload = new PushPayload();

            pushPayload.platform     = Platform.all();
            pushPayload.audience     = Audience.all();
            pushPayload.notification = new Notification().setAlert(ALERT);

            ScheduleClient scheduleclient = new ScheduleClient(app_key, master_secret);

            //init a TriggerPayload
            TriggerPayload triggerConstructor = new TriggerPayload(START, END, TIME_PERIODICAL, TIME_UNIT, FREQUENCY, POINT);
            //init a SchedulePayload
            SchedulePayload schedulepayloadperiodical = new SchedulePayload(NAME, ENABLED, triggerConstructor, pushPayload);

            //PUT the name
            SchedulePayload putschedulepayload = new SchedulePayload();

            putschedulepayload.setName(NAME);

            //the default enabled is true,if you want to change it,you have to set it to false
            try
            {
                var result = scheduleclient.putSchedule(putschedulepayload, SCHEDULE_ID);
                Console.WriteLine(result);
            }
            catch (APIRequestException e)
            {
                Console.WriteLine("Error response from JPush server. Should review and fix it. ");
                Console.WriteLine("HTTP Status: " + e.Status);
                Console.WriteLine("Error Code: " + e.ErrorCode);
                Console.WriteLine("Error Message: " + e.ErrorCode);
            }
            catch (APIConnectionException e)
            {
                Console.WriteLine(e.Message);
            }
        }
예제 #11
0
        private PushPayload GetPushPayload(SnsRequest request)
        {
            PushPayload pushPayload = new PushPayload();

            pushPayload.platform = Platform.all();
            pushPayload.audience = Audience.all();

            pushPayload.options.apns_production = JpushAppApn; //JpushAppApn
            //pushPayload.notification = new Notification().setAlert(request.PushMsg);

            AndroidNotification androidNotification = new AndroidNotification();
            IosNotification     iosNotification     = new IosNotification();

            androidNotification.setAlert(request.PushMsg);
            iosNotification.setAlert(request.PushMsg);
            if (request.PushExtras != null)
            {
                foreach (var item in request.PushExtras)
                {
                    androidNotification.AddExtra(item.Key, item.Value);
                    iosNotification.AddExtra(item.Key, item.Value);
                }
            }

            if ((int)request.PushType != 0)
            {
                androidNotification.AddExtra("type", (int)request.PushType);
                iosNotification.AddExtra("type", (int)request.PushType);
            }

            cn.jpush.api.push.mode.Notification noti = new cn.jpush.api.push.mode.Notification();
            noti.setAndroid(androidNotification);
            noti.setIos(iosNotification);
            //pushPayload.notification = new Notification().setAndroid(noty1);
            //pushPayload.notification = new Notification().setIos(noty2);

            pushPayload.notification = noti;
            return(pushPayload);
        }
예제 #12
0
        public static PushPayload PushObject_All_All_Alert(string[] args)
        {
            PushPayload pushPayload = new PushPayload();

            pushPayload.platform = Platform.all();
            pushPayload.audience = Audience.all();
            pushPayload.message  = Message.content(args[0])
                                   .AddExtras("Type", args[1])
                                   .AddExtras("SheetID", args[2])
                                   .AddExtras("TypeName", args[4])
                                   .AddExtras("SheetTitle", args[5])
                                   .AddExtras("SheetStateCN", args[6])
                                   .AddExtras("IDS", args[3]);
            //pushPayload.message = Message.content("显示内容为推送")
            //                  .AddExtras("Type", 1)
            //                  .AddExtras("SheetID", 76)
            //                  .AddExtras("TypeName", "任务")
            //                  .AddExtras("SheetTitle", "wmt")
            //                  .AddExtras("SheetStateCN","已受理")
            //                  .AddExtras("IDS","10049,10015,10034,10026");
            return(pushPayload);
        }
예제 #13
0
        public bool Push(string message, Dictionary <string, string> paras = null)
        {
            PushPayload pushPayload = new PushPayload();

            pushPayload.platform     = Platform.all();
            pushPayload.audience     = Audience.all();
            pushPayload.notification = new Notification().setAlert(message);
            pushPayload.notification.IosNotification     = new IosNotification().setAlert(message).autoBadge();
            pushPayload.notification.AndroidNotification = new AndroidNotification().setAlert(message);

            if (paras != null)
            {
                foreach (var para in paras)
                {
                    pushPayload.notification.IosNotification.AddExtra(para.Key, para.Value);
                    pushPayload.notification.AndroidNotification.AddExtra(para.Key, para.Value);
                }
            }

            pushPayload.options.apns_production = ApnsProduction;
            try
            {
                JPushClient client = new JPushClient(AppKey, MasterSecret);
                client.SendPush(pushPayload);
                return(true);
            }
            catch (APIRequestException e)
            {
                Logger.Error("Error response from JPush server. Should review and fix it. HTTP Status: " + e.Status +
                             "Error Code: " + e.ErrorCode + "Error Message: " + e.ErrorCode);
                return(false);
            }
            catch (APIConnectionException e)
            {
                Logger.Error(e, "消息推送失败");
                return(false);
            }
        }
예제 #14
0
파일: PushMessage.cs 프로젝트: 15831944/EPC
        /// <summary>
        /// 推送给IOS用户
        /// </summary>
        /// <param name="content">通知内容</param>
        /// <param name="sound">提示音</param>
        /// <param name="dict">额外信息</param>
        /// <returns></returns>
        public static PushPayload PushObject_Ios_All(string content, Dictionary <string, string> dict = null, string sound = "default")
        {
            PushPayload pushPayload = new PushPayload();

            pushPayload.platform = Platform.android_ios();
            pushPayload.audience = Audience.all();
            var notification = new Notification();

            notification.IosNotification = new IosNotification();
            notification.IosNotification.setAlert(content).incrBadge(1).setSound(sound);

            //判断是否有额外信息
            if (dict != null)
            {
                foreach (var item in dict)
                {
                    notification.IosNotification.AddExtra(item.Key, item.Value);
                }
            }

            pushPayload.notification = notification;
            pushPayload.message      = Message.content(content);
            return(pushPayload);
        }
예제 #15
0
        public void sendAlert_ios()
        {
            //PushPayload payload = PushPayload.newBuilder()
            //        .setPlatform(Platform.ios())
            //        .setAudience(Audience.all())
            //        .setNotification(Notification.newBuilder()
            //                .setAlert("alert")
            //                .addPlatformNotification(IosNotification.alert("ios alert"))
            //                .build())
            //        .build();
            PushPayload payload = new PushPayload();

            payload.platform = Platform.all();
            payload.audience = Audience.all();

            payload.notification = new Notification()
                                   .setAlert("alert")

                                   .setIos(new IosNotification().setAlert("ios alert"));

            var result = _client.SendPush(payload);

            Assert.IsTrue(result.isResultOK());
        }
        public void sendNotification_alert_json()
        {
            JObject json = new JObject();

            json.Add("key1", "value1");
            json.Add("key2", true);

            String alert = json.ToString();

            Console.WriteLine(alert);

            PushPayload payload = new PushPayload();

            payload.platform     = Platform.all();
            payload.audience     = Audience.all();
            payload.notification = new Notification()
                                   .setAndroid(new AndroidNotification()
                                               .setAlert(alert)
                                               .setTitle("title"));

            var result = _client.SendPush(payload);

            Assert.IsTrue(result.isResultOK());
        }
예제 #17
0
        private void TimingJush(object o)
        {
            if (_j_time == null)
            {
                _j_time = new JushTiming_DAL();
            }
            if (payload == null)
            {
                payload = new PushPayload();
            }

            list = _j_time.PushTiming();
            if (list.Count > 0)
            {
                foreach (var item in list)
                {
                    long MessageID = 0;
                    int  ErrorCode = 0;
                    switch (item.SendPlatform)
                    {
                    case 1: payload.platform = Platform.android();
                        break;

                    case 2: payload.platform = Platform.ios();
                        break;

                    case 3: payload.platform = Platform.android_ios();
                        break;

                    default:
                        break;
                    }
                    switch (item.SendAudience)
                    {
                    case 0: payload.audience = Audience.all();
                        break;

                    case 1:
                        string[] tagArray = item.AudienceName.Split(',');
                        payload.audience = Audience.s_tag(tagArray);
                        break;

                    case 2: payload.audience = Audience.s_alias(item.AudienceName);
                        break;

                    case 3: payload.audience = Audience.s_registrationId(item.AudienceName);
                        break;

                    default:
                        break;
                    }
                    string strJosn = item.Extras == null ? "" : item.Extras;
                    if (strJosn.Length > 0)
                    {
                        data = strJosn.JsonToDocuments();
                        if (data.Count > 0)
                        {
                            android = new AndroidNotification();
                            ios     = new IosNotification();
                            foreach (var tt in data)
                            {
                                android.AddExtra(tt.Key, tt.Value.ToString());
                                ios.AddExtra(tt.Key, tt.Value.ToString());
                            }
                        }
                        payload.notification = new Notification().setAlert(item.Content).setAndroid(android).setIos(ios);
                    }
                    else
                    {
                        payload.notification = new Notification().setAlert(item.Content);
                    }

                    try
                    {
                        var result = client.SendPush(payload);
                        //由于统计数据并非非是即时的,所以等待一小段时间再执行下面的获取结果方法
                        System.Threading.Thread.Sleep(5000);
                        MessageID = Convert.ToInt64(result.msg_id);
                        //记录日志
                        Config.PushLogAPP("APP定时发送", "所有", item.Content);
                    }
                    catch (Jpush.api.common.APIRequestException ee)
                    {
                        MessageID = ee.MsgId;
                        ErrorCode = ee.ErrorCode;


                        string.Format("\r\n>>>APP定时发送 推送异常:{0},time:{1}",
                                      ee.Message,
                                      DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
                                      ).WriteLog("ERROR");
                    }

                    _j_time.PushTimingUpdateStatu(MessageID, ErrorCode, item.Id);
                    android = null;
                    ios     = null;
                }
            }
        }
예제 #18
0
        public override object ReadJson(
            JsonReader reader,
            Type objectType,
            object existingValue,
            JsonSerializer serializer)
        {
            Audience audience = Audience.all();

            if ((int)reader.TokenType == 11)
            {
                return((object)null);
            }
            if ((int)reader.TokenType == 9)
            {
                audience.allAudience = reader.Value.ToString();
            }
            else if ((int)reader.TokenType == 1)
            {
                audience.allAudience = (string)null;
                Dictionary <string, HashSet <string> > dictionary = new Dictionary <string, HashSet <string> >();
                string           key       = "key";
                HashSet <string> stringSet = (HashSet <string>)null;
                while (reader.Read())
                {
                    Debug.WriteLine("Type:{0},Path:{1}", (object)reader.TokenType, (object)reader.Path);
                    var tokenType = (int)reader.TokenType;
                    if (tokenType <= 9)
                    {
                        switch (tokenType - 1)
                        {
                        case 0:
                        case 2:
                            break;

                        case 1:
                            stringSet = new HashSet <string>();
                            break;

                        case 3:
                            key = reader.Value.ToString();
                            break;

                        default:
                            if (tokenType == 9)
                            {
                                stringSet.Add(reader.Value.ToString());
                                break;
                            }
                            break;
                        }
                    }
                    else
                    {
                        if (tokenType == 13)
                        {
                            return((object)audience);
                        }
                        if (tokenType == 14)
                        {
                            dictionary.Add(key, stringSet);
                        }
                    }
                }
                audience.dictionary = dictionary;
            }
            return((object)audience);
        }
예제 #19
0
파일: PushMessage.cs 프로젝트: 15831944/EPC
 /// <summary>
 /// 推送到ios和andriod平台
 /// </summary>
 /// <param name="content">通知内容</param>
 /// <param name="title">通知标题</param>
 /// <param name="dict">额外信息</param>
 /// <param name="isProduction">是生产环境</param>
 /// <returns></returns>
 public static PushPayload PushObject_AndroidAndIos_All(string content, string title, bool isProduction = true, Dictionary <string, string> dict = null)
 {
     return(PushObject_AndroidAndIos_Audience(content, title, Audience.all(), isProduction, dict));
 }
예제 #20
0
 /// <summary>
 /// 推送消息
 /// </summary>
 /// <param name="alert">弹窗</param>
 /// <param name="content">消息内容</param>
 /// <param name="extras">参数</param>
 /// <param name="targets">目标(istag=true:角色表FID  istag=false:用户表FNumber)</param>
 /// <param name="type">推送目标</param>
 public void Push(string alert, PushParam content,
                  string[] targets, PushType type = PushType.Alias)
 {
     try
     {
         JPushClient client  = new JPushClient(app_key, master_secret);
         PushPayload payload = new PushPayload();
         payload.platform = Platform.android_ios();
         payload.options.apns_production = true;
         if (type == PushType.Tag)
         {
             payload.audience = Audience.s_tag(targets);
         }
         else if (type == PushType.Alias)
         {
             payload.audience = Audience.s_alias(targets);
         }
         else if (type == PushType.All)
         {
             payload.audience = Audience.all();
         }
         var message = Message.content(content.Text);
         if (!string.IsNullOrEmpty(alert))
         {
             var notification = new Notification().setAlert(alert);
             notification.IosNotification = new IosNotification();
             notification.IosNotification.incrBadge(1);
             notification.IosNotification.setContentAvailable(true);
             notification.IosNotification.setSound("happy");
             notification.AndroidNotification = new AndroidNotification();
             var extras = content.ToDictionary();
             if (extras != null)
             {
                 foreach (var i in extras)
                 {
                     message.AddExtras(i.Key, i.Value);
                     notification.AndroidNotification.AddExtra(i.Key, i.Value);
                     notification.IosNotification.AddExtra(i.Key, i.Value);
                 }
             }
             payload.notification = notification.Check();
         }
         payload.message = message.Check();
         var result = client.SendPush(payload);
     }
     catch (APIRequestException e)
     {
         StringBuilder builder = new StringBuilder();
         builder.Append("Error response from JPush server. Should review and fix it. ");
         builder.Append("HTTP Status: " + e.Status);
         builder.Append("Error Code: " + e.ErrorCode);
         builder.Append("Error Message: " + e.ErrorCode);
         //throw new Exception(builder.ToString());
     }
     catch (APIConnectionException e)
     {
         StringBuilder builder = new StringBuilder();
         builder.Append("Error response from JPush server. Should review and fix it. ");
         builder.Append("Message: " + e.Message);
         builder.Append("StackTrace: " + e.StackTrace);
         //throw new Exception(builder.ToString());
     }
     catch (Exception)
     {
     }
 }
예제 #21
0
 public static string doPushMessage(string title, Dictionary <string, object> extras, string[] cids = null, string sendtype = "android", string msg_content = "", int PushAPP = 1, bool IsToAll = false)
 {
     try
     {
         if (cids == null && !IsToAll)
         {
             return("没有接收用户");
         }
         PushPayload pushPayload = new PushPayload();
         //pushPayload.notification = new Notification();
         if (IsToAll)
         {
             pushPayload.audience = Audience.all();
         }
         else
         {
             pushPayload.audience = Audience.s_registrationId(cids);
         }
         pushPayload.message = Message.content(msg_content);
         //pushPayload.message.setTitle(title);
         pushPayload.message.setContentType("text");
         foreach (var item in extras)
         {
             pushPayload.message.AddExtras(item.Key, item.Value.ToString());
         }
         pushPayload.message.AddExtras("sound", "widget://res/newmessage.mp3");
         var notification = new Notification();
         notification.setAlert(msg_content);
         if (sendtype.Equals("android"))
         {
             pushPayload.platform             = Platform.android();
             notification.AndroidNotification = new AndroidNotification();
             //notification.AndroidNotification.setAlert(msg_content);
             foreach (var item in extras)
             {
                 notification.AndroidNotification.AddExtra(item.Key, item.Value.ToString());
             }
             notification.AndroidNotification.AddExtra("sound", "widget://res/newmessage.mp3");
             //pushPayload.notification = notification;
         }
         else if (sendtype.Equals("ios"))
         {
             pushPayload.platform         = Platform.ios();
             notification.IosNotification = new IosNotification();
             notification.IosNotification.setBadge(1);
             notification.IosNotification.setSound("widget://res/newmessage.mp3");
             notification.IosNotification.setAlert(msg_content);
             foreach (var item in extras)
             {
                 notification.IosNotification.AddExtra(item.Key, item.Value.ToString());
             }
             pushPayload.notification = notification;
         }
         pushPayload.options.apns_production = true;
         pushPayload.options.time_to_live    = 3600;
         var    config = new Utility.SiteConfig();
         string key    = string.Empty;
         string secret = string.Empty;
         if (PushAPP == 1)
         {
             key    = config.JPushKey_User;
             secret = config.JPushMasterSecret_User;
         }
         else if (PushAPP == 2)
         {
             key    = config.JPushKey_Customer;
             secret = config.JPushMasterSecret_Customer;
         }
         else if (PushAPP == 3)
         {
             key    = config.JPushKey_Business;
             secret = config.JPushMasterSecret_Business;
         }
         var client   = new JPushClient(key, secret);
         var response = client.SendPush(pushPayload);
         return("code:" + response.ResponseResult.responseCode + " content:" + response.ResponseResult.responseContent);
     }
     catch (Exception ex)
     {
         Utility.LogHelper.WriteError("Jpush.JpushAPI", "PushMessage", ex);
         return(ex.Message);
     }
 }
        static void Main(string[] args)
        {
            //init a pushpayload
            PushPayload pushPayload = new PushPayload();

            pushPayload.platform     = Platform.all();
            pushPayload.audience     = Audience.all();
            pushPayload.notification = new Notification().setAlert(ALERT);

            ScheduleClient scheduleclient = new ScheduleClient(app_key, master_secret);

            //init a TriggerPayload
            TriggerPayload triggerConstructor = new TriggerPayload(START, END, TIME_PERIODICAL, TIME_UNIT, FREQUENCY, POINT);
            //init a SchedulePayload
            SchedulePayload schedulepayloadperiodical = new SchedulePayload(NAME, ENABLED, triggerConstructor, pushPayload);

            try
            {
                var result = scheduleclient.sendSchedule(schedulepayloadperiodical);
                //由于统计数据并非非是即时的,所以等待一小段时间再执行下面的获取结果方法
                System.Threading.Thread.Sleep(10000);
                Console.WriteLine(result);
                //保留这里获取的schedule_id,作为后面删除schedule的参数,如果不想删除这个可以删掉这一行,另外设置一个schedule_id
                schedule_id = result.schedule_id;
            }
            catch (APIRequestException e)
            {
                Console.WriteLine("Error response from JPush server. Should review and fix it. ");
                Console.WriteLine("HTTP Status: " + e.Status);
                Console.WriteLine("Error Code: " + e.ErrorCode);
                Console.WriteLine("Error Message: " + e.ErrorCode);
            }
            catch (APIConnectionException e)
            {
                Console.WriteLine(e.Message);
            }


            SchedulePayload schedulepayloadsingle = new SchedulePayload();
            TriggerPayload  triggersingle         = new TriggerPayload(TIME);

            //SchedulePayload schedulepayloadsingle = new SchedulePayload();
            schedulepayloadsingle.setPushPayload(pushPayload);
            schedulepayloadsingle.setTrigger(triggersingle);
            schedulepayloadsingle.setName(NAME);
            schedulepayloadsingle.setEnabled(ENABLED);

            try
            {
                var result = scheduleclient.sendSchedule(schedulepayloadsingle);
                //由于统计数据并非非是即时的,所以等待一小段时间再执行下面的获取结果方法
                System.Threading.Thread.Sleep(10000);
                Console.WriteLine(result);
                //保留这里获取的schedule_id,作为后面删除schedule的参数,如果不想删除这个可以删掉这一行,另外设置一个schedule_id
                schedule_id = result.schedule_id;
            }
            catch (APIRequestException e)
            {
                Console.WriteLine("Error response from JPush server. Should review and fix it. ");
                Console.WriteLine("HTTP Status: " + e.Status);
                Console.WriteLine("Error Code: " + e.ErrorCode);
                Console.WriteLine("Error Message: " + e.ErrorCode);
            }
            catch (APIConnectionException e)
            {
                Console.WriteLine(e.Message);
            }


            //get schedule
            try
            {
                var result = scheduleclient.getSchedule(PAGEID);
                //由于统计数据并非非是即时的,所以等待一小段时间再执行下面的获取结果方法
                System.Threading.Thread.Sleep(10000);
                Console.WriteLine(result.schedules[0].name);

                //if the test Schedule is too much,delete it

                /*
                 * for (int counter = 0; counter <= 40; counter++) {
                 *  scheduleclient.deleteSchedule(result.schedules[counter].schedule_id);
                 * }
                 */
                Console.WriteLine(result.schedules);
                Console.WriteLine(result);
            }
            catch (APIRequestException e)
            {
                Console.WriteLine("Error response from JPush server. Should review and fix it. ");
                Console.WriteLine("HTTP Status: " + e.Status);
                Console.WriteLine("Error Code: " + e.ErrorCode);
                Console.WriteLine("Error Message: " + e.ErrorCode);
            }
            catch (APIConnectionException e)
            {
                Console.WriteLine(e.Message);
            }


            //get schedule by id
            try
            {
                var result = scheduleclient.getScheduleById(PUT_SCHEDULE_ID);
                //由于统计数据并非非是即时的,所以等待一小段时间再执行下面的获取结果方法
                System.Threading.Thread.Sleep(10000);
                Console.WriteLine(result.name);
                Console.WriteLine(result);
            }
            catch (APIRequestException e)
            {
                Console.WriteLine("Error response from JPush server. Should review and fix it. ");
                Console.WriteLine("HTTP Status: " + e.Status);
                Console.WriteLine("Error Code: " + e.ErrorCode);
                Console.WriteLine("Error Message: " + e.ErrorCode);
            }
            catch (APIConnectionException e)
            {
                Console.WriteLine(e.Message);
            }

            //put the name

            SchedulePayload putschedulepayload = new SchedulePayload();

            putschedulepayload.setName(NAME);

            /*
             * putschedulepayload.setPushPayload(null);
             * putschedulepayload.setTrigger(null);
             */
            //the default enabled is true,if you want to change it,you have to set it to false
            try
            {
                var result = scheduleclient.putSchedule(putschedulepayload, schedule_id);
                //由于统计数据并非非是即时的,所以等待一小段时间再执行下面的获取结果方法
                System.Threading.Thread.Sleep(10000);
                Console.WriteLine(result);
            }
            catch (APIRequestException e)
            {
                Console.WriteLine("Error response from JPush server. Should review and fix it. ");
                Console.WriteLine("HTTP Status: " + e.Status);
                Console.WriteLine("Error Code: " + e.ErrorCode);
                Console.WriteLine("Error Message: " + e.ErrorCode);
            }
            catch (APIConnectionException e)
            {
                Console.WriteLine(e.Message);
            }


            //deleteSchedule
            try
            {
                //删除的是第一次创建的schedule_id,如果要保留第一次创建的,请重新传入schedule_id
                var result = scheduleclient.deleteSchedule(schedule_id);
                //由于统计数据并非非是即时的,所以等待一小段时间再执行下面的获取结果方法
                System.Threading.Thread.Sleep(10000);
                Console.WriteLine(result);
            }
            catch (APIRequestException e)
            {
                Console.WriteLine("Error response from JPush server. Should review and fix it. ");
                Console.WriteLine("HTTP Status: " + e.Status);
                Console.WriteLine("Error Code: " + e.ErrorCode);
                Console.WriteLine("Error Message: " + e.ErrorCode);
            }
            catch (APIConnectionException e)
            {
                Console.WriteLine(e.Message);
            }

            //
            SchedulePayload schedulepayloadSet = new SchedulePayload();
            TriggerPayload  triggerSet         = new TriggerPayload();

            triggerSet.setStart(START);
            triggerSet.setEnd(END);
            triggerSet.setTime(TIME_PERIODICAL);
            triggerSet.setTime_unit(TIME_UNIT);
            triggerSet.setFrequency(FREQUENCY);
            triggerSet.setPoint(POINT);

            schedulepayloadSet.setPushPayload(pushPayload);
            schedulepayloadSet.setTrigger(triggerSet);
            schedulepayloadSet.setName(NAME);
            schedulepayloadSet.setEnabled(ENABLED);

            try
            {
                var result = scheduleclient.sendSchedule(schedulepayloadSet);
                //由于统计数据并非非是即时的,所以等待一小段时间再执行下面的获取结果方法
                System.Threading.Thread.Sleep(10000);
                Console.WriteLine(result);
                //保留这里获取的schedule_id,作为后面删除schedule的参数,如果不想删除这个可以删掉这一行,另外设置一个schedule_id
                schedule_id1 = result.schedule_id;
            }
            catch (APIRequestException e)
            {
                Console.WriteLine("Error response from JPush server. Should review and fix it. ");
                Console.WriteLine("HTTP Status: " + e.Status);
                Console.WriteLine("Error Code: " + e.ErrorCode);
                Console.WriteLine("Error Message: " + e.ErrorCode);
            }
            catch (APIConnectionException e)
            {
                Console.WriteLine(e.Message);
            }
        }
예제 #23
0
 //广播发送
 private Audience broadcast()
 {
     return(Audience.all());
 }
예제 #24
0
        /// <summary>
        /// 极光推送处理方法
        /// </summary>
        protected override void Handler()
        {
            pushPayload          = new PushPayload();
            pushPayload.audience = PushModel.Audience == null?Audience.all() : GetAudience();

            pushPayload.options.apns_production = PushModel.APNSIsProduction;

            if (PushModel.NotificationWay == NotificationWay.All)
            {
                pushPayload.message      = Message.content(PushModel.Message);
                pushPayload.notification = new Notification().setAlert(PushModel.Message);
            }
            else if (PushModel.NotificationWay == NotificationWay.APNs)
            {
                pushPayload.notification = new Notification().setAlert(PushModel.Message);
            }
            else
            {
                pushPayload.message = Message.content(PushModel.Message);
            }

            // 平台判断
            switch (PushModel.Platform)
            {
            case 1:     // Android
                if (pushPayload.notification != null)
                {
                    pushPayload.notification.AndroidNotification = new AndroidNotification();
                }
                pushPayload.platform = Platform.android();
                break;

            case 2:     // iOS
                if (pushPayload.notification != null)
                {
                    pushPayload.notification.IosNotification = new IosNotification();
                }
                pushPayload.platform = Platform.ios();
                break;

            case 3:     // Android + iOS
                if (pushPayload.notification != null)
                {
                    pushPayload.notification.IosNotification     = new IosNotification();
                    pushPayload.notification.AndroidNotification = new AndroidNotification();
                }
                pushPayload.platform = Platform.android_ios();
                break;

            case 4:     // WinPhone
                if (pushPayload.notification != null)
                {
                    pushPayload.notification.WinphoneNotification = new WinphoneNotification();
                }
                pushPayload.platform = Platform.winphone();
                break;

            case 5:     // Android + WinPhone
                if (pushPayload.notification != null)
                {
                    pushPayload.notification.AndroidNotification  = new AndroidNotification();
                    pushPayload.notification.WinphoneNotification = new WinphoneNotification();
                }
                pushPayload.platform = Platform.android_winphone();
                break;

            case 6:     // iOS + WinPhone
                if (pushPayload.notification != null)
                {
                    pushPayload.notification.IosNotification      = new IosNotification();
                    pushPayload.notification.WinphoneNotification = new WinphoneNotification();
                }
                pushPayload.platform = Platform.ios_winphone();
                break;

            default:    // 全平台
                if (pushPayload.notification != null)
                {
                    pushPayload.notification.IosNotification      = new IosNotification();
                    pushPayload.notification.AndroidNotification  = new AndroidNotification();
                    pushPayload.notification.WinphoneNotification = new WinphoneNotification();
                }
                pushPayload.platform = Platform.all();
                break;
            }

            // APNs
            if (pushPayload.notification != null)
            {
                // iOS设置声音、消息内容、角标、额外信息
                if (pushPayload.notification.IosNotification != null)
                {
                    pushPayload.notification.IosNotification.setSound(PushModel.Sound);
                    pushPayload.notification.IosNotification.AddExtra("command", PushModel.Command);

                    if (PushModel.Badge <= 1)
                    {
                        pushPayload.notification.IosNotification.incrBadge(1);
                    }
                    else
                    {
                        pushPayload.notification.IosNotification.setBadge(PushModel.Badge);
                    }

                    // 额外信息
                    if (PushModel.Extras.Keys.Count != 0)
                    {
                        foreach (var item in PushModel.Extras.Keys)
                        {
                            pushPayload.notification.IosNotification.AddExtra(PushModel.LowerExtraKey ? item.ToLower() : item, PushModel.Extras[item]);
                        }
                    }
                }

                // Android设置标题、消息内容、额外信息
                if (pushPayload.notification.AndroidNotification != null)
                {
                    pushPayload.notification.AndroidNotification.AddExtra("command", PushModel.Command);
                    pushPayload.notification.AndroidNotification.setBig_text(PushModel.Message);

                    if (PushModel.Title != null)
                    {
                        pushPayload.notification.AndroidNotification.setTitle(PushModel.Title);
                    }

                    // 额外信息
                    if (PushModel.Extras.Keys.Count != 0)
                    {
                        foreach (var item in PushModel.Extras.Keys)
                        {
                            pushPayload.notification.AndroidNotification.AddExtra(PushModel.LowerExtraKey ? item.ToLower() : item, PushModel.Extras[item]);
                        }
                    }
                }

                // WinPhone设置标题、消息内容、额外信息
                if (pushPayload.notification.WinphoneNotification != null)
                {
                    pushPayload.notification.WinphoneNotification.AddExtra("command", PushModel.Command);

                    if (PushModel.Title != null)
                    {
                        pushPayload.notification.WinphoneNotification.setTitle(PushModel.Title);
                    }

                    // 额外信息
                    if (PushModel.Extras.Keys.Count != 0)
                    {
                        foreach (var item in PushModel.Extras.Keys)
                        {
                            pushPayload.notification.WinphoneNotification.AddExtra(PushModel.LowerExtraKey ? item.ToLower() : item, PushModel.Extras[item]);
                        }
                    }
                }
            }

            // 消息透传
            if (pushPayload.message != null)
            {
                pushPayload.message.AddExtras("command", PushModel.Command);

                if (PushModel.Title != null)
                {
                    pushPayload.message.setTitle(PushModel.Title);
                }

                // 额外信息
                if (PushModel.Extras.Keys.Count != 0)
                {
                    foreach (var item in PushModel.Extras.Keys)
                    {
                        pushPayload.message.AddExtras(PushModel.LowerExtraKey ? item.ToLower() : item, PushModel.Extras[item]);
                    }
                }
            }
        }
예제 #25
0
    //tag-标签,alias-别名,title-标题,content-内容,type-类型,subtitle-副标题
    public static void push(string tag, string alias, string title, string subtitle, string content, string type)
    {
        //string[] tags = tag.Split(',');
        JPushClient client;

        if (type == "shop")
        {
            client = new JPushClient(sysconf.jpush_app_key_shop, sysconf.jpush_master_secret_shop);
        }
        else
        {
            client = new JPushClient(sysconf.jpush_app_key, sysconf.jpush_master_secret);
        }
        Audience target;

        switch (type)
        {
        case "alert":
            target = Audience.all();
            break;

        case "verified":
            target = Audience.s_alias(alias);
            break;

        case "order":
            string[] tags = tag.Split(',');
            target = Audience.s_tag_and(tags);
            break;

        case "finish":
            target = Audience.s_alias(alias);
            break;

        case "shop":
            target = Audience.s_alias(alias);
            break;

        default:
            target = Audience.all();
            break;
        }
        PushPayload payload = ios(target, title, content, type, subtitle);

        try
        {
            var result = client.SendPush(payload);
        }
        catch
        {
            //
        }
        payload = android(target, title, content, type, subtitle);
        try
        {
            var result = client.SendPush(payload);
        }
        catch
        {
            //
        }
    }