The binary mask
Why using a binary mask?
It is used a bitmask in order to know which preferences the user has chosen. If you set one device per bit, when you have to check the option set by the user, you will only need to compare that option with the device mask.
For now, the Push Api only support 3 kind of target devices (emails, smartphones and chrome) so it is using 3 bits. In this case, it would be easy to know with an integer which option the user have set. The problem is, the more devices the Push Api supports, the more complexity to know in which devices the user wants to receive the Push.
Here are some examples in order to understand it:
- With 2 devices you can remember the 4 integers of the preferences and you can know what preferences have set the user. In this case, the masks used are 01 and 10.
| Mask | Integer | Target |
|---|---|---|
| 00 | 0 | none |
| 01 | 1 | |
| 10 | 2 | smartphone |
| 11 | 3 | all devices |
- With 4 devices (a, b, c, d) there are 15 different options to be set and it would be harder to remember the integers, the easiest option you have is to set a list of constants in order to know the meaning of each number. Using a bitmap, you only have to remember the masks and the comparison with the option should be easier:
| Mask | Integer | Target |
|---|---|---|
| 0000 | 0 | none |
| 0001 | 1 | a |
| 0010 | 2 | b |
| 0100 | 4 | c |
| 1000 | 8 | d |
| 0101 | 5 | only c and a |
| ... | ... | ... |
| 1101 | 13 | only d, c and |
| ... | ... | ... |
| 1111 | 15 | all devices |
Current bitmask
Here is the actual bitmask that Push Api is using:
| Target | Mask | Integer |
|---|---|---|
| None | 000 | 0 |
| 001 | 1 | |
| Smartphones | 010 | 2 |
| Chrome | 100 | 4 |
| All devices | 111 | 7 |
Updated less than a minute ago
