java 极光推送

时间:2021-07-06 00:15:37

Web.xml配置文件

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/classes/applicationContext.xml,/WEB-INF/classes/applicationContext-everydaytuisong.xml</param-value>

</context-param>

<!-- 开启监听 -->

<listener>

<listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>

</listener>

applicationContext-everydaytuisong.xml配置文件

<!-- 配置极光推送定时任务 -->

<bean id="SchedulerTask3" class="org.springframework.scheduling.quartz.JobDetailBean">

<property name="jobClass">

<value>JPush.Jdpush</value>

</property>

</bean>

<!-- 配置定时时间 -->

<bean id="SchedulerTaskTrigger3" class="org.springframework.scheduling.quartz.CronTriggerBean">

<property name="jobDetail" ref="SchedulerTask3" />

<property name="cronExpression">

<value>0 50 11 * * ?</value>

<!-- 0 0/30 0/1 * * ? 代表每天每30分钟运行一次

0 0/3 0/1 * * ? 代表每天每3分钟运行一次

0 11 23 * * ? 代表每天晚上23:11运行一次 -->

</property>

</bean>

<!-- 开启定时任务 -->

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

<property name="triggers">

<list>

<ref bean="SchedulerTaskTrigger3" />

</list>

</property>

</bean>

Java 类

package JPush;

import org.quartz.JobExecutionContext;

import org.quartz.JobExecutionException;

import org.springframework.beans.factory.BeanFactory;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.scheduling.quartz.QuartzJobBean;

import cn.jpush.api.JPushClient;

import cn.jpush.api.common.resp.APIConnectionException;

import cn.jpush.api.common.resp.APIRequestException;

import cn.jpush.api.push.PushResult;

import cn.jpush.api.push.model.Message;

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.IosNotification;

import cn.jpush.api.push.model.notification.Notification;

import com.opensymphony.xwork2.util.logging.Logger;

import com.opensymphony.xwork2.util.logging.LoggerFactory;

/**

* @author Administrator

*

*/

/**

* @author Administrator

*

*/

public class Jdpush extends QuartzJobBean{

protected static final Logger LOG = LoggerFactory.getLogger(Jdpush.class);

// demo App defined in resources/jpush-api.conf

private static final String appKey = "c36a5a0b049f950ef8ba3bf6";

private static final String masterSecret = "57d5d83288d7e6472ce811c7";

public static final String TITLE = "Test from API example";

public static final String ALERT = "每日新品九点五十开抢,不容错过哦!";

public static final String MSG_CONTENT = "dsdsdsdsdsds";

public static final String REGISTRATION_ID = "555666";

public static final String TAG = "tag_api";

private static String[] tag1 = { "shenz", "nanshan" };

public static PushPayload buildPushObject_all_all_alert() {

return PushPayload.alertAll(ALERT);

}

private static BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");

@Override

protected void executeInternal(JobExecutionContext arg0)

throws JobExecutionException {

// TODO Auto-generated method stub

JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3);

// For push, all you need do is to build PushPayload object.

PushPayload payload = buildPushObject_all_all_alert();

PushResult result = null;

try {

result = jpushClient.sendPush(payload);

} catch (APIConnectionException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (APIRequestException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println("Got result - " + result);

}

}