0

I'm reading over this previous thread because I was having a problem converting the device token to something I could send in a POST. That problem is solved, and my token is being sent properly to the server, which we store in a user's account record.

That is all I need to do, right? If we notify using that token, it will go to that user, and only that user? Or am I missing something really critical here?

Community
  • 1
  • 1
Maury Markowitz
  • 8,303
  • 8
  • 36
  • 79

2 Answers2

1

When you say 'user' do you mean that your app can have different users log in and out?

To handle this, you can set up a table to map user_id to push_token and device_id. You should also add an enabled flag to this map - which means the user is currently logged in on that device.

Conceptually, while a device has only one push token, it may have many users, and a user may have many devices.

When a user signs into your app on a device you send user_id, push_token and device_id and set enabled=TRUE in your table. If this user/device combo never existed, add to your table.

When a user signs out of your app on a device you send user_id and device_id and set enabled=FALSE in your table.

When an event occurs that would trigger a push to user with user_id then you can look up what push_tokens are associated to that user_id and enabled=TRUE. Then you can simply push to those tokens.

dmorrow
  • 3,734
  • 2
  • 17
  • 30
  • There is only one user per device, and they are only on one device at one time. The data in the app comes from a service which requires login on that service, it's a JSON server. So there are accounts on the server, although no one really "logs in or out", right now it's just a list of meaningless userids and their device tokens. I guess my question has to do with push_token and device_id. Are these not the same thing? – Maury Markowitz Feb 03 '15 at 18:58
  • If the user has to log into the service, they could log in on two devices. This is why you need `device_id` as well. `device_id` is essentially UDID - a unique identifier for the device. Per the Apple docs, `push_token` is not guaranteed to remain fixed either. Typically, its good to update it on every log in. Also, if a user logs out of the service and another logs in on the same device, then both will have the same `push_token`. You don't want user1 getting pushes about user2's activity. Similarly, you do want user1 getting push notifications on any device they are logged in to. – dmorrow Feb 03 '15 at 23:08
  • OK, but not knowing anything on the server side, what does the server have to send to Apple? Both of these, or can it do it with just one. Sadly I don't have a lot of control over the API. – Maury Markowitz Feb 04 '15 at 18:30
  • The server just sends the `push_token` and the payload json – dmorrow Feb 04 '15 at 21:23
0

@dmorrow is right. In addition, you need to make a .pem certificate with the same app id to make server push notifications work, check this ANSWER

Community
  • 1
  • 1
aramusss
  • 2,271
  • 18
  • 30
  • I have the PEM already, we used it with Localytics... which I think answers my question because it could send messages to specific users. – Maury Markowitz Feb 03 '15 at 19:00