Namespace: SpawnDev.SpawnJS.JSObjects
Inherits: SpawnJSObject
Source: JSObjects/PushSubscription.cs
MDN Reference: PushSubscription on MDN
The PushSubscription interface of the Push API provides a subscription's URL endpoint and allows unsubscribing from a push service. https://developer.mozilla.org/en-US/docs/Web/API/PushSubscription
| Signature | Description |
|---|---|
PushSubscription(SpawnJSObjectReference _ref) |
Deserialization constructor |
| Property | Type | Access | Description |
|---|---|---|---|
Endpoint |
string |
get | A string containing the endpoint associated with the push subscription. |
ExpirationTime |
double? |
get | A DOMHighResTimeStamp of the subscription expiration time associated with the push subscription, if there is one, or null otherwise. |
Options |
PushManagerSubscribeOptions |
get | An object containing the options used to create the subscription. |
| Method | Return Type | Description |
|---|---|---|
Unsubscribe() |
Task<bool> |
Starts the asynchronous process of unsubscribing from the push service, returning a Promise that resolves to a boolean value when the current subscription is successfully unregistered. |
GetKey(string keyName) |
ArrayBuffer |
Returns an ArrayBuffer which contains the client's public key, which can then be sent to a server and used in encrypting push message data. |
SpawnDev.SpawnJS Mapping Note: The JavaScript examples below show the standard Web API usage. In SpawnDev.SpawnJS, the same API is available with C# conventions:
- Properties and methods use PascalCase (e.g.,
readyStatebecomesReadyState)- Events use ActionEvent with
+=/-=(e.g.,addEventListener("click", fn)becomesOnClick += handler)- Async methods return
Task<T>instead ofPromise<T>- Objects should be disposed with
usingstatements- Access via
JS.Get<PushSubscription>(...)or constructornew PushSubscription(...)
// Get a PushSubscription object
const pushSubscription =
await serviceWorkerRegistration.pushManager.subscribe();
// Create an object containing the information needed by the app server
const subscriptionObject = {
endpoint: pushSubscription.endpoint,
keys: {
p256dh: pushSubscription.getKey("p256dh"),
auth: pushSubscription.getKey("auth"),
},
encoding: PushManager.supportedContentEncodings,
/* other app-specific data, such as user identity */
};
// Stringify the object and post to the app server
fetch("https://example.com/push/", {
method: "post",
body: JSON.stringify(subscriptionObject),
});navigator.serviceWorker.ready
.then((reg) => reg.pushManager.getSubscription())
.then((subscription) => subscription.unsubscribe())
.then((successful) => {
// You've successfully unsubscribed
})
.catch((e) => {
// Unsubscribing failed
});