极光推送2

package com.jpxx.jpwgh.util;

import org.slf4j.LoggerFactory;

import org.slf4j.Logger;

import com.alibaba.fastjson.JSONObject;

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

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.audience.AudienceTarget;

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

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

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

/**

*

* 极光推送消息提醒工具类

*/

public class JPushUtil {

/**

* JPush appkey 极光推送平台生成的key值

*/

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

private static String AppKey="0893720e15af3422427211c5";

/**

* JPush MasterSecret 极光推送平台生成的密钥

*/

private static String MasterSecret="95ad282852bb57184b2d1fde";

/**

* 向客户端设备发送通知方法--regid

* @param regid  设备标识码

* @param content  消息内容

* @param id        消息id

* @param type      消息类型

*/

private static PushPayload Notifiton(String regid, String id,String content, String type) {

return PushPayload.newBuilder()

                .setPlatform(Platform.android_ios())

                .setAudience(Audience.registrationId(regid))

                        .setNotification(Notification.newBuilder()

                        .addPlatformNotification(IosNotification.newBuilder()

                                .setAlert(content)

                                .setBadge(1)

                                .setSound("happy")

                                .addExtra("id",id)

                                .addExtra("content",content)

                                .addExtra("type",type)

                                .build()


                        )

                        .addPlatformNotification(AndroidNotification.newBuilder()

                                .setAlert(content).build())

                        .build())

                        .setOptions(Options.newBuilder()

                        .setApnsProduction(true)

                        .build())

                .build();

}

/**

* 向客户端按标签发送通知方法

* @param tag  接收人

* @param message 消息内容

* @return

*/

private static  PushPayload PushTag(String tag,String message){

return PushPayload.newBuilder()

                .setPlatform(Platform.android_ios())

                .setAudience(Audience.newBuilder()

                        .addAudienceTarget(AudienceTarget.tag(tag))

                        .build())

                        .setNotification(Notification.newBuilder()

                        .addPlatformNotification(IosNotification.newBuilder()

                                .setAlert(message)

                                .setBadge(5)

                                .setSound("happy")

                                .addExtra("from", "JPush")

                                .build()


                        )

                        .addPlatformNotification(AndroidNotification.newBuilder()

                                .setAlert(message).build())

                        .build())

                        .setOptions(Options.newBuilder()

                        .setApnsProduction(true)

                        .build())

                .build();


}

/**

* 发送广播通知;

* @param content推送内容

*/

public static PushPayload pushAll(String content) {

return PushPayload.alertAll(content);

}

/**

* 推送通知

* @param regid

* @param id

* @param content

* @param type

* @return

*/

public static boolean pushNotifiton(String regid,String id,String content,String type){

boolean flag=false;

JPushClient jpushClient = new JPushClient(MasterSecret, AppKey, 3);

//调取通知推送

PushPayload payload=Notifiton(regid,id,content,type);

try {

jpushClient.sendPush(payload);

flag=true;

} catch (Exception e) {

flag=false;

}

return flag;

}

/**

* 向手机端按标签推送通知

*/

public static boolean pushTag(String tag,String content){

boolean flag=false;

JPushClient jpushClient = new JPushClient(MasterSecret, AppKey, 3);

    PushPayload payload = PushTag(tag,content);

    try {

jpushClient.sendPush(payload);

flag=true;

} catch (Exception e) {

flag=false;

}

return flag;

}

/**

* 向手机端发送广播

*/

public static boolean pushALL(String content){

boolean flag=false;

JPushClient jpushClient = new JPushClient(MasterSecret, AppKey, 3);

    PushPayload payload = pushAll(content);

    try {

    jpushClient.sendPush(payload);

flag=true;

} catch (Exception e) {

flag=false;

}

return flag;

}

/**

* 发送自定义消息

* @param content 消息内容

* @param userid 接收人id

* @param type 消息自定义类型  equipment设备消息  noticemsg 通知消息 

* @throws APIConnectionException

* @throws APIRequestException

*/

public static boolean sendMsg(String userid,String content,String type)  {

boolean flag=false;

JPushClient _client = new JPushClient(MasterSecret, AppKey, 3);

PushPayload payload = PushPayload.newBuilder()

        .setAudience((Audience.newBuilder()

                .addAudienceTarget(AudienceTarget.alias(userid))

                .build()))

        .setPlatform(Platform.android())

        .setMessage(Message.newBuilder()

        .addExtra("type", type)

                .setMsgContent(content).build())

        .build();

try {

PushResult result = _client.sendPush(payload);

flag=true;

} catch (Exception e) {

flag=false;

}

return flag;

}

public static void main(String args[]) {

JSONObject json=new JSONObject();

json.put("id", "645d20805c194259a4b820f6f1c26a36");

String content="123456789";

json.put("content", content);

//ios的regid推送通知==========

//regid 

System.out.println(pushNotifiton("13165ffa4e244ded33c","12","内容","attestMsg"));//按照regid推送

//安卓的自定义消息推送============

//用户id  内容  类型

//sendMsg( "b33582a3ddb94137846fe466a00259f9","1111","noticemsg");//个人按照别名推送

//System.out.println(pushNotifiton("",""));//个人按照别名推送

//System.out.println(pushTag("6","标签测试"));//按照标签推送

//System.out.println(pushALL("广播测试"));//广播

}

}

你可能感兴趣的:(极光推送2)