java服务端极光推送一

先在极光推送官网上注册一个极光账号,创建应用,
需要ios或安卓人员提供~应用成功创建之后会生成二个字符码.
创建工具类:
需要在工具类中添加相应的jar
package hls.itms.common.general.JPush;
import cn.jiguang.common.ClientConfig;
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 org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
* Created by Administrator on 2017/1/10.
*/

public final class JPushHandler {


/**
* 广播
* @param message
*/
public static void broadCast(String appKey, String masterSecret, String title, String message){
ClientConfig config = ClientConfig. getInstance ();
JPushClient client = new JPushClient(masterSecret, appKey, null, config);
PushPayload payload = buildBroadCastPayload (title, message);
PushResult result = null;
try {
result = client.sendPush(payload);
// System.out.println("推送结果:" + result.getResponseCode());
} catch (Exception e1) {
}
}

private static PushPayload buildBroadCastPayload(String title, String message){
return PushPayload. newBuilder ()
.setPlatform(Platform. newBuilder ().setAll(true).build())
.setAudience(Audience. newBuilder ().setAll(true).build())
.setNotification(Notification. newBuilder ().
addPlatformNotification(IosNotification. newBuilder ()
.setBadge(1)
.setContentAvailable(false)
.setMutableContent(false)
.setSound("defalut")
.setAlert(message)
.setCategory(title)
.build()
)
.addPlatformNotification(AndroidNotification. newBuilder ()
.setAlert(message)
.setTitle(title)
.build())
.build())
.setOptions(Options. newBuilder ()
.setApnsProduction(true)
.setSendno(100000)
.setTimeToLive(86400)
.build()
)
.build();
}

/**
* 推送消息到个人
* @param user_id
* @param message
*/
public static void pushForOne(String appKey, String masterSecret, Long user_id, String title, String message){
ClientConfig config = ClientConfig. getInstance ();
JPushClient client = new JPushClient(masterSecret, appKey, null, config);
PushPayload payload = buildSinglePayload (user_id, title, message);
PushResult result = null;
try {
result = client.sendPush(payload);
// System.out.println("推送结果:" + result.getResponseCode());
} catch (Exception e1) {
}
}

private static PushPayload buildSinglePayload(Long user_id, String title, String message){
return PushPayload. newBuilder ()
.setPlatform(Platform. newBuilder ().setAll(true).build())
.setAudience(Audience. alias (user_id + ""))
.setAudience(Audience. tag (user_id + ""))
.setNotification(Notification. newBuilder ().
addPlatformNotification(IosNotification. newBuilder ()
.setBadge(1)
.setContentAvailable(false)
.setMutableContent(false)
.setSound("defalut")
.setCategory(title)
.setAlert(message)
.build()
)
.addPlatformNotification(AndroidNotification. newBuilder ()
.setAlert(message)
.setTitle(title)
.build())
.build())
.setOptions(Options. newBuilder ()
.setApnsProduction(true)
.setSendno(100000)
.setTimeToLive(86400)
.build()
)
.build();
}



}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值