Updated on 17. April 2024 by Jan Bunk
The basic concept on how you can send push notifications to a group of users of your app is as follows:
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). When you want to subscribe an app instance to a push notification topic, simply call the setNotificationTopicSubscriptionStatus function with the new subscription status (true = subscribed, false = unsubscribed) and your desired topic name.
<script>
try {
await setNotificationTopicSubscriptionStatus(true, "mytopic");
}
catch (e) {
// Can occur if:
// - you passed an invalid topic name.
// - you didn't pass all necessary parameters to setNotificationTopicSubscriptionStatus().
// - the app couldn't subscribe to the topic, for example because of connection issues. Should be very unlikely.
// - the app couldn't connect to the native code. Should be very unlikely.
// - push notifications are not included in your current plan
console.log(e);
}
</script>
Things to keep in mind:
[a-zA-Z0-9-_.~%]{1,850}
You might also later want to get a list of topics the app instance is subscribed to. The return value is a list of strings.
<script>
try {
var topics = (await getSubscribedNotificationTopics())["topics"];
}
catch (e) {
// Can occur if:
// - the app couldn't connect to the native code. Should be very unlikely.
// - push notifications are not included in your current plan
console.log(e);
}
</script>
You might also want to check out a more complete code example. Here's the code for a button that subscribes/unsubscribes the user to/from the category of the current page on a WordPress website.
Inside the app settings, there's also a way for your users to unsubscribe from notification topics.
Now that you have subscribed users to your topic, you can begin sending notifications to your topic through our API.
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.
Here's a list of related developer documentation about push notifications:
And here are some more (non-developer) articles related to push notifications: