后台app个推

来源:互联网 发布:网络通信加密软件 编辑:程序博客网 时间:2024/04/29 16:45

做项目会用到把消息推送给app

public class AppMessagePush {    private static String appId = MyProperties.getByKey("appId");;    private static String appKey = MyProperties.getByKey("appKey");;    private static String masterSecret = MyProperties.getByKey("masterSecret");;    public static void sendMessage(PushMsg msg) {        if (msg.getType().equals("android")) {            ITemplate notificationTemplate = notificationTemplateDemo(                    msg.getTitle(), msg.getMessageInfo(), msg.getBadge());            pushSingleMessage(msg.getCid(), notificationTemplate, false);            ITemplate iosTransmissionTemplate = iosTransmissionTemplate(                    msg.getTitle(), msg.getMessageInfo(), msg.getBadge());// 带APNPayload            pushSingleMessage(msg.getCid(), iosTransmissionTemplate, false);        } else if (msg.getType().equals("ios")) {            // ios透传通知 安卓透传            ITemplate iosTransmissionTemplate = iosTransmissionTemplate(                    msg.getTitle(), msg.getMessageInfo(), msg.getBadge());// 带APNPayload            pushSingleMessage(msg.getCid(), iosTransmissionTemplate, true);        }    }    // 单个推送    private static void pushSingleMessage(String Cid, ITemplate template,            boolean offline) {        IGtPush push = new IGtPush(appKey, masterSecret, true);        SingleMessage message = new SingleMessage();        message.setOffline(offline);        // 离线有效时间,单位为毫秒,可选        message.setOfflineExpireTime(24 * 3600 * 1000);        message.setData(template);        // 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发        message.setPushNetWorkType(0);        Target target = new Target();        target.setAppId(appId);        target.setClientId(Cid);        IPushResult ret = null;        try {            ret = push.pushMessageToSingle(message, target);        } catch (RequestException e) {            e.printStackTrace();            ret = push.pushMessageToSingle(message, target, e.getRequestId());        }    }    private static NotificationTemplate notificationTemplateDemo(String title,            String messageInfo, String badge) {        NotificationTemplate template = new NotificationTemplate();        // 设置APPID与APPKEY        template.setAppId(appId);        template.setAppkey(appKey);        // 设置通知栏标题与内容        template.setTitle(title);        template.setText(messageInfo);        // 配置通知栏图标        template.setLogo("icon.png");        // 配置通知栏网络图标        template.setLogoUrl("");        // 设置通知是否响铃,震动,或者可清除        template.setIsRing(true);        template.setIsVibrate(true);        template.setIsClearable(true);        template.setAPNInfo(getApnPayload(title, messageInfo, badge));        template.setTransmissionType(2);        template.setTransmissionContent("notification." + messageInfo);        return template;    }    // ios透传,设置APNPayload参数    private static TransmissionTemplate iosTransmissionTemplate(String title,            String messageInfo, String badge) {        TransmissionTemplate template = transmissionTemplate(messageInfo);        APNPayload payload = getApnPayload(title, messageInfo, badge);        // 字典模式使用下者        // payload.setAlertMsg(getDictionaryAlertMsg());        template.setAPNInfo(payload);        return template;    }    private static APNPayload getApnPayload(String title, String messageInfo,            String badge) {        APNPayload payload = new APNPayload();        // +1在已有数字基础上加1显示,设置为-1时,在已有数字上减1显示,设置为数字时,显示指定数字        payload.setAutoBadge(badge);        payload.setContentAvailable(1);        payload.setSound("123.wav");        payload.setCategory("$由客户端定义");        // //简单模式APNPayload.SimpleMsg        // payload.setAlertMsg(new APNPayload.SimpleAlertMsg(messageInfo));        DictionaryAlertMsg dictionaryAlertMsg = new DictionaryAlertMsg();        dictionaryAlertMsg.setTitle(title);        dictionaryAlertMsg.setBody(messageInfo);        payload.setAlertMsg(dictionaryAlertMsg);        return payload;    }    // 透传消息    private static TransmissionTemplate transmissionTemplate(String messageInfo) {        TransmissionTemplate template = new TransmissionTemplate();        template.setAppId(appId);        template.setAppkey(appKey);        template.setTransmissionContent(messageInfo);        template.setTransmissionType(2);        return template;    }}
package com.park.util.push;public class PushMsg {    private String cid;    private String type;    private String title;    private String messageInfo;    private String badge;    public String getCid() {        return cid;    }    public void setCid(String cid) {        this.cid = cid;    }    public String getType() {        return type;    }    public void setType(String type) {        this.type = type;    }    public String getTitle() {        return title;    }    public void setTitle(String title) {        this.title = title;    }    public String getMessageInfo() {        return messageInfo;    }    public void setMessageInfo(String messageInfo) {        this.messageInfo = messageInfo;    }    public String getBadge() {        return badge;    }    public void setBadge(String badge) {        this.badge = badge;    }    public static PushMsg initial(String cid, String type, String title,            String messageInfo,String badge) {        PushMsg msg = new PushMsg();        msg.setCid(cid);        msg.setType(type);        msg.setTitle(title);        msg.setMessageInfo(messageInfo);        msg.setBadge(badge);        return msg;    }}

在项目上调用就好了

                PushMsg msg = PushMsg.initial(clientId, "ios", msgTitle, msgContent, badge + "");                AppMessagePush.sendMessage(msg);
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 腱鞘炎做小针刀手指伸不直怎么办 胳膊无故筋疼怎么办 胳膊肘聚筋了怎么办 手臂筋扭伤肿痛怎么办 胳膊经常扭到怎么办 小孩胳膊扭到了怎么办 打篮球胳膊扭了怎么办 大腿根有淋巴结怎么办 脸上长了淋巴结怎么办 拔牙后淋巴肿痛怎么办 脖子上生淋巴结怎么办 脖子两侧淋巴结肿大怎么办 脖子左侧锁骨痛怎么办 整牙后中线歪了怎么办 右侧卵巢旁囊肿怎么办 颈下淋巴结肿大怎么办 耳朵后面淋巴结肿大怎么办 颈侧淋巴结肿大怎么办 锁骨窝淋巴肿大怎么办 晚上颈肌痉挛怎么办 劲部淋巴结肿大怎么办 胸锁乳头肌增厚怎么办 幼儿脖子两边歪怎么办 小孩脖子偏一边怎么办 小孩头有点歪怎么办 宝宝脖子有点歪怎么办 心情郁闷胸口疼怎么办 宝宝咳嗽胸口疼怎么办 小孩脖子歪了怎么办 脖子有点向右偏怎么办 感觉乳房有硬块怎么办 宝宝有点歪头怎么办 一岁半宝宝头歪怎么办 宝宝头偏向左边怎么办 长大了头睡偏了怎么办 一岁半宝宝头偏怎么办 小宝宝头有点歪怎么办 婴儿头向左歪怎么办 孩子突然脖子歪怎么办 宝宝睡觉歪脖子怎么办 孩子老歪脖子怎么办