极光消息推送JPush

Deadly 提交于 2019-11-29 23:28:47

 

 

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;

import javax.annotation.PostConstruct;
import java.util.Map;

@Component
public class JPushUtils {

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

    private static JPushClient jpushClient;

    @Autowired
    private Environment environment;

    @PostConstruct
    public void init() {
        String appKey = environment.getProperty("config.jpush.appkey");
        String masterSecret = environment.getProperty("config.jpush.mastersecret");
        if (StringUtils.hasText(appKey) && StringUtils.hasText(masterSecret)) {
            jpushClient = new JPushClient(masterSecret, appKey);
        } else {
            throw new RuntimeException("appkey或masterSecret不可为空");
        }
    }

    public static PushResult push(String title, String alert, Map<String, String> extras, String... alias) {
        return push(title, alert, extras, true, Audience.alias(alias));
    }

    /**
     * @param title     标题
     * @param alert     消息体
     * @param extras    附加参数
     * @param audiences 推送用户
     */
    public static PushResult push(String title, String alert, Map<String, String> extras, boolean ignoreException, Audience... audiences) {
        PushPayload.Builder payloadBuilder = PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setNotification(Notification.newBuilder()
                        .setAlert(alert) //消息体
                        .addPlatformNotification(AndroidNotification.newBuilder() //发送android
                                .setTitle(title)
                                .addExtras(extras) //附加参数
                                .build())
//                        .addPlatformNotification(IosNotification.newBuilder() //发送ios
//                                .setBadge(+1)
//                                .setSound("happy") //ios提示音
//                                .addExtras(extras) //附加参数
//                                .build())
                        .build())
                .setOptions(Options.newBuilder().setApnsProduction(false).build());//指定iOS开发环境 true为生产模式, false为测试模式

        for (Audience audience : audiences) {
            // 一个推送失败, 整个推送流程都会失败
            payloadBuilder.setAudience(audience);
        }
        try {
            return jpushClient.sendPush(payloadBuilder.build());
        } catch (Exception e) {
            if (ignoreException) {
                //可能用户未登录,忽略失败
                LOG.error("消息推送失败", e);
                return null;
            }
            throw new RuntimeException("消息推送失败", e);
        }
    }
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!