社交产品中的消息推送设置
1.平台所有用户默认打开接受推送通知
2.如个别用户关闭某个消息设置,则不产生跟他相关的消息推送
问:
这块业务功能怎么实现,有sf小伙伴能提下实现思路的没? 谢过 ^_^
假设有10个配置项,id是从1到10,数据库只存用户操作过的数据,一个id是一条数据
type | user_id | active |
---|---|---|
1 | 1 | 0 |
3 | 1 | 0 |
6 | 1 | 0 |
那么查询这个用户的数据库数据得到
const userSwitches = [
{"type": 1, "active": 0},
{"type": 3, "active": 0},
{"type": 6, "active": 0},
];
组装数据
const switches = Array.from({length: 10}, (v, index) => {
const type = index + 1;
return userSwitches.find(item => item.type === type)
|| {type, "active": 1},
});
就能得到数据
[
{"type": 1, "active": 0},
{"type": 2, "active": 1},
{"type": 3, "active": 0},
{"type": 4, "active": 1},
{"type": 5, "active": 1},
{"type": 6, "active": 0},
{"type": 7, "active": 1},
{"type": 8, "active": 1},
{"type": 9, "active": 1},
{"type": 10, "active": 1},
]
###up 置顶。
###差不多这个思路:
notify_settings:
id (主键ID)
pid (默认为0,防止一个设置下面存在多子设置)
uid (当然用户ID了)
key (设置通知标示,确保唯一性,用户前端的硬编码具体的设置项,用key的目的是更加友好)
extra (冗余扩展字段)
status(当然status可以可以是多种状态不局限关闭和开启根据业务自动扩展)
其实我觉得可以建立一张用户消息提醒设置表,大概每个提醒项是一个字段,默认是开启的.
表字段大概如下
uid | week_tip_status | talk_status | friend_request_status | ... |
---|---|---|---|---|
1 | 1 | 1 | 1 | |
2 | 1 | 1 | 1 | |
3 | 1 | 1 | 1 |
开启状态是1,关闭是0
然后封装/定义一个模型,加上常用操作:
<?php
class TipsStatusModel
{
const WEEK_TIP_FIELD_NAME = 'week_tip_status';
const TALk_FIELD_NAME = 'talk_status';
const FRIEND_REQUEST_FIELD_NAME = 'friend_request_status';
/**
* 是否可以发送指定提醒
*
* @param $uid 用户id
* @param $statusName 状态字段名
* @return mixed
*/
public function isCanSend($uid, $statusName)
{
return Model::where('uid', $uid)->value($statusName);
}
}
在发送提醒之前调用是否可以发送提醒的方法:
// 返回0表示提醒关闭 直接返回即可
if (!$this->tipsStatusModel->isCanSend(1, TipsStatusModel::WEEK_TIP_FIELD_NAME))
{
return;
}
// todo
// 发送提醒逻辑
定义常量和字段名,其他位置调用的时候,不用写字段名.
如果字段名改变了的话,只需要修改const变量对应的值就好了.