# PWA

**PushExpress -- SDK PWA Native Push**

* [Creating an application in Push Express](#creating-an-application-in-push-express)
* [Add Push Express web sdk in your code](#add-push-express-web-sdk-in-your-code)
* [Example ready code](#example-ready-code)

### Creating an application in Push Express

1. Create new application.

<div align="center"><img src="/files/y3DKzlat58wNZ0pZ43ZI" alt="" width="800"></div>

2. Choose type of application PWA/Web.
3. After creating the application, push\_express\_app\_id will be available.

<div align="center"><img src="/files/ojBcLX9TMW7mdKjyoVZq" alt="" width="800"></div>

4. The VAPID KEY will also be available in application settings. You will need it when integrating the sdk in the app.

<div align="center"><img src="/files/q0XwlCbN0TW6lHpZDGr1" alt="" width="800"></div>

### Add Push Express web sdk in your code

2. Add PushExpress library below

```js
<script src="https://sdk.push.express/js/v2.0.3/push-express-sdk.js"></script>
```

3. Perform the SDK initialization

```js
  if (window.matchMedia('(display-mode: standalone)').matches){
        async function redirectUrl() {
            await initPushExpress({
                appID: "32809-2221",
                vapidKey: "BNIh3BEkoOIt15NJhbcf0XptMQOZF29WwdRRehtb8k8WbvY9544R5yURPeX3-jN_hQbrg-KYH8-2FGeOfFs9rdQ",
                externalID: "ec8c1a88-8d29-40bb-bc80-0bc9c0bd88c0",
                tags: {
                    adID: "",
                    segment: "test-segment",
                    webmaster: "test-webmaster"
                }
            });
            location.href = "https://www.google.com/";
        }
        redirectUrl();
    }
```

4. Initialization function contains the following required settings - appID, vapidKey. As well as optional externalID and tags fields.

```js
  appID: "32809-2221",
```

appID required field is located on the application page in the Push Express service ([look here](#push-express-app-id)).

```js
  vapidKey: "BNIh3BEkoOIt15NJhbcf0XptMQOZF29WwdRRehtb8k8WbvY9544R5yURPeX3-jN_hQbrg-KYH8-2FGeOfFs9rdQ",
```

The vapidKey is located in your application settings ( [look here](#firebase-vapid-key)).

```js
externalID : "ec8c1a88-8d29-40bb-bc80-0bc9c0bd88c0",
```

Send the unique installation id here.

```js
redirectDeeplink: true,
```

If a push notification contains a link, then clicking on the notification will launch the PWA application, and the link will be passed in the deeplink GET parameter. If the redirectDeeplink: true parameter is set, a redirect to this link will be performed. To cancel the redirect to the link, set the value to false.

```js
tags: {
        audiences: "test-segment",
    }
```

The segment code. Use it to add a device to a segment. Segments can be created in the [segments section](https://app.push.express/postbacks/offers/) of your personal account.

```js
tags: {
        webmaster: "test-webmaster",
    }
```

To transfer devices to other users of the service, use the functionality of webmasters. You can add a webmaster in the [personal section](https://app.push.express/settings/personal/) of your personal account.

4. Example code index.html

```js
 <!DOCTYPE html>
 <html lang="en">
     <head>
     <meta charset="UTF-8">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <link rel="stylesheet" href="src/style.css">
     </head>
     <body>
         <h1 id="header"></h1>
     </body>
 </html>
 <script src="https://sdk.push.express/js/v2.0.3/push-express-sdk.js"></script>
 <script>
      if (window.matchMedia('(display-mode: standalone)').matches){
      async function redirectUrl() {
         await initPushExpress({
              appID: "32809-2221",
              vapidKey: "BNIh3BEkoOIt15NJhbcf0XptMQOZF29WwdRRehtb8k8WbvY9544R5yURPeX3-jN_hQbrg-KYH8-2FGeOfFs9rdQ",
              externalID: "ec8c1a88-8d29-40bb-bc80-0bc9c0bd88c0",
              redirectDeeplink: true,
              tags: {
                  adID: "",
                  segment: "test-segment",
                  webmaster: "test-webmaster"
              }
          });
          location.href = "https://www.google.com/";
      }
      redirectUrl();
  }
  </script>
```

5. Create service worker file in your project. The file name must be as follows "push-express-sw\.js".

> \[!IMPORTANT] The service worker file must have a name push-express-sw\.js . Otherwise, the installation of service worker will fail.

6. Import library in service worker.

   ```js
   importScripts('https://sdk.push.express/js/v2.0.3/push-express-sw.js');
   ```
7. Example code push-express-sw\.js

```js
importScripts('https://sdk.push.express/js/v2.0.3/push-express-sw.js');
```

### Example ready code

[Сheck out an example of the finished code >>>](https://playcode.io/2346972)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.push.express/sdk/pwa.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
