极光推送(Jpush)实践

  JPush 是经过考验的大规模 App 推送平台,每天推送消息数超过 5 亿条。 开发者集成 SDK 后,可以通过调用 API 推送消息。同时,JPush 提供可视化的 web 端控制台发送通知,统计分析推送效果。 JPush 全面支持 Android, iOS, Winphone 三大手机平台。

    具体内容,请看:http://docs.jiguang.cn/jpush/guideline/intro/(官方文档)

一.导入依赖  

<!--激光推送-->
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jiguang-common</artifactId>
<version>1.1.6</version>
<!--<version>1.0.8</version>-->
</dependency>
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.3.12</version>

</dependency>

二.工具类

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;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
public class JpushUtil {
private static final String appKey = 极光key
private static final String masterSecret = 密钥
private static final String wyAppKey = 极光key
private static final String wyMasterSecret =密钥


/**
* 发送给所有用户
* notificationTitle 通知内容标题
* @param notificationContent 消息内容标题
*/
public static int wxSendToRegistrationId(String regid, String notificationTitle, String notificationContent, Map<String, String> map) {
log.info("推送给极光的注册id:" + regid);
int result = 0;
JPushClient jpushClient = new JPushClient( wyMasterSecret,wyAppKey);
PushPayload payload = buildPushObject_all_registration_id_alertWithTitle(regid, notificationTitle, notificationContent, map);
try {
PushResult pushResult = jpushClient.sendPush(payload);
result = pushResult.getResponseCode();
} catch (APIConnectionException e) {
log.error("Connection error. Should retry later. ", e);
} catch (APIRequestException e) {
e.getErrorCode();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}

private static PushPayload buildPushObject_all_registration_id_alertWithTitle(String registration_id, String notificationTitle, String notificationContent, Map<String, String> map) {
return PushPayload.newBuilder()
//设置接受的平台 ,Platform.all()所有的,Platform.android()安卓,Platform.android_ios() 苹果
.setPlatform(Platform.all())
//指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id
.setAudience(Audience.registrationId(registration_id))//设置受众-极光注冊id
//jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发
.setNotification(Notification.newBuilder()
//指定当前推送的android通知
.addPlatformNotification(AndroidNotification.newBuilder()
//通知标题信息
.setAlert(notificationContent)
//通知内容
.setTitle(notificationTitle)
//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
.addExtras(map)
.build())
//指定当前推送的iOS通知
.addPlatformNotification(IosNotification.newBuilder()
//通知标题信息
.setAlert(notificationContent)
//此项是指定此推送的badge自动加1
.incrBadge(1)

.addExtras(map)
//此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,
// 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音
.setSound("sound.caf")

.setContentAvailable(true)
.build())
.build())

.setOptions(Options.newBuilder()
//此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
.setApnsProduction(true)
//此字段是给开发者自己给推送编号,方便推送者分辨推送记录
.setSendno(1)
//此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒
.setTimeToLive(86400)
.build())
.build();

}
三. 该例子是根据用户来推,根据用户id查出是否有极光注册id,有即推送,没有不推送
原文地址:https://www.cnblogs.com/yangxiaoli/p/12937449.html