เรื่องมีอยู่ว่าผมทำการเรียกส่ง post http
ให้ส่งไปเรื่อยๆ ทุกๆ 5 วิ ด้วย โค้ด
แต่ติดปัญหาตรงที่ รอบที่ ทำการส่งในรอบที่ 3 มันจะนานมาก
ใครทราบสาเหตุบอกทีครับ
นี้เป็นฟังก์ชั่นส่งครับ
private void button3_Click(object sender, EventArgs e)
{
MobileAppNotificationEntity mne = new MobileAppNotificationEntity();
MobileAppNotificationService mns = new MobileAppNotificationService(JayPush.Properties.Settings.Default.ConnectionString);
List<MobileAppNotificationEntity> MobileAppNotificationService = new List<MobileAppNotificationEntity>

);
if (mns.PartialList(ref MobileAppNotificationService, MobileAppNotificationEntity.Process.Pending, MobileAppNotificationEntity.OrderKey.Id))
{
foreach (var item in MobileAppNotificationService)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create("
https://api.ionic.io/push/notifications");
httpWebRequest.Headers.Add("Authorization", "Bearer " + item.Registeration.Application.TokenId);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var stream = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"tokens\": [\"" + item.Registeration.TokenId + "\"],\"uuid\": \"" + item.Registeration.Uuid + "\",\"notification\": {\"message\":\"" + item.Message + "\",\"ios\": {\"message\":\"" + item.Message + "\"}, \"android\": {\"message\":\"" + item.Message + "\"}},\"profile\": \"" + item.Registeration.Application.Profile + "\"}";
stream.Write(json);
}
var response = (HttpWebResponse)httpWebRequest.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
TextDisplay += "{\"tokens\": [\"" + item.Registeration.TokenId + "\"],\"uuid\": \"" + item.Registeration.Uuid + "\",\"notification\": {\"message\":\"" + item.Message + "\",\"ios\": {\"message\":\"" + item.Message + "\"}, \"android\": {\"message\":\"" + item.Message + "\"}},\"profile\": \"" + item.Registeration.Application.Profile + "\"}";
TextDisplay += " ";
}
richTextBox1.Text += TextDisplay;
}
นี่เป็นฟังก์ชั่นเวลาครับ
private void button4_Click(object sender, EventArgs e)
{
Timer t = new Timer();
t.Interval = 5000;//1000 มิลิวินาที เท่ากับ 1 วินาที
t.Tick += new EventHandler(button3_Click);
t.Enabled = true;
//อย่าลืมถ้าจะให้ timer ทำงานตอนไหน
t.Start();
//อย่าลืมถ้าจะให้ timer หยุดทำงานตอนไหน
// t.Stop();
}
ปล. ถ้าส่งแบบไม่เรียกทางเซอวิส จะส่งได้ปกติครับ ใน 5 วิ ส่ง 1 ครั้งครับ
อันนี้ส่งแบบ List
ตัวนี้เป็น list ครับ
public class Notification
{
public string uuid { get; set; }
public string TokenDevice { get; set; }
public string TokenApp { get; set; }
public string NMessage { get; set; }
public string IMessage { get; set; }
public string AMessage { get; set; }
public string Profile { get; set; }
}
ตัวนี้เป็นฟังก์ชั่นในการส่งครับ
private void button5_Click(object sender, EventArgs e)
{
List<Notification> Notification = new List<Notification>

);
//var Notification = new List<Notification>

);
Notification.Add(new Notification { TokenDevice = "d1b3jy0_Fg4:APA91bHBOiutTfPcNHZcFYPtAPvByQNt8AcjD2B_3A3k0CoSRdDtuyyU1S9AGtLR3hM-JG6IGCWbLNywA9KtDSxaicOi2DEZ5F8fY9dKfH8P94JywMnflpDLaNed55z59bR", TokenApp = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVkiOiJjNmJjYmEyZi1iM2Q5LTQ4OGEtYWFhNy0xYmQ4ZDI0YjE0MzgifQ.es1iUVGbq6GR6LEUBbsxIU3DqJrdtw68ZJRHDyMsSrM", NMessage = "ผ่าน", IMessage = "ผ่าน", AMessage = "ผ่าน", Profile = "mappointasia" });
foreach (var item in Notification)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create("
https://api.ionic.io/push/notifications");
httpWebRequest.Headers.Add("Authorization", "Bearer " + item.TokenApp);
// httpWebRequest.Headers.Add("Content-Type", "application/json");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var stream = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"tokens\": [\"" + item.TokenDevice + "\"],\"notification\": {\"message\":\"" + item.NMessage + "\",\"ios\": {\"message\":\"" + item.IMessage + "\"}, \"android\": {\"message\":\"" + item.AMessage + "\"}},\"profile\": \"" + item.Profile + "\"}";
stream.Write(json);
// stream.Flush();
// stream.Close();
}
//ตอนเรียกส่ง
var response = (HttpWebResponse)httpWebRequest.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
TextDisplay += "{\"tokens\": [\"" + item.TokenDevice + "\"],\"notification\": {\"message\":\"" + item.NMessage + "\",\"ios\": {\"message\":\"" + item.IMessage + "\"}, \"android\": {\"message\":\"" + item.AMessage + "\"}},\"profile\": \"" + item.Profile + "\"}";
TextDisplay += " ";
}
}
ตัวนี้เป็นฟังก์ชั่นเวลาที่เริ่มทำงานครับ
private void button4_Click(object sender, EventArgs e)
{
Timer t = new Timer();
t.Interval = 5000;//1000 มิลิวินาที เท่ากับ 1 วินาที
t.Tick += new EventHandler(button5_Click);
t.Enabled = true;
//อย่าลืมถ้าจะให้ timer ทำงานตอนไหน
t.Start();
//อย่าลืมถ้าจะให้ timer หยุดทำงานตอนไหน
// t.Stop();
}
ใครพอทราบสาเหตุโปรดช่วยหน่อยครับ
Post Http request & Service (c#) ช่วยด้วยจ้า
ให้ส่งไปเรื่อยๆ ทุกๆ 5 วิ ด้วย โค้ด
แต่ติดปัญหาตรงที่ รอบที่ ทำการส่งในรอบที่ 3 มันจะนานมาก
ใครทราบสาเหตุบอกทีครับ
นี้เป็นฟังก์ชั่นส่งครับ
private void button3_Click(object sender, EventArgs e)
{
MobileAppNotificationEntity mne = new MobileAppNotificationEntity();
MobileAppNotificationService mns = new MobileAppNotificationService(JayPush.Properties.Settings.Default.ConnectionString);
List<MobileAppNotificationEntity> MobileAppNotificationService = new List<MobileAppNotificationEntity>
if (mns.PartialList(ref MobileAppNotificationService, MobileAppNotificationEntity.Process.Pending, MobileAppNotificationEntity.OrderKey.Id))
{
foreach (var item in MobileAppNotificationService)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.ionic.io/push/notifications");
httpWebRequest.Headers.Add("Authorization", "Bearer " + item.Registeration.Application.TokenId);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var stream = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"tokens\": [\"" + item.Registeration.TokenId + "\"],\"uuid\": \"" + item.Registeration.Uuid + "\",\"notification\": {\"message\":\"" + item.Message + "\",\"ios\": {\"message\":\"" + item.Message + "\"}, \"android\": {\"message\":\"" + item.Message + "\"}},\"profile\": \"" + item.Registeration.Application.Profile + "\"}";
stream.Write(json);
}
var response = (HttpWebResponse)httpWebRequest.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
TextDisplay += "{\"tokens\": [\"" + item.Registeration.TokenId + "\"],\"uuid\": \"" + item.Registeration.Uuid + "\",\"notification\": {\"message\":\"" + item.Message + "\",\"ios\": {\"message\":\"" + item.Message + "\"}, \"android\": {\"message\":\"" + item.Message + "\"}},\"profile\": \"" + item.Registeration.Application.Profile + "\"}";
TextDisplay += " ";
}
richTextBox1.Text += TextDisplay;
}
นี่เป็นฟังก์ชั่นเวลาครับ
private void button4_Click(object sender, EventArgs e)
{
Timer t = new Timer();
t.Interval = 5000;//1000 มิลิวินาที เท่ากับ 1 วินาที
t.Tick += new EventHandler(button3_Click);
t.Enabled = true;
//อย่าลืมถ้าจะให้ timer ทำงานตอนไหน
t.Start();
//อย่าลืมถ้าจะให้ timer หยุดทำงานตอนไหน
// t.Stop();
}
ปล. ถ้าส่งแบบไม่เรียกทางเซอวิส จะส่งได้ปกติครับ ใน 5 วิ ส่ง 1 ครั้งครับ
อันนี้ส่งแบบ List
ตัวนี้เป็น list ครับ
public class Notification
{
public string uuid { get; set; }
public string TokenDevice { get; set; }
public string TokenApp { get; set; }
public string NMessage { get; set; }
public string IMessage { get; set; }
public string AMessage { get; set; }
public string Profile { get; set; }
}
ตัวนี้เป็นฟังก์ชั่นในการส่งครับ
private void button5_Click(object sender, EventArgs e)
{
List<Notification> Notification = new List<Notification>
//var Notification = new List<Notification>
Notification.Add(new Notification { TokenDevice = "d1b3jy0_Fg4:APA91bHBOiutTfPcNHZcFYPtAPvByQNt8AcjD2B_3A3k0CoSRdDtuyyU1S9AGtLR3hM-JG6IGCWbLNywA9KtDSxaicOi2DEZ5F8fY9dKfH8P94JywMnflpDLaNed55z59bR", TokenApp = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVkiOiJjNmJjYmEyZi1iM2Q5LTQ4OGEtYWFhNy0xYmQ4ZDI0YjE0MzgifQ.es1iUVGbq6GR6LEUBbsxIU3DqJrdtw68ZJRHDyMsSrM", NMessage = "ผ่าน", IMessage = "ผ่าน", AMessage = "ผ่าน", Profile = "mappointasia" });
foreach (var item in Notification)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.ionic.io/push/notifications");
httpWebRequest.Headers.Add("Authorization", "Bearer " + item.TokenApp);
// httpWebRequest.Headers.Add("Content-Type", "application/json");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var stream = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"tokens\": [\"" + item.TokenDevice + "\"],\"notification\": {\"message\":\"" + item.NMessage + "\",\"ios\": {\"message\":\"" + item.IMessage + "\"}, \"android\": {\"message\":\"" + item.AMessage + "\"}},\"profile\": \"" + item.Profile + "\"}";
stream.Write(json);
// stream.Flush();
// stream.Close();
}
//ตอนเรียกส่ง
var response = (HttpWebResponse)httpWebRequest.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
TextDisplay += "{\"tokens\": [\"" + item.TokenDevice + "\"],\"notification\": {\"message\":\"" + item.NMessage + "\",\"ios\": {\"message\":\"" + item.IMessage + "\"}, \"android\": {\"message\":\"" + item.AMessage + "\"}},\"profile\": \"" + item.Profile + "\"}";
TextDisplay += " ";
}
}
ตัวนี้เป็นฟังก์ชั่นเวลาที่เริ่มทำงานครับ
private void button4_Click(object sender, EventArgs e)
{
Timer t = new Timer();
t.Interval = 5000;//1000 มิลิวินาที เท่ากับ 1 วินาที
t.Tick += new EventHandler(button5_Click);
t.Enabled = true;
//อย่าลืมถ้าจะให้ timer ทำงานตอนไหน
t.Start();
//อย่าลืมถ้าจะให้ timer หยุดทำงานตอนไหน
// t.Stop();
}
ใครพอทราบสาเหตุโปรดช่วยหน่อยครับ