java服务端集成极光消息推送

极光推送官网地址:https://www.jiguang.cn/

api文档:https://docs.jiguang.cn/jpush/server/push/server_overview/

1.注册登录后进入应用管理创建应用进入应用设置

java服务端集成极光消息推送_第1张图片

服务端需要用到AppKey和Master Secret 其他推送设置由安卓操作啦.....

2.导入jar包


    cn.jpush.api
    jpush-client
    3.3.9

3.测试


import cn.jiguang.common.ClientConfig;
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.Notification;
import org.junit.Test;

public class TestJPush {
    private  String MASTER_SECRET="fed4cd87fc7c2260f9d22d1";
    private  String APP_KEY="cad9b71c32e15083d7c7491";
    private  String ID="105680689801926246";//设备别名
    private  String title="消息通知";
    private  String alert="测试消息通知";
    private ClientConfig config = ClientConfig.getInstance();
    private JPushClient client = new JPushClient(MASTER_SECRET, APP_KEY, null, config);

    /**
     * 面向平台:安卓端
     * 推送目标:单推个人(别名推送)
     */
    @Test
    public void pushAline(){
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.android())//推送平台
                .setAudience(Audience.alias(ID))//推送目标
                .setNotification(Notification.newBuilder().addPlatformNotification(AndroidNotification.newBuilder().
                        setAlert(alert).setTitle(title).build()).build())//通知消息(标题,内容)
                .setOptions(Options.newBuilder().setApnsProduction(true).setTimeToLive(86400).build()//离线消息保存时间
                )
                .build();
        try {
            PushResult result = client.sendPush(payload);//推送
            System.out.println("推送结果:" + result.getResponseCode());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 面向平台:安卓端
     * 推送目标:所有用户
     */
    @Test
    public void pushAll(){
        PushPayload pushPayload= PushPayload.newBuilder()
                .setPlatform(Platform.android())//推送平台
                .setAudience(Audience.all())//推送目标
                .setNotification(Notification.newBuilder().addPlatformNotification(AndroidNotification.newBuilder().
                        setAlert(alert).setTitle(title).build()).build())//通知消息(标题,内容)
                .setOptions(Options.newBuilder().setApnsProduction(true).setTimeToLive(86400).build()//离线消息保存时间
                )
                .build();
        try{
            PushResult result=client.sendPush(pushPayload);//推送
            System.out.println("推送结果:"+result.getResponseCode());
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

主要参数:推送平台Platform,推送目标Audience,通知 Notification  也可以自定义消息Message

4.结果

responseContent:{"msg_id":"604592161","error":{"code":1011,"message":"cannot find user by this audience"}}

>>>>>如果这样的话,那就是安卓端的账号没有登录会推送失败 只要登录就可以解决了 (安卓需要给设备绑定别名)

>>>>>目前没有用户在线

>>>>>没有用户在线

>>>>>所以失败啦

推送结果:200

>>>>>这样就是推送成功啦 


注:

用户ID设置为别名用来标识 刚好很方便

HTTP状态码:https://docs.jiguang.cn/jpush/server/push/http_status_code/

 

 

只是记录自己接入api过程出现的问题?

 

 

你可能感兴趣的:(第三方)