jpush app 消息推送项目 实例

来源:互联网 发布:galgame机翻软件 编辑:程序博客网 时间:2024/06/07 07:37

1、jpush 推送 用的比较多的是 别名:alias 以及 标签 tag 推送

2、下载jpush-client-3.2.3.jar 

3、appkey、Secret 在数据库配置、开放如下接口

       public void pushToUser(String appid, String userId, MobilePushEntity pushEntity);

public void pushToUserList(String appid, List<String> userIdList, MobilePushEntity pushEntity);

public void pushToDevice(String appid, List<String> deviceTokenList, MobilePushEntity pushEntity);

public void pushToApp(String appid, MobilePushEntity pushEntity);

4、代码结构如下:


3、核心推送代码如下:

// 如果是IOS,设置平台特性
if (deviceType.equals(DeviceType.IOS)) {


// IOS内容的长度限制
String notificationContent = pushEntity.getMsgContent();
if (StringUtils.isNotBlank(notificationContent) && notificationContent.getBytes().length > 70) {
notificationContent = StringUtilsEx.substringb(notificationContent, 70) + "...";
}


IosNotification.Builder iosNotificationBuilder = IosNotification.newBuilder().setAlert(notificationContent).setBadge(1)
.setSound("default");


for (String key : extrasMap.keySet()) {
iosNotificationBuilder.addExtra(key, String.valueOf(extrasMap.get(key)));
}


payloadBuilder.setNotification(Notification.newBuilder().addPlatformNotification(iosNotificationBuilder.build()).build());
} else {


AndroidNotification.Builder androidNotificationBuilder = AndroidNotification.newBuilder()
.setAlert(pushEntity.getMsgContent()).setTitle(pushEntity.getMsgTitle());


for (String key : extrasMap.keySet()) {
androidNotificationBuilder.addExtra(key, String.valueOf(extrasMap.get(key)));
}


payloadBuilder.setNotification(Notification.newBuilder().addPlatformNotification(androidNotificationBuilder.build())
.build());
}


PushPayload pushPayload = payloadBuilder.build();


for (String[] jpushInfo : JpushInfoList) {
try {
JPushClient jPushClient = new JPushClient(jpushInfo[0], jpushInfo[1], iosMode,
(pushEntity.getJpushTimeToLive() == null ? 86400 : pushEntity.getJpushTimeToLive()));
jPushClient.sendPush(pushPayload);
} catch (Exception e) {


// 个推时如果手机端没有注册用户,不打错误日志
if (e.getMessage().indexOf("\"code\": 1011") == -1) {
logger.error("JPUSH推送消息时发生异常:[" + e.getMessage() + "]", e);
}
}
}
} catch (Exception e) {
if (e.getMessage().indexOf("\"code\": 1011") == -1) {
logger.error("JPUSH推送消息时发生异常:[" + e.getMessage() + "]", e);
}
}

3、整个项目采用动态配置jdbc、spring  如需源码 请联系我 qq 451366323。
0 0
原创粉丝点击