Aktualisiert am 29. Juli 2021 von Jan Bunk
Das grundlegende Konzept, wie du Push-Benachrichtigungen an eine Gruppe von Nutzern deiner App senden kannst, ist wie folgt:
executeWhenAppReady()
Funktion von
unser App-Helfer-Skript. Sie stellt sicher, dass deine Website nicht versucht, mit der App zu interagieren, bevor die App bereit ist oder wenn die Website mit einem normalen Browser geladen wird.
Wenn eine App-Instanz ein Push-Benachrichtigungsthema abonnieren soll, rufe einfach die Funktion setNotificationTopicSubscriptionStatus mit dem neuen Abonnement-Status (true = abonniert, false = nicht abonniert) und deinem gewünschten Themennamen auf.
<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 enabled for your app
console.log(e);
}
</script>
Dinge, die du im Hinterkopf behalten solltest:
[a-zA-Z0-9-_.~%]{1,850}
Vielleicht möchtest du später auch eine Liste der Themen erhalten, die die App-Instanz abonniert hat. Der Rückgabewert ist eine Liste von 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 enabled for your app
console.log(e);
}
</script>
Hol dir deinen App-spezifischen API-Schlüssel von deinem App Dashboard.
Sende deinen API-Schlüssel als Query in der URL und den Request Body im JSON-Format.