How to send push notifications to multiple devices (belonging to one user) if device token changes?

2k views Asked by At

The case is this:

  1. A user logs in to the app on their iPhone A
  2. The user login in to the same app on their iPhone B

Now, in the database, there are 2 tokens for one user, that is correct.

+---------+-------------------+
| user_id |   device_token    |
+---------+-------------------+
|       1 | 1st_device_token1 |
|       1 | 2nd_device_token1 |
+---------+-------------------+

Say, that user deleted the app, installed it again (on both devices) and that means, tokens in APNs changed, therefore in our database, there are 4 unique tokens.

+---------+-----------------------+
| user_id |     device_token      |
+---------+-----------------------+
|       1 | 1st_device_token1     |
|       1 | 2nd_device_token1     |
|       1 | 1st_device_token1_new |
|       1 | 2nd_device_token1_new |
+---------+-----------------------+

Now, a push notification for that user is going to be sent to 4 devices (4 tokens). If they keep deleting and installing the app (may happen :) ), I would have in the database a large amount of tokens for one user and think, that they have such a vast number of devices, while in reality, there are a lot of invalid tokens.

How to prevent that?

1

There are 1 answers

0
WFHM On

Pushwoosh periodically clears its database from invalidated tokens (once per day or so), so having a "vast number" of entries should not be a problem. As for sending pushes to multiple devices of one user, there is a specific Push by UserID feature for this case.

The idea is to set a particular UserID on a login event and reset it to a default value on a logout with the following method:

[[PushNotificationManager pushManager] setUserId:@“external_user_1”]

Once UserID is set, you can send your /createMessage API requests with "users" parameter:

{
  "request": {
    "application": "APPLICATION_CODE",
    "auth": "API_ACCESS_TOKEN",
    "notifications": [{
      "send_date": "now",
      "content": "Hello world!",
      "users":["external_user_1"]
    }]
  }
}

This feature is accessible for all payment plans, so it should be accessible for you.