WEB PUSHãASP.NETã¨ã使ã£ã¦C#ã§ãã£ã¦ã¿ã
EdgeãPreviewçã§ã¯å¯¾å¿ãã¦ãããããããã¼ãã«ãå½åã«æ¯ã¹ãã¨ä¸ãã£ã¦ãããããã®ã§è©¦ãã¦ã¿ã¾ãã
WEB PUSHã®è©³ããããããã¯ã°ã°ã£ã¦ãã ãããç§ã¯ããããã£ã¦ã¾ããã®ã§
ä»åã®è¨äºã§åºæ¥ä¸ãã£ããã®ã¯ãã¡ãã«ä¸ãã¦ããã¾ã
ç°¡åãªåã
ãµã¼ãã¼å´ã§å ¬ééµã¨ç§å¯éµãæºåãã
ã¯ã©ã¤ã¢ã³ãå´ã§éç¥å ã®URLã¨ãä½ã
éç¥ããã人ãä¸ã®æ å ±ã使ã£ã¦éç¥ãã
ã£ã¦æããããã§ã
éµã®ä½ãæ¹ã¨ãããããã£ã¦ãªãã®ã§åã人ã®ä½ã£ã¦ãããã©ã¤ãã©ãªãã¤ããã¾ããã
ä½ã£ã¦ã¿ã
Webãµã¼ãã¼
ã¾ãã¯åºæ¬ã¨ãªãWebãã¼ã¸ãASP.NET MVC5ã®ãã³ãã¬ã¼ã使ã£ã¦æºåãã¦
Install-Package WebPush
ãã®ã¾ã¾ãªãã¤ãNuGetã«ããã®ã§å ¥ãã¾ãããããããé¢åãªã¨ãããã¿ããªãã£ã¦ããã¾ããããã¡ãã¡ãã¡ãã§ãã
ã¯ã©ã¤ã¢ã³ã表示æã«å ¬ééµãæãã¦ãããå¿ è¦ãããã®ã§Controllerã«å¦çãæ¸ãã¾ã
public ActionResult Index() { var model = new HomeModel(); var pushInformation = PushInformation.Load(AppDomain.CurrentDomain.BaseDirectory + "../info.xml"); // ä¿åå ã¯ã¦ãã¨ãã« if (pushInformation == null) // VAPIDã§ä½¿ç¨ããå ¬ééµãä½æ { pushInformation = new PushInformation(); var vapidKeys = VapidHelper.GenerateVapidKeys(); pushInformation.VapidPublic = vapidKeys.PublicKey; pushInformation.VapipPrivate = vapidKeys.PrivateKey; PushInformation.Save(pushInformation, AppDomain.CurrentDomain.BaseDirectory + "../info.xml"); // ä½ã£ããä¿åãã¨ã } model.VapidPublic = pushInformation.VapidPublic; model.Data = Request.QueryString.Get("data"); return View(model); }
ãã¨ã¯ã¯ã©ã¤ã¢ã³ãããéç¥ã«å¿ è¦ãªæ å ±ãããå£ãã¤ãã£ã¦ããã¾ã
[HttpPost] public void Post(NotificationTarget target) { var pushInformation = PushInformation.Load(AppDomain.CurrentDomain.BaseDirectory + "../info.xml"); // ä¿åå ã¯ã¦ãã¨ãã« if (pushInformation == null) return; pushInformation.Targets.Remove(pushInformation.Targets.FirstOrDefault(t => t.EndPoint == target.EndPoint)); pushInformation.Targets.Add(target); PushInformation.Save(pushInformation, AppDomain.CurrentDomain.BaseDirectory + "../info.xml"); }
Webã¯ã©ã¤ã¢ã³ã
javascriptãé å¼µã£ã¦æ¸ãã¾ããã
ãã®è¾ºåèã«ããã°ãªãã¨ããªãããããªãã§ãããã
ä¸ã¤æ°ãä»ããã®ã¯serviceWorkerã«èªã¿è¾¼ã¾ããã¹ã¯ãªããã¯ã«ã¼ãã«ç½®ããªãã¨ãã¤ã¾ã§ãã£ã¦ãèªã¿è¾¼ããªããã£ã¦ã¨ãããããã§ãã
javascriptã ããã£ã¦Scriptsãã©ã«ãã«å ¥ãã¦ãã ãã ãï¼ï¼
ããã¾ã§åºæ¥ããChromeã§ãã¼ã¸ãéãã¦ã¿ã¾ã
éç¥ã®è¨±å¯ãåºã¦è¨±å¯ããã¨è³¼èªãå§ã¾ãã¾ã
Pushã¢ããª
ã³ã³ã½ã¼ã«ã¢ããªãèµ·åããã¨èµ·åæãã©ã¡ã¼ã¿ã¼ãä¸ã§ç»é²ããã¯ã©ã¤ã¢ã³ãã«Pushããã¢ããªãä½ãã¾ã
Install-Package WebPush
var pushInformation = PushInformation.Load(AppDomain.CurrentDomain.BaseDirectory + "../../../info.xml"); // ASP.NETãä¿åãã¦ãããã¡ã¤ã«ãè¦ã¾ã var webPushClient = new WebPushClient(); var vapidDetails = new VapidDetails("mailto:[email protected]", pushInformation.VapidPublic, pushInformation.VapipPrivate); var targetList = pushInformation.Targets.ToArray(); foreach (var target in targetList) { try { var subscription = new PushSubscription(target.EndPoint, target.PublicKey, target.AuthSecret); webPushClient.SendNotification(subscription, args[0], vapidDetails); } catch (Exception exp) { if (exp.Message == "Subscription no longer valid") // è³¼èªè ãããªããªãã¨ãããªæãã®ä¾å¤ãåãã®ã§éä¿¡å ããæ¶ãã¦ããã { pushInformation.Targets.Remove(target); } } } PushInformation.Save(pushInformation, AppDomain.CurrentDomain.BaseDirectory + "../../../info.xml");
Webãµã¼ãã¼ãä¿åããæ å ±ã使ã£ã¦éç¥ããã ãã§ã
å®è¡ããã¨
éç¥ããã£ã¦ãã¦
ãã¼ã¸ãéãã¾ãã
ããã ãã§Androidã®Chromeã ã¨ããã¯ãã¦ãã¦ãéç¥åãã¦ãããããã«ãªããã§ããã
iOSãé å¼µã£ã¦ã»ãããã©æã¿èã§ãããã¼