Subscribe to Topics


Optionally subscribe the user to one or more topics to target multiple users with a shared interest when sending notifications.

Depending on your app's notification criteria, you may be able to leverage topics to simply the process of sending the same notification to multiple users. If your app only sends personalized notifications, skip this step and simply target individual users by their unique device tokens.

Please select your iOS project type for the appropriate instructions:

Swift
Objective-C

Simply add the following code to subscribe the user to a topic:

// Initialize Pushy SDK
let pushy = Pushy(UIApplication.shared)

// Subscribe the user to a topic
pushy.subscribe(topic: "news", handler: { (error) in
    // Handle errors
    if error != nil {
        return print("Subscribe failed: \(error!)")
    }
    
    // Subscribe successful
    print("Subscribed to topic successfully")
})
// Initialize Pushy SDK
Pushy* pushy = [[Pushy alloc]init:[UIApplication sharedApplication]];

// Subscribe the user to a topic
[pushy subscribeWithTopic:@"news" handler:^(NSError *error) {
    // Handle errors
    if (error != nil) {
        return NSLog(@"Subscribe failed: %@", error);
    }
    
    // Subscribe successful
    NSLog(@"Subscribed to topic successfully");
}];

Note: Replace news with your own case-sensitive topic name that matches the following regular expression: [a-zA-Z0-9-_.]+. You may also pass in a string array with multiple topics.


You can then notify multiple users subscribed to a certain topic by specifying the topic name (prefixed with /topics/) as the to parameter in the Send Notifications API.