cn.jpush.api.push.mode.PushPayload.IsIosExceedLength C# (CSharp) Method

IsIosExceedLength() public method

public IsIosExceedLength ( ) : bool
return bool
        public bool IsIosExceedLength()
        {
            if (this.notification != null)
            {
                if (this.notification.IosNotification != null)
                {
                    var iosJson = JsonConvert.SerializeObject(this.notification.IosNotification, jSetting);
                    if (iosJson != null)
                    {
                        return UTF8Encoding.UTF8.GetBytes(iosJson).Length > MAX_IOS_PAYLOAD_LENGTH;
                    }
                }
                else
                {
                    if (!(this.notification.alert == null))
                    {
                        string jsonText;
                        using (StringWriter sw = new StringWriter())
                        {
                            JsonWriter writer = new JsonTextWriter(sw);
                            writer.WriteValue(this.notification.alert);
                            writer.Flush();
                            jsonText = sw.GetStringBuilder().ToString();
                        }
                        return UTF8Encoding.UTF8.GetBytes(jsonText).Length > MAX_IOS_PAYLOAD_LENGTH;
                    }
                    else
                    {
                        // No iOS Payload
                    }
                }

            }
            return false;
        }

Usage Example

  public void testIosExceed2() 
   {
       PushPayload payload = new PushPayload();
       payload.platform = Platform.all();
       payload.audience = Audience.all();
       payload.notification = new Notification();
       payload.notification.IosNotification = new IosNotification().setAlert(LONG_TEXT_1);
       payload.Check();
       
       Debug.WriteLine("Size: " + UTF8Encoding.UTF8.GetBytes(LONG_TEXT_1).Length);
       Assert.IsTrue(payload.IsIosExceedLength(), "Should exceed - " + UTF8Encoding.UTF8.GetBytes(LONG_TEXT_1).Length); 
 }