uni-app中消息推送⽅案(⼀)
笔者最近有个需求:pc端创建任务后需要实时下发到app端,即安装app的⽤户在任务创建之后能实时收到通知。
⽬前只实现了app在线时可以实时接收到通知,离线的话需要再次打开app后补发推送
因为项⽬⼀直是基于uni-app开发的,故利⽤个推的进⾏开发
话不多说,直接开⼲:
STEP1:登录个推开发者中⼼,注册应⽤,获取AppID、AppKey、MasterSecret等参数。,如果是DCloud uniPush的⽤户,获取AppID、AppKey、MasterSecret等信息。
STEP2:集成服务端SDK,
<dependencies>
<!--unipush-->
<dependency>
<groupId&in.platform</groupId>
<artifactId>gexin-rp-sdk-base</artifactId>
<version>4.0.0.30</version>
</dependency>
<dependency>
<groupId&in.platform</groupId>
<artifactId>gexin-rp-sdk-template</artifactId>
<version>4.0.0.24</version>
</dependency>
<dependency>
<groupId&in.platform</groupId>
<artifactId>gexin-rp-sdk-http</artifactId>
<version>4.1.0.5</version>
</dependency>
<dependency>
<groupId&in.platform</groupId>
<artifactId>gexin-rp-fastjson</artifactId>
<version>1.0.0.3</version>
</dependency>
<dependency>
<groupId&le.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>2.5.0</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>getui-nexus</id>
<url>ui/nexus/content/repositories/releases/</url>
</repository>
</repositories>
STEP3:查看接⼝说明,按需调⽤
笔者使⽤的是【】执⾏批量推
1. 先定义实体bo
public class PushStyle {
private String title;//通知栏标题
private String text;//通知栏内容
private String logo;//通知的图标名称,包含后缀名(需要在客户端开发时嵌⼊),如“icon.png”⼩LOGO,默认push_small.png,需要提前内置到客户端private String logoUrl;//通知栏⽹络图标
private String channel;//通知渠道id
private String channelName;//通知渠道名称
private Integer channelLevel;//通知渠道重要性具体值有0、1、2、3、4;设置之后不能修改;具体展⽰形式如下:0:⽆声⾳,⽆震动,不显⽰。(不推荐) //1:⽆声⾳,⽆震动,锁屏不显⽰,通知栏中被折叠显⽰,导航栏⽆logo。
//2:⽆声⾳,⽆震动,锁屏和通知栏中都显⽰,通知不唤醒屏幕。
/
/3:有声⾳,有震动,锁屏和通知栏中都显⽰,通知唤醒屏幕。(推荐)
//4:有声⾳,有震动,亮屏下通知悬浮展⽰,锁屏通知以默认形式展⽰且唤醒屏幕。(推荐)
//以下两种⽅式选⼀种
private String bigImageUrl;//通知展⽰⼤图样式,参数是⼤图的URL地址
private String bigText;//通知展⽰⽂本+长⽂本样式,参数是长⽂本
public String getTitle(){
return title;
}
public void setTitle(String title){
this.title = title;
}
public String getText(){
return text;
}
public void setText(String text){
< = text;
}
public String getLogo(){
return logo;
}
public void setLogo(String logo){
this.logo = logo;
}
public String getLogoUrl(){
return logoUrl;
}
public void setLogoUrl(String logoUrl){
this.logoUrl = logoUrl;
}
public String getChannel(){
return channel;
}
public void setChannel(String channel){
this.channel = channel;
}
public String getChannelName(){
return channelName;
}
public void setChannelName(String channelName){
this.channelName = channelName;
}
public Integer getChannelLevel(){
return channelLevel;
}
public void setChannelLevel(Integer channelLevel){
this.channelLevel = channelLevel;
}
public String getBigImageUrl(){
return bigImageUrl;
}
public void setBigImageUrl(String bigImageUrl){ this.bigImageUrl = bigImageUrl;
}
public String getBigText(){
return bigText;
}
public void setBigText(String bigText){
this.bigText = bigText;
}
/*设定默认值*/
public PushStyle(){
this.logoUrl="";
this.channel ="Default";
this.channelName ="Default";
this.channelLevel =3;
this.bigImageUrl="";
this.bigText="";
}
}
2.然后定义⼀个配置类
@Configuration
public class PushConfig {
public static String appId;
public static String appKey;
public static String masterSecret;
public static String host;
public static long offlineExpireTime;//过多久该消息离线失效(单位毫秒)⽀持1-72⼩时*3600000毫秒
@Value("${spring.push.appId}")
public void setAppId(String appId){
PushConfig.appId = appId;
}
@Value("${spring.push.appKey}")
public void setAppKey(String appKey){
PushConfig.appKey = appKey;
}
@Value("${spring.push.masterSecret}")
public void setMasterSecret(String masterSecret){
PushConfig.masterSecret = masterSecret;
}
@Value("${spring.push.host}")
public void setHost(String host){
PushConfig.host = host;
}
@Value("${spring.push.offlineExpireTime}")
public void setOfflineExpireTime(long offlineExpireTime){
PushConfig.offlineExpireTime = offlineExpireTime;
}
}
3.接着定义⼀个⼯具类
@Component
public class PushHelper {
/**
* 批量推送
* @param pushStyle 通知样式
* @param appPushList 通知范围存储clientId字符串
*/
public void pushMessage(PushStyle pushStyle, List<String> appPushList){
System.setProperty("gexin_pushList_needDetails","true");
IGtPush push =new IGtPush(PushConfig.host, PushConfig.appKey, PushConfig.masterSecret);        NotificationTemplate template =getNotificationTemplate(pushStyle);
// 采⽤toList⽅案,定义ListMessage消息类型
ListMessage message =new ListMessage();
message.setData(template);
message.setOffline(true);
message.setOfflineExpireTime(PushConfig.offlineExpireTime);// 时间单位为毫秒
String contentId = ContentId(message);
flutter uniapp 哪个好// 获取推送⽬标
List targets =new ArrayList();//推送⽬标列表,单次推送数量限制1000以内
for(String ap : appPushList){
Target target =new Target();
target.setAppId(PushConfig.appId);
target.setClientId(ap);
targets.add(target);
}
}
IPushResult ret = push.pushMessageToList(contentId, targets);
System.out.Response().toString());
}
/**
* 推-即所有安装app的可执⾏⽤户
* 注:也可以设定筛选条件,择出符合要求的推送⽤户
* @param pushStyle 通知样式
*/
public void pushToApp(PushStyle pushStyle){
System.setProperty("gexin_pushList_needDetails","true");
IGtPush push =new IGtPush(PushConfig.host, PushConfig.appKey, PushConfig.masterSecret);
NotificationTemplate template =getNotificationTemplate(pushStyle);
// 采⽤toApp⽅案,AppMessage
List<String> appIds =new ArrayList<String>();
appIds.add(PushConfig.appId);
AppMessage message =new AppMessage();
message.setData(template);
message.setAppIdList(appIds);
message.setOffline(true);
message.setOfflineExpireTime(PushConfig.offlineExpireTime);// 时间单位为毫秒
/
/执⾏推送
IPushResult ret = push.pushMessageToApp(message);
System.out.Response().toString());
}
//配置通知模板
private static NotificationTemplate getNotificationTemplate(PushStyle pushStyle){
NotificationTemplate template =new NotificationTemplate();
// 设置APPID与APPKEY
template.setAppId(PushConfig.appId);
template.setAppkey(PushConfig.appKey);
if(StringUtil.BigImageUrl())&& StringUtil.BigText())){//系统样式            Style0 style =new Style0();
// 设置通知栏标题与内容
style.Title());
style.Text());
// 配置通知栏图标
//        style.setLogo("icon.png");
// 配置通知栏⽹络图标
style.LogoUrl());
// 设置通知是否响铃,震动,或者可清除
style.setRing(true);
style.setVibrate(true);
style.setClearable(true);
style.Channel());
style.ChannelName());
style.ChannelLevel());//设置通知渠道重要性
template.setStyle(style);
}else{//⼤图+⽂本样式
Style6 style =new Style6();
style.Title());
style.Text());
style.LogoUrl());
style.BigImageUrl());
style.BigText());
style.setRing(true);
style.setVibrate(true);
style.setClearable(true);
style.Channel());
style.ChannelName());
style.ChannelLevel());
template.setStyle(style);
}