使用Service Worker和FCM在网站上推送通知

时间:2019-01-04 18:53:25

标签: c# asp.net push-notification service-worker

嗨,我是推送通知的新手。我想使用service worker和fcm在我的网站上实现推送通知。我遵循此URL'https://developers.google.com/web/fundamentals/codelabs/push-notifications/'并在允许通知其创建入口点,p256dh和auth密钥时,为其客户端应用程序创建其显示通知弹出窗口。我还创建了一个使用entpoint,p256dh,auth以及私钥,公钥和发送通知的服务。此通知是客户的回执。

用于发送通知的c#代码如下。

 var pushEndpoint = @"https://fcm.googleapis.com/fcm/send/fOetqkkxzlM:APA91bHASHHThdRC4dDCnZJxAy6ZngUHhxsT9mjF5xuBDpM5kzy-dq2jcbQtsiDcXZ9WcOTE4GWzUZkvGbt3xBH0y61mqbkRVkjvBfvgnUqAGf1hsIrUXpsrl9nfqmrlXx2UFWaTJerC";
    var p256dh = @"BHmoaUoqJ6rUitaXj-YvXbuORO_x6lFigO-NV1ceDygb4GhywOw7WIKWOtWqMsVswl8pUCVJrPiNtyVQFirD1oI";
    var auth = @"-aFkO3hZKP6u0BZwKRNDiw";

    var subject = @"mailto:xyz@gmail.com";
    var publicKey = @"BNizeduk-kgre_pz7aj0_t89t-5nvQttzyL17rQfsc7GlOFIfn0qVKyLF81ahCGdsMdhWz-5n_PQavZHywBt9Rs";
    var privateKey = @"W21b4Jh9qSoGI7GpKHAq6ikMBjTStiBvQP_wWVvVRqE";

    var subscription = new PushSubscription(pushEndpoint, p256dh, auth);
    var vapidDetails = new VapidDetails(subject, publicKey, privateKey);
    //var gcmAPIKey = @"[your key here]";
    string payload = @"{""user"":{""name"":""mohan"",""teamname"":""b"",""email"":""c"",""players"":[""1"",""2""]}}";

    var webPushClient = new WebPushClient();
    try
    {
        webPushClient.SendNotification(subscription, payload, vapidDetails);

        //webPushClient.SendNotification(subscription, "payload", gcmAPIKey);
    }
    catch (WebPushException exception)
    {
        Console.WriteLine("Http STATUS code" + exception.StatusCode);

    }

我的问题是我必须通过的每个客户端都必须传递entpoint,p256dh和auth key。我可以自动化它吗?就像我可以为所有已订阅用户并一次发送消息的用户获取此密钥(entpoint,p256dh,auth)。

请帮助

0 个答案:

没有答案