# 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="https://4196027459-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEXhSGyb0mYOQ2RZvkDaH%2Fuploads%2Fgit-blob-4be1cca928f93eb11689dc6a0c7357e01d6a54e4%2Fpush-express-app-1.png?alt=media" 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="https://4196027459-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEXhSGyb0mYOQ2RZvkDaH%2Fuploads%2Fgit-blob-007765651b8a85fefcdf5638d910a146c0dd6d67%2Fpush-express-app-5.png?alt=media" 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="https://4196027459-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEXhSGyb0mYOQ2RZvkDaH%2Fuploads%2Fgit-blob-9f4c5ae1ed3be7e7bb02abe6766a5d3309f95c46%2Fvapidkey.png?alt=media" 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)
