其他分享
首页 > 其他分享> > 极光消息推送JPush

极光消息推送JPush

作者:互联网

 

 

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);
        }
    }
}

 

标签:cn,jpush,极光,JPush,api,push,import,推送,String
来源: https://blog.csdn.net/SURLIYA/article/details/90752840