Subscribe to Topic

Subscribes the device to a Firebase push topic.

Description

The subscribeToTopic helper subscribes the device to a Firebase push topic. Native subscribes only when the store's push plugin is Firebase. The topic is trimmed before it is sent.

Call it as Superfans.helpers.marketing.subscribeToTopic.

Function Signature

await Superfans.helpers.marketing.subscribeToTopic({
  topic: "sale"
});
ParameterTypeRequiredDescription
topicStringYesFirebase push topic. Must be a non-empty string. Leading and trailing spaces are removed.

Structure of Response

StatusResponse
Success{ "status": "success", "message": "Subscribed to topic successfully" }
Invalid topic{ "status": "error", "errorId": 400, "errorHandle": "invalid-params", "message": "Topic should be provided." }
Firebase disabled{ "status": "error", "errorId": 503, "errorHandle": "firebase-disabled", "message": "Firebase is not enabled." }
Unexpected Error{ "status": "error", "errorId": 500, "errorHandle": "unknown-error", "message": "Something unexpected happened" }

Example Usage

try {
  await Superfans.helpers.marketing.subscribeToTopic({
    topic: "sale"
  });
} catch (error) {
  await Superfans.actions.showToast({
    title: "Something went wrong",
    message: error.message || "Please try again."
  });
}

Best Practices

  1. Use a stable topic name. The same string is what you pass later to unsubscribeFromTopic.
  2. Handle errors with try/catch. A rejected call uses status, errorId, errorHandle, and message.

Caveats

  1. A missing, non-string, or blank topic is rejected before anything is sent.
  2. Native subscribes only when Firebase is the store's push plugin. If Firebase is disabled, the call rejects with firebase-disabled.

Did this page help you?