消息推送(极光推送)流程

1.service层

String alias = this.userMapper.getUserAlias(userId);//推送识别标识
String content =""//推送内容
this.jpushClientService.senedToAlias(alias,content);

2.JpushClientService


import net.$51zhiyuan.dev.pedicure.module.JpushClientTUtil;
import net.$51zhiyuan.dev.pedicure.module.JpushClientUtil;
import org.springframework.stereotype.Service;

@Service
public class JpushClientService {

    /**
     * 发送应用内的通知
     * @param alias 设置标识
     * @param content 内容
     */
    public void senedToAlias(String alias,String content){
        try {
            JpushClientUtil.senedToAlias(alias,content);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void senedToAliast(String alias,String content){
        try {
            JpushClientTUtil.senedToAlias(alias,content);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

3.JpushClientUtil


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.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;

/**
 *
 */
public class JpushClientUtil {
    private final static String appKey = "***";

    private final static String masterSecret = "***";

    private static JPushClient jPushClient = new JPushClient(masterSecret,appKey);


    public static void senedToAlias(String alias,String content){
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.tag(alias))
                .setMessage(Message.content(content))
                .build();
        try {
            try {
                PushResult result = jPushClient.sendPush(payload);
            } catch (APIConnectionException e) {
                e.printStackTrace();
            }
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }

}

appKey和masterSecret的值,可以在极光推送中找到
这里写图片描述

欢迎大家补充

ps:这里写图片描述

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页