推送消息工具类

java

浏览数:5

2019-1-13


推送消息工具类

package com.tinease.tools.jpush;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.APIConnectionException;
import cn.jpush.api.common.APIRequestException;
import cn.jpush.api.push.model.Message;
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 java.util.ArrayList;
import java.util.List;
public class Pusher {
    //static String AppKey = PropUtil.JPUSH_APPKEY;
    //static String masterSecret = PropUtil.JPUSH_MASTERSECRET;
     String AppKey = "e1929d91d23759ffbd1e5c60";
     String masterSecret = "9beb520e2fcf4c43d4f7e6ee";

    /**
     * 向全部用户发送消息
     * 
     * @param msg
     * @return
     */
    public boolean pushMessagetoAll(String msg) {
        JPushClient client = new JPushClient(masterSecret, AppKey, 3);
        try {
            client.sendPush(PushPayload.alertAll(msg));
        } catch (APIConnectionException e) {
            e.printStackTrace();
            return false;
        } catch (APIRequestException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean pushMessagetoAnay(List<String> a, String msg) {
        PushPayload pushpayload = PushPayload.newBuilder().setPlatform(Platform.android_ios())
                .setAudience(Audience.alias((a)))
                .setNotification(Notification.newBuilder().setAlert(msg).build()).build();
        JPushClient client = new JPushClient(masterSecret, AppKey, 3);
        try {
            client.sendPush(pushpayload);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 根据别名向用户推送消息
     * 
     * @param alias
     * @param msg
     * @return
     */
    public boolean pushMessagetoOne(String alias, String msg) {
        PushPayload pushpayload = PushPayload.newBuilder().setPlatform(Platform.android_ios())
                .setAudience(Audience.alias(new String[] { alias }))
                .setNotification(Notification.newBuilder().setAlert(msg).build()).build();
        JPushClient client = new JPushClient(masterSecret, AppKey, 3);
        try {
            client.sendPush(pushpayload);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean pushMessagetoArray(List<String> alias, String msg) {
        PushPayload pushpayload = PushPayload.newBuilder().setPlatform(Platform.android_ios())
                .setAudience(Audience.alias( alias ))
                .setNotification(Notification.newBuilder().setAlert(msg).build()).build();
        JPushClient client = new JPushClient(masterSecret, AppKey, 3);
        try {
            client.sendPush(pushpayload);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    /**
     * 复杂推送
     * 
     * @param alias
     *            别名 一般为用户的ID 用“,”分隔
     * @param title
     * @param msg
     * @param type
     *            附加值
     * @param data
     * @param alert
     * @return
     */
    public boolean push(String alias, String title, String msg, String type, String data, String alert) {
        JPushClient client = new JPushClient(masterSecret, AppKey, 3);
        String[] aliasArray = alias.split(",");
        String [] aliasArray2 = new String[aliasArray.length];
        for(int i=0;i<aliasArray.length;i++)
        {
            aliasArray2[i]=aliasArray[i].trim();
        }
        System.out.println("长度:" + aliasArray.length);
        Message message;
        if (null != type && null != data) {
            message = Message.newBuilder().setMsgContent(msg).setTitle(title).addExtra("type", type)
                    .addExtra("data", data).build();
        }
        else if(null==type&&null != data)
        {
            message = Message.newBuilder().setMsgContent(msg).setTitle(title)
                    .addExtra("data", data).build();
        }
        else if(null ==data&&null!=type)
        {
            message = Message.newBuilder().setMsgContent(msg).setTitle(title)
                    .addExtra("type", type).build();
        }
        else {
            message = Message.newBuilder().setMsgContent(msg).setTitle(title).build();
        }

        if (alias.length() > 0) {
            System.out.println("推送个体");
            PushPayload pushpayload = PushPayload.newBuilder()
                    //生产环节  .setOptions(Options.newBuilder().setApnsProduction(true).build()) 
                    .setPlatform(Platform.android_ios()).setAudience(Audience.alias(aliasArray2))
                    .setMessage(message)
                    .setNotification(
                            Notification.newBuilder()
                                    .addPlatformNotification(IosNotification.newBuilder().setAlert(alert)
                                            .setSound("default.caf").build())
                            .addPlatformNotification(AndroidNotification.newBuilder().setAlert(alert).build()).build())
                    .build();
            try {
                client.sendPush(pushpayload);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
            return true;
        } else {
            System.out.println("推送全部");
            PushPayload pushpayload = PushPayload.newBuilder().setPlatform(Platform.android_ios())
                    .setAudience(Audience.all())
                    .setMessage(message)
                    .setNotification(Notification.newBuilder()
                            .addPlatformNotification(
                                    IosNotification.newBuilder().setAlert(alert).setSound("default.caf").build())
                            .addPlatformNotification(AndroidNotification.newBuilder().setAlert(alert).build()).build())
                    .build();
            try {
                client.sendPush(pushpayload);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }
    }

    public static void main(String[] args){
        Pusher pusher = new Pusher();
        List<String> stringList = new ArrayList<>();
        stringList.add("1");        pusher.pushMessagetoArray(stringList,"我擦");
    }

}

相关推荐