เมื่อ create user ใหม่ใน windows server เราจะต้องตั้ง password ตามกฏที่ windows server กำหนดมาให้ ถ้าตั้งไม่ตรงตามกฎ windows server จะไม่ยอมให้สร้าง user อย่างเช่นสร้าง user ชื่อ sysadmin แล้วตั้ง password ว่า sysadmin123 จะไม่สามารถสร้าง user ได้ ซึ่งจะมี error ขึ้นดังรูป
using System.Net.Http;
using System.Web.Http;
using System.Threading.Tasks;
using System.Configuration;
using dotAPNS;
using System.Web;
using System;
namespace APNsService.Controllers
{
[RoutePrefix("api")]
public class MainController : ApiController
{
[Route("SendPush")]
[AcceptVerbs("GET", "POST")]
public async Task<ApnsResponse> SendPush(string title, string body, string token, string category)
{
try
{
//อ่านค่า CertContent หรือ CertFilePart, BundleID, KeyID,TeamID มาจาก ไฟล์ web.config
string certContent = ConfigurationManager.AppSettings["CertContent"];
string certFilePath = ConfigurationManager.AppSettings["CertFilePath"];
string bundleId = ConfigurationManager.AppSettings["BundleId"];
string keyId = ConfigurationManager.AppSettings["KeyId"];
string teamId = ConfigurationManager.AppSettings["TeamId"];
//สร้าง Option สำหรับ Token
var options = new ApnsJwtOptions()
{
CertContent = certContent,
BundleId = bundleId,
KeyId = keyId,
TeamId = teamId
};
//สร้าง Token
var apns = ApnsClient.CreateUsingJwt(new HttpClient(new WinHttpHandler()), options);
//สร้าง Push Object
var push = new ApplePush(ApplePushType.Alert)
.AddAlert(HttpUtility.UrlDecode(title), HttpUtility.UrlDecode(body))//ข้อความแจ้งเตือน
.AddBadge(0)//แสดงจำนวนแจ้งเตือนที่หน้าแอปพลิเคชัน
.AddCategory(category)//กำหนดกลุ่มของการแจ้งเตือน
.AddToken(token);//token
//ส่ง Notification
return await apns.Send(push);
}
catch (Exception ex)
{
return ApnsResponse.Error(ApnsResponseReason.Unknown, ex.Message + ex.StackTrace);
}
}
}
}
จากโค้ดด้านบน สามารถทราบว่าส่วนไหนทำหน้าที่อะไรได้จาก Comment ครับ เพียงเท่านี้ก็สามารถ Publish และ Deploy Web API บน Server เพื่อทดสอบการใช้งานได้แล้ว อย่างไรก็แล้วแต่ Web API ดังกล่าวยังต้องการ การปรับแต่งให้เหมาะสมกับระบบของนักพัฒนาแต่ละท่าน แต่โดยขั้นตอนหลักๆก็มีเพียงเท่านี้ครับ