How to register/subscribe an app user to topics in OneSignal?

Viewed 185

I am using OneSignal.com platform for sending pushes. I want to register/subscribe the app users to categories like weather, sport, technology... There is a method in Firebase SDK:

Messaging.messaging().subscribe(toTopic: "weather") { error in
  print("Subscribed to weather topic")
}

But I couldn't find the method in OneSignal. How can I subscribe users to categories? (It can be more than one)

1 Answers

OneSginal have something similar to Firebase Topics that are called Data Tags, here is a sample java code on how to add them:

class ExampleNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
  // This fires when a notification is opened by tapping on it.
  @Override
  public void notificationOpened(OSNotificationOpenResult result) {
    JSONObject data = result.notification.payload.additionalData;
    String topic;
    long timestamp = System.currentTimeMillis() / 1000L;
    OneSignal.sendTag("last_notification_click", timestamp);

    if (data != null) {
      notification_topic = data.optString("notification_topic", null);
      if (notification_topic != null)
        OneSignal.sendTag("notification_topic", notification_topic);
    }
  }
}

and here is the swift code:

let notificationOpenedBlock: OSHandleNotificationActionBlock = { result in
  let payload: OSNotificationPayload? = result?.notification.payload
  let timestamp = Int(NSDate().timeIntervalSince1970)
  OneSignal.sendTags(["last_notification_click": timestamp])
  
    if let notification_topic = payload.additionalData?["notification_topic"] {
        OneSignal.sendTag("notification_topic", value: notification_topic)
    }
}

check out this article from OneSignal.

Related