> For the complete documentation index, see [llms.txt](https://docs.antsomi.com/cdp-365-user-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.antsomi.com/cdp-365-user-guide/quick-start-guide/how-to-push-notification-firebase-cloud-messaging-for-shopify.md).

# How to push notification "Firebase Cloud Messaging" for Shopify

At step Install custom app, please contact us before taking the step.

## I. Create a Firebase project

### 1. Create a project

#### - Log in [Firebase](https://console.firebase.google.com/) and create a new project

<figure><img src="https://3576497809-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmECwNqMNUmu6OXHSYgDv%2Fuploads%2FCBTUiyLEWSYPpTPizsHZ%2Fimage.png?alt=media&amp;token=3a265353-ff69-4302-ae94-1737fc6b0846" alt=""><figcaption><p>Add new project</p></figcaption></figure>

#### - Enter the name of project and click "Continue"

<figure><img src="https://3576497809-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmECwNqMNUmu6OXHSYgDv%2Fuploads%2FjV9YKSQO8qGr8NzAapvc%2Fimage.png?alt=media&amp;token=0fe128f2-af56-414f-99ce-4fc038797c5a" alt=""><figcaption><p>Enter Name of project</p></figcaption></figure>

#### - Accept the privacy and create project

<figure><img src="https://3576497809-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmECwNqMNUmu6OXHSYgDv%2Fuploads%2FprXDPLrEA3hLQDswyvIE%2Fimage.png?alt=media&amp;token=1a59fdad-de43-492f-853b-7eff08616827" alt=""><figcaption><p>Create project</p></figcaption></figure>

### 2. Build "Cloud Messaging"

#### - Click dropdown "Build" and select "Cloud Messaging"

<figure><img src="https://3576497809-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmECwNqMNUmu6OXHSYgDv%2Fuploads%2FGthxeUJPF7GTUo34SAAQ%2Fimage.png?alt=media&amp;token=69134f7b-5135-4395-ae17-2e071941c52d" alt=""><figcaption><p>Cloud Messaging</p></figcaption></figure>

#### - Add firebase to your web app

<figure><img src="https://3576497809-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmECwNqMNUmu6OXHSYgDv%2Fuploads%2FvlQDFOQBu7TG7IFOJ3jA%2Fimage.png?alt=media&amp;token=c8f3a4ec-6708-40e9-a9f9-6fd0ba509666" alt=""><figcaption><p>Add firebase to web app</p></figcaption></figure>

#### - Click "Register app"

<figure><img src="https://3576497809-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmECwNqMNUmu6OXHSYgDv%2Fuploads%2FFPboyZ9tTmlyeWUU5BxA%2Fimage.png?alt=media&amp;token=bc5735b2-a614-4578-8900-ad437d0d91c1" alt=""><figcaption><p>Register app</p></figcaption></figure>

#### - Copy "firebaseConfig" and Click "Continue to console"

<figure><img src="https://3576497809-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmECwNqMNUmu6OXHSYgDv%2Fuploads%2FVQnSw52KlNMMFlsqua10%2Fimage.png?alt=media&amp;token=d5b3b20d-cb33-4ae8-a7d1-4fe378ed6460" alt=""><figcaption><p>Continue to Console</p></figcaption></figure>

#### - Firebase config:

```
apiKey: "AIzaSyBUJrgxGjFUWld-q_zr_cVzrN9IaB4KfmE",
  authDomain: "fir-clound-messaging-ab666.firebaseapp.com",
  projectId: "fir-clound-messaging-ab666",
  storageBucket: "fir-clound-messaging-ab666.appspot.com",
  messagingSenderId: "974234323980",
  appId: "1:974234323980:web:03d23bee74ed48eb5c7229",
  measurementId: "G-EE955NEBKJ"
```

#### - Click settings icon and select "Project settings"

<figure><img src="https://3576497809-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmECwNqMNUmu6OXHSYgDv%2Fuploads%2FRL59cIY0kufLEjkmzL0d%2Fimage.png?alt=media&amp;token=ac7cd7cf-70e8-4711-97f3-f820302c09a1" alt=""><figcaption><p>Project settings</p></figcaption></figure>

#### - Click "Cloud Messaging"

<figure><img src="https://3576497809-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmECwNqMNUmu6OXHSYgDv%2Fuploads%2Fy3MRDSspMP2jzGy5q7Hi%2Fimage.png?alt=media&amp;token=c36e0b65-200b-4a0d-ad23-1a06368047b4" alt=""><figcaption><p>Cloud Messaging settings</p></figcaption></figure>

#### - Enable Cloud Messaging API

<figure><img src="https://3576497809-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmECwNqMNUmu6OXHSYgDv%2Fuploads%2FQLrIKIEAZhxBCmgSqED0%2Fimage.png?alt=media&amp;token=b8b19a06-74c1-486a-9e1d-a7484b7cc750" alt=""><figcaption><p>Manage API</p></figcaption></figure>

<figure><img src="https://3576497809-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmECwNqMNUmu6OXHSYgDv%2Fuploads%2FIS3BhSLU8INTSOPVtryB%2Fimage.png?alt=media&amp;token=f8f6dd40-0761-4f54-9fae-dc57d22b8bb4" alt=""><figcaption><p>Click Enable</p></figcaption></figure>

#### - After that, we have the data of Server Key

<figure><img src="https://3576497809-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmECwNqMNUmu6OXHSYgDv%2Fuploads%2FFYgMQBAgOmY3u0kOWo7Z%2Fimage.png?alt=media&amp;token=378a14d8-4a7d-48c6-bebb-f55fa0a827d2" alt=""><figcaption><p>Server Key</p></figcaption></figure>

### 3. Add script to source code of website

#### - Copy and paste these scripts into the bottom of your \<body> tag

```
<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/8.10.0/firebase-app.js"></script>
<!-- TODO: Add SDKs for Firebase products that you want to use https://firebase.google.com/docs/web/setup#available-libraries -->
<script src="https://www.gstatic.com/firebasejs/8.10.0/firebase-analytics.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.10.0/firebase-messaging.js"></script>
<script>
  var _cdpGetCookie = function (cookieName) {
    var cookiePattern = new RegExp("(^|;)[ ]*" + cookieName + "=([^;]*)");
    cookieMatch = cookiePattern.exec(document.cookie);
    return cookieMatch ? window.decodeURIComponent(cookieMatch[2]) : 0;
  };
​
  var _cdpSetCookie = function (cookieName, value, msToExpire, path, domain, secure) {
    var expiryDate;
    if (msToExpire != undefined) {
      expiryDate = new Date();
      expiryDate.setTime(expiryDate.getTime() + msToExpire);
    }
    var valueData = value + "";
    if (isNaN(value)) {
      valueData = window.encodeURIComponent(value);
    }
    var cookieInfo =
      cookieName +
      "=" +
      valueData +
      (msToExpire ? ";expires=" + expiryDate.toGMTString() : "") +
      ";path=" +
      (path || "/") +
      (domain ? ";domain=" + domain : "") +
      (secure ? ";secure" : "");
    document.cookie = cookieInfo;
  };
​
  var _cdpGetLeadTime = function () {
    var d = new Date();
    var m = d.getMonth() + 1;
    if (parseInt(m) < 10) {
      m = "0" + m;
    }
​
    var day = d.getDate();
    if (parseInt(day) < 10) {
      day = "0" + day;
    }
​
    var hour = d.getHours();
    if (parseInt(hour) < 10) {
      hour = "0" + hour;
    }
​
    var min = d.getMinutes();
    if (parseInt(min) < 10) {
      min = "0" + min;
    }
​
    var sec = d.getSeconds();
    if (parseInt(sec) < 10) {
      sec = "0" + sec;
    }
​
    var leadTime = d.getFullYear() + "-" + m + "-" + day + " " + hour + ":" + min + ":" + sec;
​
    return leadTime;
  };
​
  var _cdpTrackFirebaseDetail = function (clientToken) {
    console.log("CDP firebase clientToken : " + clientToken);
    var props = {
      extra: {
        identify_event: "firebase_subscribed",
        uid_firebase: clientToken,
        firebase_time: _cdpGetLeadTime(),
      },
    };
​
    var expireTime = 60 * 60 * 1000; // 60p
    var cookieName = "_uid_firebase";
​
    var cookieToken = _cdpGetCookie(cookieName);
    if (!cookieToken) {
      if (typeof web_event.track == "function") {
        web_event.track("firebase", "subscribed", props);
      } else {
        web_event.queue.push(["track", "firebase", "subscribed", props]);
      }
    } else if (cookieToken !== clientToken) {
      props.extra.identify_event = "firebase_refresh";
​
      if (typeof web_event.track == "function") {
        web_event.track("firebase", "subscribed", props);
      } else {
        web_event.queue.push(["track", "firebase", "subscribed", props]);
      }
    }
​
    _cdpSetCookie(cookieName, clientToken, expireTime, "/", "", 0);
  };
​
  // Your web app's Firebase configuration
  // For Firebase JS SDK v7.20.0 and later, measurementId is optional
  var firebaseConfig = {
    *firebaseConfig*
  };
​
  // Initialize Firebase
  firebase.initializeApp(firebaseConfig);
  firebase.analytics();
  const messaging = firebase.messaging();
  let enableForegroundNotification = true;
​
  if ("serviceWorker" in navigator) {
    window.addEventListener("load", () => {
      navigator.serviceWorker
        .register(
          "/apps/firebase-messaging-sw?apiKey=" +
            firebaseConfig.apiKey +
            "&authDomain=" +
            firebaseConfig.authDomain +
            "&projectId=" +
            firebaseConfig.projectId +
            "&storageBucket=" +
            firebaseConfig.storageBucket +
            "&messagingSenderId=" +
            firebaseConfig.messagingSenderId +
            "&appId=" +
            firebaseConfig.appId +
            "&measurementId=" +
            firebaseConfig.measurementId
        )
        .then((register) => {
          messaging.useServiceWorker(register);
          messaging.requestPermission().then(() => {
            messaging.getToken().then((fcmToken) => {
              _cdpTrackFirebaseDetail(fcmToken);
​
              messaging.onMessage((payload) => {
                console.log("onMessage event fired", payload);
                if (enableForegroundNotification) {
                  console.log(payload);
                  let notification = payload.notification;
                  notification.data = {
                    url: notification.click_action,
                  };
                  navigator.serviceWorker.getRegistrations().then((registration) => {
                    console.log("$$$$,", notification);
                    registration[0].showNotification(notification.title, notification);
                  });
                }
              });
            });
          });
        });
    });
  }
</script>
```

#### - \*firebaseConfig\* is the data of [firebase config](#firebase-config) in section 2

#### - Install custom app (Contact us before taking this step)

* At this step, we need you to provide the link of your domain website in Shopify&#x20;

<figure><img src="https://3576497809-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmECwNqMNUmu6OXHSYgDv%2Fuploads%2FVFO0TGR91iWWqKbRiinz%2Fimage.png?alt=media&amp;token=1e18f2c9-d469-4fba-9dc8-e19485486a02" alt=""><figcaption></figcaption></figure>

* Copy and send it to us then we will give you a link to install a custom app&#x20;
* We need to install an app, so please contact us before you install the app.
* Example link to install app: <https://demo.myshopify.com/admin/oauth/install_custom_app?client_id=******&signature=*******%3D--3f7929dda8***2972bb59>
* Click the link then press Install app

<figure><img src="https://3576497809-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmECwNqMNUmu6OXHSYgDv%2Fuploads%2FjtCbvhxlRzqHVtU6gdtU%2Fimage.png?alt=media&amp;token=78aa7af1-7772-42ea-9fe4-4c0aa44ea8a6" alt=""><figcaption></figcaption></figure>

#### - We have done with the set up on your website. Next, we will continue with set up in CDP

## II. Create destination "Firebase Cloud Messaging"

### 1. Access the "MARKETING HUB" app and go to the "Destinations" menu

<figure><img src="https://3576497809-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmECwNqMNUmu6OXHSYgDv%2Fuploads%2FvehpQTElSkvSLWo7hmrn%2Fimage.png?alt=media&amp;token=bce7fa0a-36e1-49e0-a3d8-8562a2791876" alt=""><figcaption><p>Destination</p></figcaption></figure>

### 2. Click "+" button and select "Web Push-Notification"

<figure><img src="https://3576497809-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmECwNqMNUmu6OXHSYgDv%2Fuploads%2FjOUqM67Dc37HMnwZ6LW9%2Fimage.png?alt=media&amp;token=1c787dc5-3ee7-42d8-908a-f82cd6b41c87" alt=""><figcaption><p>Web Push-Notification</p></figcaption></figure>

### 3. Select "Destination catalog": "Firebase Cloud Messaging"&#x20;

<figure><img src="https://3576497809-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmECwNqMNUmu6OXHSYgDv%2Fuploads%2F0TDkz2iCcsa4NQ9QhFRc%2Fimage.png?alt=media&amp;token=6926a657-97ea-41a8-b8eb-9b949a5ef8d6" alt=""><figcaption><p>Firebase Cloud Messaging Destination #1</p></figcaption></figure>

&#x20;

<figure><img src="https://3576497809-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmECwNqMNUmu6OXHSYgDv%2Fuploads%2FDUFYstKqzdfKBvYmKXmh%2Fimage.png?alt=media&amp;token=34d1ac62-5709-4303-b212-5627ada8ca77" alt=""><figcaption><p>Firebase Cloud Messaging Destination #2</p></figcaption></figure>

* **Server Token**: we can copy server token as instruction in [previous section](#after-that-we-have-the-data-of-server-key)

#### - Get "To Client Tokens"

* Visit the website, press F12 and go to Console to get "To Client Tokens"

<figure><img src="https://3576497809-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmECwNqMNUmu6OXHSYgDv%2Fuploads%2F6qWu38iOigvHOjYsCqMa%2Fimage.png?alt=media&amp;token=62dd5fba-5d9e-47bb-b4f2-a2c375ef7ab2" alt=""><figcaption><p>To Client Tokens</p></figcaption></figure>

* Copy "To Client Tokens" and paste to destination setting
* Click "Save" to save destination

#### Note: After client finish creating "Firebase Cloud Messaging", Antsomi team will collect the data and update "To Client Tokens" attribute in CDP system.

## III. Push "Firebase Cloud Messaging" notification via Journey

### 1. Access "MARKETING HUB" app and go to "Customer Journey" menu

<figure><img src="https://3576497809-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmECwNqMNUmu6OXHSYgDv%2Fuploads%2FpB8P0svuxFsie58eqOdb%2Fimage.png?alt=media&amp;token=f4cc882b-e862-4c17-b1b4-28b094034ba6" alt=""><figcaption><p>Customer Journeys</p></figcaption></figure>

* We have 2 channels that supporting to send "Firebase Cloud Messaging". These are "Web Push-Notification" and "Journey Orchestration"

<figure><img src="https://3576497809-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmECwNqMNUmu6OXHSYgDv%2Fuploads%2F5MQsJXdsbCERrWJTsiEf%2Fimage.png?alt=media&amp;token=af15d249-1d3a-4ba7-b8db-e5ad95044a01" alt=""><figcaption><p>Web Push-Notification &#x26; Journey Orchestration</p></figcaption></figure>

### 2. Click "+" button to Create Journey, add "Destination" node and select "Firebase Cloud Messaging" destination

<figure><img src="https://3576497809-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmECwNqMNUmu6OXHSYgDv%2Fuploads%2F0aDKnmyx7FPac4qyR5r1%2Fimage.png?alt=media&amp;token=8988870f-28c2-47bb-a118-f7fbaaf0f762" alt=""><figcaption><p>Add destination "Firebase Cloud Messaging"</p></figcaption></figure>

* Fill in the required information

<figure><img src="https://3576497809-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmECwNqMNUmu6OXHSYgDv%2Fuploads%2FpgRQtRv0sTdVw79JZLGq%2Fimage.png?alt=media&amp;token=e1a1857c-9f6f-41aa-9285-d7560c5944ea" alt=""><figcaption><p>Campaign setting</p></figcaption></figure>

<figure><img src="https://3576497809-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmECwNqMNUmu6OXHSYgDv%2Fuploads%2Fs3apewo01KY3mCrvUhuW%2Fimage.png?alt=media&amp;token=95599b61-2980-446a-b947-70f32f33addc" alt=""><figcaption><p>Variant setting</p></figcaption></figure>

* Click "Save change" to Save the journey

### 3. Click Activate to activate the journey

<figure><img src="https://3576497809-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmECwNqMNUmu6OXHSYgDv%2Fuploads%2FWdzxfLKLTHrtwmc1tR4x%2Fimage.png?alt=media&amp;token=66e974d0-c15e-448c-b27e-c652c877cb7a" alt=""><figcaption><p>Activate journey</p></figcaption></figure>
