Publishing messages only to channel subscribers - iOS

Hello.
I am using the following code in app delegate to register device:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
 ..
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
..
}


func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
 self.backendless?.messaging.registerDeviceToken(deviceToken, responder: nil)
}

and then the following method for subscribing to a channel (which runs successfully)

func subscribe() {
 do {
 let responder = Responder(responder: self, selResponseHandler: #selector(self.subscribeResponseHandler), selErrorHandler: #selector(self.subscribeErrorHandler))
 let subscriptionOptions = SubscriptionOptions()
 let subscription: BESubscription? = backendless?.messagingService.subscribe(“somenewchannelname”, subscriptionResponder: responder, subscriptionOptions: subscriptionOptions)
 print("SUBSCRIPTION: \(subscription)")
 } catch let fault {
 print("FAULT = \(fault)")
 }
 }

and in business logic, i have deployed this code

@Asset( "UserPosts" )
public class UserPostsTableEventHandler extends com.backendless.servercode.extension.PersistenceExtender<UserPosts>
{
 
 @Override
 public void afterCreate( RunnerContext context, UserPosts userposts, ExecutionResult<UserPosts> result ) throws Exception
 {
 // add your code here
 Celebs celeb = userposts.getCeleb();
 String celebobjectid = celeb.getObjectId(); //this is the channel name
 BackendlessUser user = userposts.getUser();
 String msg = user.getProperty("name").toString() + " spotted " + celeb.getName().toString();
 
 PublishOptions publishOptions = new PublishOptions();
 MessageStatus status = Backendless.Messaging.publish(celebobjectid, (Object) msg, publishOptions );
 }
 
}

this doesn’t send any notification to devices that are subscribed.
If i remove the channel name from the code in business logic and write instead:

MessageStatus status = Backendless.Messaging.publish((Object) msg, publishOptions );

then this runs successfully and sends a push notif to everyone.
What am i doing wrong here?

Hi Zeba,

Is the goal to send a push notification to a channel (which will deliver to all devices subscribed to the channel) or send it to a specific device?

Regards,
Mark

Hi, I want to send to a channel which will deliver to all devices subscribed to this channel.

In your code you subscribe to “somenewchannelname”

backendless?.messagingService.subscribe(“somenewchannelname”…

Then when you publish a message, it is sent to a channel which is named after objectId of the Celebs object. Is that intentional? Seems like the disconnect in logic is right there.

I wrote the name somenewchannelname just to ease it for u to understand. Im creating channel name by the objectID string, the same one that is passed while publishing (i checked them for equality)

Have you verified in Backendless console if devices show up for the right channel names as expected?

Ok I checked, the channel name shows in the list on the left pane, but no devices show up inside it. Devices are visible only in the “default” channel.

Thanks for checking. It seems like the root cause of the problem - devices register with “default” even though you specify a different channel. As a workaround, you could try using subtopics with the Default channel. Each device would subscribe to a specific subtopic (could use the same naming convention as now - using Celebs objectId). Please let me know if that does the trick.

Hi, thank you so much, I did the subscription code, now I am changing the business logic code now and checking, but is it possible to check in console if the device successfully subscribed to the subtopic? i cannot see any change.

Unfortunately no - console does not show subtopic information. We will improve that in version 4 of Backendless.

YES! It worked!! Thank you so much! :smiley: