[英]How to store device tokens in our database for push notifications?
We're in the process of setting up push notifications for our app and have found out that we'll run into a couple issues if we store the users device token in the users table. 我们正在为我们的应用设置推送通知,发现如果将用户设备令牌存储在users表中,则会遇到一些问题。
Our users table: 我们的用户表:
| id (user pk) | ... other fields | device_token (for push notifs) |
The other 2 solutions would be to: 其他两种解决方案是:
I'm wondering how others do this & whether these solutions can be improved in any way? 我想知道其他人怎么做以及这些解决方案是否可以通过任何方式进行改进?
i think you should try : Store a JSON array in the users table this way is clear and best for optimize Json document be like : 我认为您应该尝试:用这种方式将JSON数组存储在users表中是清楚的,最适合优化Json文档,例如:
{
user_id : "user id",
list_devices_token : [....];
}
It's not clear what database you're using, but if it's relational (like MySQL) than a JSON array would violate the first normal form of good database design. 目前尚不清楚您使用的是哪个数据库,但是如果它是关系数据库(例如MySQL),则比JSON数组会违反良好数据库设计的第一种常规形式 。 And is, in my opinion, a non-starter.
在我看来,这是一个入门者。
Storing a single device id on the user table is an ok solution considering you said you'd only ever send a push notification to one device. 考虑到您说过只会向一台设备发送推送通知,因此在用户表上存储单个设备ID是一个不错的解决方案。 But there are two downsides to this apporach in my opinion (not to say it's wrong , but just things to consider):
但是在我看来,此方法有两个缺点(并不是说这是错误的 ,而是要考虑的事情):
app_version
. app_version
添加至少一列。 Extend this further ( date_last_notified
? device_type
? etc) and you've suddenly added a lot of extra columns to your user table that might not relate to the user entity as clearly. date_last_notified
? device_type
?等),您突然在用户表中添加了很多额外的列,这些列可能与用户实体显然不相关。 This would violate the 2nd normal form of relational database design because your new user columns will no longer describe the user entity. The extra table for device ids referring to users does add some complexity, but this is what relational databases were designed for. 指向用户的设备ID的额外表确实增加了一些复杂性,但这就是关系数据库的设计目的。
My recommendation is to give it a separate table now, but if you're going build it with a column on the users table that's fine too - just be prepared (and fearless) when the time comes to refactor. 我的建议是现在为它提供一个单独的表,但是如果您要在用户表上使用一列来构建它,也很好-在重构的时候就做好准备(并且毫不畏惧)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.