Send Push to single device based on entry in Users table - JS API

I want to send a push to a specific device based on a selected user in the list of registered users in the Backendless database. From the documentation, it seems that you can only specify a single device for a push by its Device ID but there is no way to match a device ID to a user. How would you recommend achieving this function given the current Backendless setup. I wasn’t able to find a complete list of deliveryOps in the docs so I’m not sure if other options exist for targeting a specific device.

Thanks.

I found the core class docs defining the deliveryOps options but that didn’t tell me anything new. I still would like to know what the best approach to this problem is.

Thanks!

When a device is being registered with the API you can get the device token and store it as a custom user property. That will give you a mapping between users and their corresponding devices.

Regards,
Mark

I solved this quite easy.

Whenever you Register a device for push messages, subscribe to the channel named by “userId” of logged in user. Result:You can now send push messages to userId channels. No need to store anything in custom properties.

Thanks for the solutions. I’m not sure I like the idea of having hundreds of channels with a single subscriber but I’ll look into both approaches.

If we store the device token after registration in the users table as a property, and the user has 2 devices,
that will override the device token, if the user switches devices right?
Does that mean that the user is be able to receive the notification only to the device he is currently logged in?

Maybe you could treat the property in the table like a list of some sort? It’s a bit hacky but you could probably make it work. In my case, I know the user will only be using a single device so this won’t be an issue for me.

Having a single channel with their user id per user is what I am doing now, but it seems a bit odd…
Any suggestions/comments ? Is that a good practice?

Yep, just treat it as array.

It really sounds odd. The question is, what are your costs of a call. In case of userId channels your costs to estimate the correct channel are zero. If you need to fetch a user object first that’s an extra call. I don’t See any disadvantages in using the odd approach, do you?

It just seems strange to have like 5000 different channels (or I don’t know how many depending on the users that will use my app) that’s all.

@Mark
Are there any limitations to channels? Any problems having 10 Million channels?

@Mark (or anyone else who might know)

In regards to the other method of adding a custom property to each user; I get a Fault 3100: “user is not exist” error when calling userUpdate. Do you have any idea why this might be? I pass the ‘currentUser’ in with ‘[Backendless sharedInstance].userService.currentUser’ after logging them in. I just can’t figure out why it would think the user doesn’t exist when I am specifically referring to a Backendless user and can see all of the properties in the debugger.

Thanks.

Also, great Webinar. Enjoyed the discussion.

Not a big deal…

Hi Brandon,

Could you please open a separate topic and post some sample code demonstrating what you’re doing when you’re getting that error?.. (with bad grammar which we will fix asap)

Regards,
Mark

I was actually able to solve it on my own yesterday, thanks. It was an issue with a variable name clashing with one of Backendless’ built in variable names. It was very weird but changing my variable name fixed it.