Setup

Enable push notification by adding push parameter in your init function

    Hood('init','YOUR_API_KEY', { push: 'YOUR_PUSH_PUBLIC_KEY' })

The public key is a text value that can be used only with a proper private key. Both public and private keys are generated by Ocamba platform. The user can see and use the public key, but the private key is secured and safely stored on Ocamba platform.

Service Worker

To use push notification you need to have Service Worker running. A Service Worker is a script that your browser runs in the background, separate from a web page.

  • If you don’t already have a Service Worker, create a new file named sw.js with the content below, and place it in the root directory of your website.
  • Otherwise, if your site already registers a Service Worker, add the content below to the

Service Worker file importScripts(“https://cdn.ocmhood.com/sdk/osw.js”);

NOTE: If you already have Service Worker running you need to configure SDK to load your service worker. You can do it by passing service worker name in initialisation.

Hood(‘init’,‘YOUR_API_KEY’, {customSw: ‘/YOUR_SW_NAME.js’,})

Default push notification It is very important to show notification when the user is ,,woken up". If the browser hasn’t received any data about notification the default notification will be shown. To configure your default message you can call setConfig in your Service Worker file (sw.js).

oswbox.setConfig(e); Where (e) is notification object. Example: if (oswbox){ let oswc = { title: “Your default title”, options : { “body”: “Your default body”, } }; oswbox.setConfig(oswc); }

Prompting To show push notification to the user you must ask for the user permission. Hood SDK shows the Native Browser Permission Prompt. Its is required for web push subscription and is not customizable. Native Browser Permission Prompt that asks for user permission:

Permission Description Granted The user clicked allow when he was asked for permission- he will receive notifications Denied The user clicked allow when he was asked for permission- he will receive notifications Default/Prompt The user decision is unknown; in this case, the application will act as if permission was denied - he will not receive notifications but he will be asked for permission again.

To send request for users permission use Hood(“requestPushPermission”). You can use callback if you want to execute function on prompt show Hood(“requestPushPermission”,callback) Or you can use event handler Hood(“event”,“onPushShow”,cb); Its is recommended to use prompt based on the users’s current status. You can get user’s status by calling Hood (“getPushStatus”,callbackFunction). Hood(‘getPushStatus’, (status)=>{ if (status==“prompt”) Hood(“requestPushPermission”);

NOTE: You can use Hood(“requestPushPermission”) without checking users’s status

You can assign your event handler functions for actions based on user gesture or permission. Events are based on the user permission to show or block the notifications. For every user that comes to the page API knows if the user already came to the page and what’s its permission. Based on that, events that can be triggered are: Hood (“event”,“event_Name”, callBack)

The callBack is a custom made function that is performed when even event_Name is fired. All events you can track are:

Event Name Description Example onPushAllow event is triggered when the user clicks allow on the prompt for notification permissions. Hood(’event’,‘onPushAllow’, cb); onPushBlock event is triggered when the user clicks block on the prompt for notification permissions. Hood(’event’,‘onPushBlock’, cb); onPushGranted event is triggered when the user that has permission granted comes to the page Hood(’event’,‘onPushGranted’, cb); onPushBlocked event is triggered when the user that has permission denied comes to the page Hood(’event’,‘onPushBlocked’, cb); onPushShow event is triggered when permission prompt is shown Hood(’event’,‘onPushShow’, cb);

You can use custom events combined with getPushStatus: Hood(‘getPushStatus’, (status)=>{ if (status==“prompt”) Hood(“requestPushPermission”); else if (status===“granted”) Hood(’event’, ‘onPushGranted’, cb); });

or without checking user’s status Hood(’event’, ‘onPushGranted’, cb);

Notification can be sent by push server but it can be also sent from the web app. Notification is sent when the user opens the web page where the Hood(‘showPushMessage’, e, cb ) is called.

  • e is notification object { “title”: “TITLE” “options”: {“body”: “My body”} }
  • cb is call back function

5.1.Changing the name of the global variable By default the name of the global variable used to call functionality is Hood. You can change it by defining your global variable name in the snippet in step 1. (window,document,‘script’,’ https://ocmhood.com/sdk/hood.js ‘, NAME_YOUR_VARIABLE); If you have window namespace conflict (there is another variable, function, method) called Hood you can change the SDK global variable name to avoid any conflicts. NOTE: You MUST use this name for calling all other functionality in the Hood system (including init).

Configurating Service Worker file name If you have another Service Woker running you can configure Push SDK to load your service Worker by calling the Hood config function Hood(‘config’, ‘SW_WORKER’, ‘/YOUR_SW_NAME.js’) NOTE: Config function can be called only after init is called

Hood calls

Event Name Description Example init Initialise Hood SDK Hood(‘init’,‘YOUR_ACTIVATION_CODE’ ); utm Sends additional utm Hood(‘utm’,‘campaign’,“value”); parameters for analytics sendUserID Sends user id parameter for Hoood(‘setUserID’,‘USER_ID’) analytics track Sends additional parameters Hood(’track’,’term’, “value”); for tracking requestPushPer mission Show prompt for getting user permission Hood(‘requestPushPermission’,cb); getPushStauts Returns current user permission Hood(‘getPushStauts’,cb); onPushAllow event is triggered when the user clicks allow on the prompt for notification permissions. Hood(’event’,‘onPushAllow’, cb); onPushBlock event is triggered when the user clicks block on the prompt for notification permissions. Hood(’event’,‘onPushBlock’, cb); onPushGranted event is triggered when the user that has permission granted comes to the page Hood(’event’,‘onPushGranted’, cb); onPushBlocked event is triggered when the user that has permission denied comes to the page Hood(’event’,‘onPushBlocked’, cb); onPushShow event is triggered when permission prompt is shown Hood(’event’,‘onPushShow’, cb); showPushMessa ge Send push message for web app Hood(‘showPushMessage’,msg);

SDK setup

  1. If you change the name of your global variable you MUST use new name in calling all functionality (including init)
  2. We strongly recommend using functions for sending additional (user, utm, track) parameters before init.
  3. Make sure that you change YOUR_ACTIVATION_CODE with your activation code in all code snippets
  4. It is recommended to set all config in init function Push setup
  5. Make sure to set push: YOUR_PUBLIC_KEY in init function
  6. It is recommended to call your Service Worker file sw.js if you don’t have another Service Worker running
  7. It is recommenced to configure your Service Worker name in init function
  8. We strongly recommend using custom events combined with getPushStatus.
On This Page