The basic concept on how you can send push notifications to individual users of your app is as follows:
- Your user John opens the app.
- Your website calls the javascript function
getNotificationTokenwhich returns the app instance's push notification token. - Your website stores the token (for example in John's entry in your user database).
- When you want to send a notification to John, you call the webtoapp.design API with the token and we will deliver the notification.
Using the Javascript Function
executeWhenAppReady() function of our app helper script. It ensures that your website doesn't try to interact with the app before it's ready or when your website is loaded using a regular browser (ReferenceError, function is not defined). This is the old implementation used in apps created/updated before August 2021.
You need to define the receivePushNotificationToken function on your website so that it can be called by the app. You can do so as follows:
<script>
// this function automatically gets called by the app
function receivePushNotificationToken(token) {
// store the token (or whatever you want to do with it)
}
</script>
Things to keep in mind:
- The app will call the javascript function on every pageload. It's normal that you will sometimes not do anything with the token, for example when the user is not logged in and you therefore can't store the token in the user database.
- The app will not call the javascript function if the app for some reason could not determine the push notification token (very unlikely, but could theoretically happen).
- Obviously not every visitor of your website will be using your app, so the function won't be called on every page load.
When you want to get an app instance's push notification token, simply call the getNotificationToken function.
<script>
try {
var token = (await getNotificationToken())["token"];
}
catch (e) {
// Can occur if:
// - the app couldn't connect to the native code. Should be very unlikely.
// - the app couldn't get the push notification token, maybe because of connection issues. Should be very unlikely.
// - push notifications are not included in your current plan
console.log(e);
}
</script>
Things to keep in mind:
- The token is a long string, for example:
- A user's token can change,
- when the same user uses the app on a different device
- when the app is uninstalled and reinstalled
- when the app's data is cleared
- Keep the push notification tokens secret.
API Endpoint Reference
Get your app-specific API key from your app dashboard.
Send your API key as a query in the URL and the request body in the JSON format.