java后台与android端集成信鸽推送和华为推送通道

时间比较紧,并未调试。

需求:后端可以针对指定账号或者列表进行通知栏信息推送,优先使用华为通道。点击通知栏消息打开指定activity

后端版本V3,android sdk 3.2.7

一.后端部分代码:

自定义XGPushMessage

public class XGPushMessage {
    private String title;
    private String content;
    private Android  android;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Android getAndroid() {
        return android;
    }

    public void setAndroid(Android action) {
        this.android = action;
    }
}

自定义一个Action类

public class Action {

    private int action_type=3;
    private String activity;
    private String intent;

    public int getAction_type() {
        return action_type;
    }

    public void setAction_type(int action_type) {
        this.action_type = action_type;
    }

    public String getActivity() {
        return activity;
    }

    public void setActivity(String activity) {
        this.activity = activity;
    }

    public String getIntent() {
        return intent;
    }

    public void setIntent(String intent) {
        this.intent = intent;
    }
}

自定义一个Android类:

public class Android {
    private Action action;

    public Action getAction() {
        return action;
    }

    public void setAction(Action action) {
        this.action = action;
    }
}

其中Action类是参照信鸽推送官网文档,自己根据实际业务情况自定义的一个bean

"android": {
        "n_id": 0,
        "builder_id": 0,
        "ring": 1,
        "ring_raw": "ring",
        "vibrate": 1,
        "lights": 1,
        "clearable": 1,
        "icon_type": 0,
        "icon_res": "xg",
        "style_id": 1,
        "small_icon": "xg",
        "action": {
            "action_type": 1,// 动作类型,1,打开activity或app本身;2,打开浏览器;3,打开Intent
            "activity": "xxx",
            "aty_attr": {// activity属性,只针对action_type=1的情况
                "if": 0, // Intent的Flag属性
                "pf": 0  // PendingIntent的Flag属性
            },
            "browser": {
                "url": "xxxx ", // 仅支持http、https
                "confirm": 1 // 是否需要用户确认
            },
            "intent": "xxx" //SDK版本需要大于等于3.2.3,然后在客户端的intent配置data标签,并设置scheme属性
        },
        "custom_content": {
            "key1": "value1",
            "key2": "value2"
        }
    }

(幸好我没有用到activity属性,否则真不知道"if"属性在业务类里面该如何声明)

还封装了一个我的后端向信鸽推送提交的消息推送请求的业务Bean:

import java.util.List;

public class PushParams {
    private String audience_type;
    private String platform;
    private String message_type;
    private XGPushMessage message;

    private List<String> account_list;




    public String getAudience_type() {
        return audience_type;
    }

    public void setAudience_type(String audience_type) {
        this.audience_type = audience_type;
    }

    public String getPlatform() {
        return platform;
    }

    public void setPlatform(String platform) {
        this.platform = platform;
    }

    public String getMessage_type() {
        return message_type;
    }

    public void setMessage_type(String message_type) {
        this.message_type = message_type;
    }

    public XGPushMessage getMessage() {
        return message;
    }

    public void setMessage(XGPushMessage message) {
        this.message = message;
    }

    public List<String> getAccount_list() {
        return account_list;
    }

    public void setAccount_list(List<String> account_list) {
        this.account_list = account_list;
    }
}

然后自己又简单的封装HttpClient工具,用于向信鸽提交请求:

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class HttpAsyncClient<T> {

public static <T1, T2> T2 postForObject(String url, T1 request,String authorization,
                                            Class<T2> responseType) throws Exception {

        T2 t = null;
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss")
                .disableHtmlEscaping().create();
        String gStr = doSendData(request);
        HttpClient client = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("Content-type", "application/json; charset=UTF-8");
        httpPost.setHeader("authorization", "Basic "+authorization);
        StringEntity entity = new StringEntity(gStr, "UTF-8");
        httpPost.setEntity(entity);
        HttpResponse response = client.execute(httpPost);
        String result = EntityUtils.toString(response.getEntity(), "UTF-8");
        System.out.println("反回的字符串:" + result);
        t = gson.fromJson(result, responseType);
        return t;
    }

}

注意httpclient的参数,

 String authorization

信鸽官网要求请求头包含一个校验内容。信鸽创建应用后会生成APP_ID和SECRET_KEY,请求头校验位就是这两个字段的Base64编码,见信鸽推送文档--RestApi概述----权鉴方式

 

将HttpClient和校验码生成封装一下并发送请求:

@Component
public class XGApi {
    @Value("${server.authcation}")
    private  String authcation;
    public  XGPushResponse push(PushParams params) throws Exception {
    byte[] bArr = authcation.getBytes("UTF-8");
    return HttpAsyncClient.postForObject(XGUrl.URL_PUSH, params, Base64Utils.encodeToString(bArr), XGPushResponse.class);
    }
}

 

具体提交推送请求的代码为:

        XGPushMessage xgPushMessage=new XGPushMessage();
        xgPushMessage.setTitle("标题");
        xgPushMessage.setContent(“测试消息的内容”);
        Android android = new Android();

        Action action = new Action();
        action.setActivity("com.app.TestActivity");
        action.setIntent("xgscheme://com.xg.push/notify_detail");
        android.setAction(action);
        xgPushMessage.setAndroid(android);

        PushParams pushParam = new PushParams ();
        pushParam.setAudience_type("account");
        pushParam.setAccount_list(account_list);
        pushParam.setMessage(xgPushMessage);
        pushParam.setPlatform("android");
        pushParam.setMessage_type("notify");


        try {
            XGPushResponse xgResponse = xgApi.push(pushParam);
        } catch (Exception e) {
            e.printStackTrace();
        }

后端推送api地址:

https://openapi.xg.qq.com/v3/push/app

后端过程完。

Action中的action_type是3(自定义点击通知后的动作).这部分有点模糊,官网说这个字段1代表打开activity,3代表自定义。我是把它设为3,然后同时传activity和intent两个字段值才能实现跳转。

二.android端集成sdk

按照官网说明来,build.gradle添加了信鸽和信鸽华为通道相关依赖

    compile 'com.tencent.xinge:xinge:3.2.7-Release'

    //jg包
    compile 'com.tencent.jg:jg:1.1'

    //wup包
    compile 'com.tencent.wup:wup:1.0.0.E-release'

    //mid包
    compile 'com.tencent.mid:mid:4.0.6-release'
    /* 华为 3.2.7-release版
 * 注意:若华为通道使用此版本,则信鸽sdk版本也需要同时使用v3.2.7-Release
 */
    compile 'com.tencent.xinge:xghw:3.2.7-release'

然后启动页onCreate中要初始化

        // 出现  otherPushType = huawei otherPushToken = null 则取消下面一行注释
//        XGPushConfig.setHuaweiDebug(true);
        //打开第三方推送(为华为通道配置)
        XGPushConfig.enableOtherPush(getApplicationContext(), true);
        XGPushConfig.enableDebug(this,true);
        XGPushManager.registerPush(this, new XGIOperateCallback() {
            @Override
            public void onSuccess(Object data, int flag) {
                //token在设备卸载重装的时候有可能会变
                Log.d("TPush", "注册成功,设备token为:" + data);
                xgToken=data+"";
                switchMainActivity();
            }
            @Override
            public void onFail(Object data, int errCode, String msg) {
                Log.d("TPush", "注册失败,错误码:" + errCode + ",错误信息:" + msg);
                switchMainActivity();
            }
        });

 

android端完成

 

token时信鸽分配的还是android手机本地生成的至今不清楚。

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值