Java生鲜电商平台-通知模块设计与架构
说明:对于一个生鲜的B2B平台而言,通知对于我们实际的运营而言来讲分为三种方式:
1. 消息推送:(采用极光推送)
2. 主页弹窗通知。(比如:现在有什么新的活动,有什么新的优惠等等)
3. 短信通知.(对于短信通知,这个大家很熟悉,我们就说下我们如何从代码层面对短信进行分层的分析与架构)
1. 消息推送
说明:目前市场上的推送很多,什么极光推送,环信,网易云等等,都可以实现秒级别的推送,我们经过了市场调研与稳定性考察,最终选择了极光推送。
极光推送,市面上有很大的文档与实例,我这边就不详细讲解了,因为文档很清晰,也的确很简单。
相关的核心功能与代码如下:
1. 功能划分
1.1向所有的人推送同一个消息。
1.2 具体的某个人,或者某类人推送消息,自己简单的进行了一个SDK等封装
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
/**
* 激光推送
*/
public class Jdpush {
private static final Logger log = LoggerFactory.getLogger(Jdpush.class);
// 设置好账号的app_key和masterSecret
public static final String APPKEY = "";
public static final String MASTERSECRET = "";
/**
* 推送所有
*/
public static PushPayload buildPushObjectAndroidIosAllAlert(String message){
return PushPayload.newBuilder()
.setPlatform(Platform.android_ios())
.setAudience(Audience.all())//推送所有;
.setNotification(Notification.newBuilder()
.addPlatformNotification(AndroidNotification.newBuilder()
.addExtra("type", "infomation")
.setAlert(message)
.build())
.addPlatformNotification(IosNotification.newBuilder().setSound("callu")
.addExtra("type", "infomation")
.setAlert(message)
.build())
.build())
.setOptions(Options.newBuilder()
.setApnsProduction(false)//true-推送生产环境 false-推送开发环境(测试使用参数)
.setTimeToLive(90)//消息在JPush服务器的失效时间(测试使用参数)
.build())
.build();
}
/**
* 推送 指定用户集合;
*/
public static PushPayload buildPushObjectAndroidIosAliasAlert(List userIds,String message){
return PushPayload.newBuilder()
.setPlatform(Platform.android_ios())
.setAudience(Audience.alias(userIds))//推送多个;
.setNotification(Notification.newBuilder()
.addPlatformNotification(AndroidNotification.newBuilder()
.addExtra("type", "infomation")
.setAlert(message)
.build())
.addPlatformNotification(IosNotification.newBuilder().setSound("callu")
.addExtra("type", "infomation")
.setAlert(message)
.build())
.build())
.setOptions(Options.newBuilder()
.setApnsProduction(false)//true-推送生产环境 false-推送开发环境(测试使用参数)
.setTimeToLive(90)//消息在JPush服务器的失效时间(测试使用参数)
.build())
.build();
}
/**
* 推送单个人;
*/
public static PushPayload buildPushObjectAndroidIosAliasAlert(String userId,String message){
ret