JavaScript Async

The example assumes that you have the following code placed defined before calling Gravitec functions.

Update APP_KEY with your Gravitec AppId.

<script src="https://cdn.gravitec.net/storage/APP_KEY/client.js" async></script>
<script>var Gravitec= Gravitec || [];</script>
Functions

init

This is the only required method that you need to call for setting up Gravitec to receive push notifications. Call it from each page of your site.

  • Parameters
  • JSON options
  • Boolean autoRegister (Optional) - Automatically show browser prompt to accept notifications. You can pass in "false" to delay this pop-up and then call registerUserForPush to prompt them later.
  • Boolean createButton (Optional) It creates a default button that generates a window for receipt of the notifications, which appears after clicking.
  • String tooltipText (Optional, use only with createButton) - Default: "One click subscription to our newsletter!" Set the text that will be shown to users on a default button.

Example:

var Gravitec = Gravitec || [];
Gravitec.push(["init", {"autoRegister":false}]);

registerUserForPush

Call it when you want to prompt the user to accept push notifications. Only call if you set "false" in autoRegister: when called "init".

Example:

var Gravitec = Gravitec || [];
Gravitec.push(["registerUserForPush", callback]);

addTag

Tags a user based on an app event of your choosing so that later you can create segments on gravitec.net to target these users. Recommend using setTags over addTag if you need to set more than one tag on a user at a time.

  • Parameters
  • string value - Value to set.

Example:

var Gravitec = Gravitec || [];
Gravitec.push(["addTag", "value"]);

addTags

Add provided tag(s) to subscriber’s tags. Thus, a push segment based on these tags could be created.

  • Parameters
  • JSON array of string value(s) – tag(s) to add

Example:

var Gravitec = Gravitec || [];
Gravitec.push(["addTags", ["value1", "value2"]]);

setTags

Tag a user based on an app event of your choosing so later you can create segments on gravitec.net to target these users.

  • Parameters
  • JSON values – Values of your choosing to create.

Example:

var Gravitec = Gravitec || [];
Gravitec.push(["setTags", ["value1", "value2"]]);

removeTag

Deletes a tag that was previously set for a user with addTag or setTags. Use removeAllTags if you need to delete all of them.

  • Parameters
  • String value – Value to remove.

Example:

var Gravitec = Gravitec || [];
Gravitec.push(["removeTag", "value"]);

removeAllTags

Deletes all tags that were previously set for a user with addTag or setTags.

Example:

var Gravitec = Gravitec || [];
Gravitec.push(["removeAllTags"]);

setAlias

Set a alias(user identifier) for each user of gravitec.net to target these users.

  • Parameters
  • String value – Value to set.

Example:

var Gravitec = Gravitec || [];
Gravitec.push(["setAlias", "value"]);

getSubscription

Lets you retrieve the Google Registration ID. Your handler is called after the device is successfully registered with Gravitec.

Example:

var Gravitec = Gravitec || [];
Gravitec.push(["getSubscription", function (subscriptionId) {
 if (subscriptionId) {
  console.log(subscriptionId);
 }
}]);

afterSubscription

Callback that is called after the device is successfully registered with Gravitec. Return token.

Example:

var Gravitec = Gravitec || [];
Gravitec.push(["afterSubscription", function (token) {
  console.log(token);
  //Your action
}]);

isSubscribed

Shows if user give permission to send notifications. Return true or false.

Example:

var Gravitec = Gravitec || [];
Gravitec.push(["isSubscribed", function (success) {
  console.log(success);
  //Your action
}]);