首页 > 其他分享 >app消息推送

app消息推送

时间:2022-12-09 10:57:38浏览次数:33  
标签:style String app 消息 template new pushMessage 推送 message

<dependency>
    <groupId>com.gexin.platform</groupId>
    <artifactId>gexin-rp-sdk-http</artifactId>
    <version>4.1.2.1</version>
</dependency>
<repositories>
    <repository>
        <id>getui-nexus</id>
        <url>http://mvn.gt.getui.com/nexus/content/repositories/releases/</url>
    </repository>
</repositories>
public class AppPush {//以下参数找前端拿
        private static String appId = "xxxxxxxxxxx";
        private static String appKey = "xxxxxxxxxxx";
        private static String masterSecret = "xxxxxxxxxxxx";
        // 如果需要使用HTTPS,直接修改url即可
        //private static String url = "https://api.getui.com/apiex.htm";
        private static String url = "http://api.getui.com/apiex.htm";

    public static void AppPush(PushMessage pushMessage, List<String> appPushList) {
        IGtPush push = new IGtPush(url, appKey, masterSecret);
//
//        Style0 style = new Style0();
//        // STEP2:设置推送标题、推送内容
//        style.setTitle(pushMessage.getTitle());
//        style.setText(pushMessage.getContent());
//        style.setLogo(pushMessage.getLogo());
////        style.setLogo("push.png"); // 设置推送图标
//        // STEP3:设置响铃、震动等推送效果
//        style.setRing(true);  // 设置响铃
//        style.setVibrate(true);  // 设置震动
//
//        // STEP4:选择通知模板
//        NotificationTemplate template = new NotificationTemplate();
//        template.setAppId(appId);
//        template.setAppkey(appKey);
//        template.setStyle(style);
//        // 点击消息打开应用
//        template.setTransmissionType(1);
//        // 传递自定义消息
//        template.setTransmissionContent(JSONUtil.toJsonStr(pushMessage));
        NotificationTemplate template = buildNotificationTemplate(pushMessage);


        // STEP5:定义"AppMessage"类型消息对象,设置推送消息有效期等推送参数
        // 采用toList方案,定义ListMessage消息类型
//        List<String> appIds = new ArrayList<String>();
//        appIds.add(appId);
        ListMessage message = new ListMessage();
        message.setData(template);
//        message.setAppIdList(appIds);
        message.setOffline(true);
        // 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发
        message.setPushNetWorkType(0);
        message.setOfflineExpireTime(1000 * 600);  // 时间单位为毫秒

        String contentId = push.getContentId(message);
        // 获取推送目标
        List targets = new ArrayList();
        for (String ap : appPushList) {
            Target target = new Target();
            target.setAppId(appId);
            target.setClientId(ap);
            targets.add(target);
        }

        // STEP6:执行推送,不采用toApp方案,采用toList方案
//        IPushResult ret = push.pushMessageToApp(message);
        IPushResult ret = push.pushMessageToList(contentId, targets);
        System.out.println(ret.getResponse().toString());

    }


    //通知栏透传模板
    public static NotificationTemplate buildNotificationTemplate(PushMessage pushMessage) {
        NotificationTemplate template = new NotificationTemplate();
        // 设置APPID与APPKEY
        template.setAppId(appId);
        template.setAppkey(appKey);

        Style0 style = new Style0();
        // 设置通知栏标题与内容
        style.setTitle(pushMessage.getTitle());
        style.setText(pushMessage.getContent());
        // 配置通知栏图标
        style.setLogo(pushMessage.getLogo());
        // 配置通知栏网络图标
        style.setLogoUrl("");
        // 设置通知是否响铃,震动,或者可清除
        style.setRing(true);
        style.setVibrate(true);
        style.setClearable(true);
        template.setStyle(style);

        Notify notify = new Notify();
        String intent = "intent:#Intent;action=android.intent.action.com.xiaokun.jzyz;launchFlags=0x14000000;component=/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title={0};S.content={1};S.payload=test;end";
        intent.replace("{0}", pushMessage.getTitle());
        intent.replace("{1}", pushMessage.getContent());
        notify.setTitle(pushMessage.getTitle());
        notify.setContent(pushMessage.getContent());
        notify.setIntent(intent);
        template.set3rdNotifyInfo(notify);

        // 透传消息设置,1为强制启动应用,客户端接收到消息后就会立即启动应用;2为等待应用启动
        template.setTransmissionType(2);
        String o = JSONObject.toJSON(pushMessage).toString();
        System.out.println(o);
        template.setTransmissionContent(o);
        return template;
    }

