本文主要是介绍Jpush极光推送教程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、引入jar包
<dependency><groupId>cn.jpush.api</groupId><artifactId>jpush-client</artifactId><version>3.4.6</version>
</dependency>
2、Jpush官网申请密钥
3、代码部分
public interface JPushService {Result pushData(Map<String,Object> map);}
@Service
public class JPushServiceImpl extends BaseService implements JPushService {@Value("${jpush.appKey}")private String appKey;@Value("${jpush.masterSecret}")private String masterSecret;@Overridepublic Result checkVaild(Object obj) {return setResult(obj);}/*** 推送数据* @param map* @return*/@Overridepublic Result pushData(Map<String,Object> map) {if(map == null){return setResult("controller1001");}return sendToAll(map.get("content"),String.valueOf(map.get("registrationId")));}/*** 发送给所有Android用户*/public PushResult sendToAndroid(String content, String title, Map<String, String> extras) {JPushClient pushClient = new JPushClient(masterSecret, appKey);PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.android()).setAudience(Audience.all()).setNotification(Notification.android(content, title, extras)).build();try {return pushClient.sendPush(payload);} catch (APIConnectionException | APIRequestException e) {e.printStackTrace();}return null;}/*** 发送给所有Ios用户*/public PushResult sendToIos(Object alert, Map<String, String> extras) {JPushClient pushClient = new JPushClient(masterSecret, appKey);PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.ios()).setAudience(Audience.all()).setNotification(Notification.ios(alert, extras)).build();try {return pushClient.sendPush(payload);} catch (APIConnectionException | APIRequestException e) {e.printStackTrace();}return null;}/*** 发送给所有用户*/public Result sendToAll(Object alert,String registrationId) {System.out.println(masterSecret+" = "+appKey);JPushClient pushClient = new JPushClient(masterSecret, appKey);PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.registrationId(registrationId)).setNotification(Notification.alert(alert)).build();try {return setResult(pushClient.sendPush(payload));} catch (APIConnectionException | APIRequestException e) {return setResult(-1,"error", JSONObject.parse(e.getMessage()));}}
}
这篇关于Jpush极光推送教程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!