简体   繁体   English

如何在我们的数据库中存储设备令牌以进行推送通知?

[英]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表中,则会遇到一些问题。

  1. The user will only be able to receive push notifs on the one device (they could be logged in multiple) 用户将只能在一台设备上接收推送通知(它们可以多次登录)

Our users table: 我们的用户表:

| id (user pk) | ... other fields | device_token (for push notifs) |

The other 2 solutions would be to: 其他两种解决方案是:

  1. Store a JSON array in the users table 将JSON数组存储在users表中
  2. Create a second table called user_devicetokens & link the user_id & device_token + any other settings which may be helpful. 创建另一个名为user_devicetokens的表,并链接user_iddevice_token +其他可能有用的设置。

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): 但是在我看来,此方法有两个缺点(并不是说这是错误的 ,而是要考虑的事情):

  • Are you sure your concerns won't change? 您确定您的担忧不会改变吗? What if you wanted to push a notification to drive the user to update their app? 如果您想推送通知以驱动用户更新其应用程序怎么办? Surely you'd want to send them to every device they had it installed on. 当然,您希望将它们发送到安装了该设备的每个设备。 This won't work with one column storing just one device id. 这不适用于仅存储一个设备ID的一列。
  • Or what if you decide you want to also be recording what app version they're on? 或者,如果您决定还想要记录他们所使用的应用程序版本,该怎么办? This would necesitate adding at least one more column to your user table for 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_notifieddevice_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.

相关问题 我应该存储用于发送推送通知的Android设备令牌吗? - Should I store Android device tokens for sending push notifications? 如何使用云功能将推送通知发送到具有FCM令牌的多个设备 - How to send push notifications to multiple device with FCM tokens using cloud functions 存储多对令牌/设备用于推送通知 - Store multiple pair token/device for push notifications FCM-通过设备令牌和环境/组发送android推送通知 - FCM - Sending android push notifications by device tokens and environment/group 如何将解析频道与我们的API同步以正确提供推送通知 - How to sync Parse channels with our API to serve push notifications correctly 如何在我的android设备上实现推送通知 - How to implement push notifications on my android device 如何获取用于推送通知的 Android 设备令牌? - How to get Android device token for push notifications? 如何在Amazon设备场上测试推送通知 - How to test push notifications on amazon device farm 如何将推送通知发送到Android中的其他设备? - how to send push Notifications to other device in android? Android推送通知 - 如何获取设备ID - Android push notifications - how to get the device id
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM