java服务端极光推送一

来源:互联网 发布:hex 编辑器 c语言 编辑:程序博客网 时间:2024/05/01 22:58
先在极光推送官网上注册一个极光账号,创建应用,
需要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 {


/**
* 广播
*@parammessage
*/
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();
}

/**
* 推送消息到个人
*@paramuser_id
*@parammessage
*/
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();
}



}



0 0
原创粉丝点击