在维护Bmob消息推送SDK的时候,有不少开发者会问到如何给某类用户进行推送,例如,在教学类的应用中你须要对60分如下的同窗进行推送提醒,鼓励他们努力学习。下边我就详细阐述一下:git
_User表结构github
属性 | 类型 | 说明 |
---|---|---|
username | String | 用户名 |
password | String | 密码 |
score | Integer | 该用户的分数 |
role | Integer | 该用户的角色,0:老师,1:学生 |
_Installation表结构数据库
属性 | 类型 | 说明 |
---|---|---|
installationId | String | 设备惟一标志,SDK自动生成 |
user | Pointer<_User> | 该设备当前登陆用户 |
在用户角色为老师的状况下,具备推送给其余客户端消息的功能,首先查询角色为学生的分数在60分如下的用户,在根据查询的结果做为推送的条件进行推送。(为了简单理解,学生的分数会在注册的时候自动随机生成,在实际项目中须要老师为学生打分。)后端
/** * 修改设备表的用户信息:先查询设备表中的数据,再修改数据中用户信息 * * @param user */ private void modifyInstallationUser(final User user) { BmobQuery<Installation> bmobQuery = new BmobQuery<>(); final String id = BmobInstallationManager.getInstallationId(); bmobQuery.addWhereEqualTo("installationId", id); bmobQuery.findObjectsObservable(Installation.class) .subscribe(new Action1<List<Installation>>() { @Override public void call(List<Installation> installations) { if (installations.size() > 0) { Installation installation = installations.get(0); installation.setUser(user); installation.updateObservable() .subscribe(new Action1<Void>() { @Override public void call(Void aVoid) { toastI("更新设备用户信息成功!"); } }, new Action1<Throwable>() { @Override public void call(Throwable throwable) { toastE("更新设备用户信息失败:" + throwable.getMessage()); } }); } else { toastE("后台不存在此设备Id的数据,请确认此设备Id是否正确!\n" + id); } } }, new Action1<Throwable>() { @Override public void call(Throwable throwable) { toastE("查询设备数据失败:" + throwable.getMessage()); } }); }
/** * 修改设备表的用户信息:先查询设备表中的数据,再修改数据中用户信息 */ private void modifyInstallationUser() { BmobQuery<Installation> bmobQuery = new BmobQuery<>(); final String id = BmobInstallationManager.getInstallationId(); bmobQuery.addWhereEqualTo("installationId", id); bmobQuery.findObjectsObservable(Installation.class) .subscribe(new Action1<List<Installation>>() { @Override public void call(List<Installation> installations) { if (installations.size() > 0) { Installation installation = installations.get(0); User user = new User(); installation.setUser(user); user.setObjectId(""); installation.updateObservable() .subscribe(new Action1<Void>() { @Override public void call(Void aVoid) { toastI("更新设备用户信息成功!"); /** * TODO 更新成功以后再退出 */ BmobUser.logOut(); startActivity(new Intent(mContext, LoginActivity.class)); finish(); } }, new Action1<Throwable>() { @Override public void call(Throwable throwable) { toastE("更新设备用户信息失败:" + throwable.getMessage()); } }); } else { toastE("后台不存在此设备Id的数据,请确认此设备Id是否正确!\n" + id); } } }, new Action1<Throwable>() { @Override public void call(Throwable throwable) { toastE("查询设备数据失败:" + throwable.getMessage()); } }); }
BmobQuery<User> bmobQueryUser = new BmobQuery<>(); bmobQueryUser.addWhereEqualTo("role",1); bmobQueryUser.addWhereLessThan("score",60); bmobQueryUser.findObjects(new FindListener<User>() { @Override public void done(List<User> list, BmobException e) { if (e==null){ for (User user:list){ BmobPushManager bmobPushManager = new BmobPushManager(); BmobQuery<BmobInstallation> query = BmobInstallation.getQuery(); query.addWhereEqualTo("user", user); bmobPushManager.setQuery(query); bmobPushManager.pushMessage("努力加油", new PushListener() { @Override public void done(BmobException e) { if (e == null) { Logger.e("推送成功!"); } else { Logger.e("异常:" + e.getMessage()); } } }); } }else { toastE(e.getMessage()); } } });
Demo下载地址:https://github.com/chaozhouzh...ide