    public static Map<String, Object> sendMessage(PushMessage pushMessage, List<String> appPushList) {
        try {
            // 使用透传模板
            TransmissionTemplate template = new TransmissionTemplate();
            // 1:收到通知直接激活app,2:客服端自行处理
            template.setTransmissionType(2);
            // 透传内容
            template.setTransmissionContent(pushMessage.getContent());
            template.setAppId(appId);
            template.setAppkey(appKey);
            APNPayload payload = new APNPayload();//设置ios
            payload.setAutoBadge("+1");
            payload.setContentAvailable(0);
            payload.setSound("default");
            payload.setAlertMsg(new APNPayload.SimpleAlertMsg(pushMessage.getTitle()));
            //在已有数字基础上加1显示,设置为-1时,在已有数
            template.setAPNInfo(payload);
            String intent = "intent:#Intent;action=android.intent.action.com.xiaokun.jzyz;launchFlags=0x14000000;component=/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title={0};S.content={1};S.payload=test;end";
            intent.replace("{0}", pushMessage.getTitle());
            intent.replace("{1}", pushMessage.getContent());
            Notify notify = new Notify();
            // 通知栏显示标题
            notify.setTitle(pushMessage.getTitle());
            // 通知栏内容
            notify.setContent(pushMessage.getContent());
            notify.setIntent(intent);
            // 设置第三方通知
            template.set3rdNotifyInfo(notify);

            ListMessage message = new ListMessage();
            message.setData(template);
            // 设置消息离线
            message.setOffline(true);
            // 离线消息有效时间为7天
            message.setOfflineExpireTime(1000 * 3600 * 24 * 7);
            //添加要推送的终端
            List<Target> targets = new ArrayList<Target>();
            for (String ap : appPushList) {
                Target target = new Target();
                target.setAppId(appId);
                target.setClientId(ap);
                targets.add(target);
            }

            //推送前通过该接口申请“ContentID”
            IGtPush push = new IGtPush(url, appKey, masterSecret);
            String contentId = push.getContentId(message);
            IPushResult ret = push.pushMessageToList(contentId, targets);
            System.out.println(ret);
            return ret.getResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }


}
@Data
public class PushMessage {
    private String title="故障通知";
    private String content;
    private String logo="push.png";
    private String payload;
}

 

标签:style,String,app,消息,template,new,pushMessage,推送,message
From: https://www.cnblogs.com/gujiajie/p/16968304.html

相关文章

  • uniapp开发中遇到的问题
    1.uniapp打包小程序包,上传之后显示包大小,超过微信要求的20M大小除掉是因为分包的原因造成的,最主要的是因为,上传的文件是执行包,而不是编译发行的微信包2.用禾匠开发的项目......
  • uni-app 使用vconsole.log
    来自:http://events.jianshu.io/p/06ae7754bfe4 侵删1.下载与安装vconsole1.1选中你的项目,弄出终端,输入以下命令:npminstallvconsolenpminstallvconsolecnp......
  • springboot中数据库批量新增InsertListMapper两种情况
    pom中mybatis的依赖包版本必须高点满足两种情况<dependency><groupId>tk.mybatis</groupId><artifactId>mapper-spring-boot-starter</artifactId><version>4.......
  • Uni App-----之u-input(密码明文小眼睛切换)
    OverridetheentrypointofanimageIntroducedinGitLabandGitLabRunner9.4.Readmoreaboutthe extendedconfigurationoptions.Beforeexplainingtheav......
  • uniapp中的@tap和@click的区别
    在HbuilderX中,两者都是点击时触发事件;不同的是:1.@click是组件被点击时触发,会有约300ms的延迟(内置处理优化了)2.@tap是手指触摸离开时触发,没有300ms的延迟,但是会员事件......
  • CountryMapping
    packagecom.-.it.regioc.bean.enums;publicenumCountryMapping{AD("AD","安道尔"),AE("AE","阿拉伯联合酋长国"),AF("AF","阿富汗"),Z......
  • django框架——简介、基本使用、django app的概念、主要目录结构、“三板斧”
    django框架——简介、基本使用、djangoapp的概念、主要目录结构、“三板斧”一、django简介1.版本问题django1.X:同步 1.11django2.X:同步 2.2......
  • flutter 极光推送
    一、配置极光开发者1.注册极光开发者​​https://www.jiguang.cn/push​​2.创建应用(1) (2)android设置包名   当前台注册好后就可以推送了(3)IOS设置      二、创......
  • 『牛角书』鸿蒙结合SpringBoot实现简易消息通知APP
    鸿蒙结合SpringBoot实现简易消息通知APP本项目为学校鸿蒙课程的课程实践设计,本身有一定的Web开发基础,但是从来没有学过移动APP开发,上了这门课之后感觉处处跟Web开发不同,所以......
  • 对接苹果 https://appleid.apple.com/auth/token
    c# 对接苹果 https://appleid.apple.com/auth/tokenusingMicrosoft.IdentityModel.Tokens;usingMobaFlyx.Utils;usingSystem;usingSystem.Collections.Generic......