zoukankan      html  css  js  c++  java
  • 极光推送

    极光推送是:使得开发者可以即时地向其应用程序的用户推送通知或者消息,与用户保持互动,从而有效地提高留存率,提升用户体验。简单的说就是通过JPush后台管理网站进行app消息的推送。可以让用户及时的收到最新的消息提示。

           但是往往有时候需要我们自己开发自己的后台管理网站实现推送的功能,这个时候就需要调用JPush提供的API接口,来进行消息的推送。这里我只讲一些核心API接口,客户端的网站上有例子大家可以自己下载下来看看。

    下面是java后台的代码部分:

    复制代码
        public class JPushClientExample {  
            //在极光注册上传应用的 appKey 和 masterSecret  
            private static final String appKey ="a148767f7440ff9daf56457f";////必填,例如466f7032ac604e02fb7bda89  
            private static final String masterSecret = "731e374afd796d5942ba1363";//必填,每个应用都对应一个masterSecret  
            private static JPushClient jpush = null;  
            /* 
             * 保存离线的时长。秒为单位。最多支持10天(864000秒)。 
             * 0 表示该消息不保存离线。即:用户在线马上发出,当前不在线用户将不会收到此消息。 
             * 此参数不设置则表示默认,默认为保存1天的离线消息(86400秒 
             */  
            private static long timeToLive =  60 * 60 * 24;    
            public static void main(String[] args) {  
                /* 
                 * Example1: 初始化,默认发送给android和ios,同时设置离线消息存活时间 
                 * jpush = new JPushClient(masterSecret, appKey, timeToLive); 
                 *  
                 * Example2: 只发送给android         *  
                 * Example3: 只发送给IOS 
                 * jpush = new JPushClient(masterSecret, appKey, DeviceEnum.IOS); 
                 *  
                 * Example4: 只发送给android,同时设置离线消息存活时间 
                 * jpush = new JPushClient(masterSecret, appKey, timeToLive, DeviceEnum.Android); 
                 */  
                jpush = new JPushClient(masterSecret, appKey, timeToLive);  
                /*  
                 * 是否启用ssl安全连接, 可选 
                 * 参数:启用true, 禁用false,默认为非ssl连接 
                 */  
                jpush.setEnableSSL(true);  
          
                //测试发送消息或者通知  
                testSend();  
            }  
            private static void testSend() {  
                // 在实际业务中,建议 sendNo 是一个你自己的业务可以处理的一个自增数字。  
                // 除非需要覆盖,请确保不要重复使用。详情请参考 API 文档相关说明。  
        //      Integer num= getRandomSendNo();  
                String sendNo="1900192560";  
                String msgTitle = "JPush测试信息";  
                String msgContent = "我是JPush测试信息,已经成功发送给你,请查收。";      
                /* 
                 * IOS设备扩展参数, 
                 * 设置badge,设置声音 
                 */  
                Map<String, Object> extra = new HashMap<String, Object>();  
                IOSExtra iosExtra = new IOSExtra(1, "WindowsLogonSound.wav");  
                extra.put("id1",iosExtra);  
                extra.put("id2","I am extra infomation");  
                //IOS和安卓一起  
                MessageResult msgResult = jpush.sendNotificationWithAppKey(sendNo, msgTitle, msgContent, 0, extra);  
                //对所有用户发送通知, 更多方法请参考文档  
            //  MessageResult msgResult = jpush.sendCustomMessageWithAppKey(sendNo,msgTitle, msgContent);  
                if (null != msgResult) {  
                    System.out.println("服务器返回数据: " + msgResult.toString());  
                    if (msgResult.getErrcode() == ErrorCodeEnum.NOERROR.value()) {  
                        System.out.println("发送成功, sendNo=" + msgResult.getSendno());  
                    } else {  
                        System.out.println("发送失败, 错误代码=" + msgResult.getErrcode() + ", 错误消息=" + msgResult.getErrmsg());  
                    }  
                } else {  
                    System.out.println("无法获取数据");  
                }     
            }  

    public static final int MAX = Integer.MAX_VALUE; public static final int MIN = (int) MAX/2; /** * 保持 sendNo 的唯一性是有必要的 * It is very important to keep sendNo unique. * @return sendNo */ public static int getRandomSendNo() { return (int) (MIN + Math.random() * (MAX - MIN)); } }
    复制代码
    复制代码
        //发送自定义消息
        private static PushPayload push_Android(String message) {  
            Map<String,String> map = new HashMap<>();
            map.put("测试", "测试1");
            Builder msg = Message.newBuilder();
            msg.setTitle("衡云title");
            msg.setMsgContent("衡云content");
            msg.setContentType("type:1");
            msg.addExtra("type", 1);
    //        Message.newBuilder();
            return PushPayload.newBuilder()  
                    .setPlatform(Platform.android())  
                    .setAudience(Audience.tag("200000249hykj"))  
                    .setMessage(msg.build())
                    .build();  
        }
    复制代码
  • 相关阅读:
    React生命周期, 兄弟组件之间通信
    React组件式编程Demo-用户的增删改查
    React之this.refs, 实现数据双向绑定
    CCF CSP 201812-4 数据中心
    CCF CSP 201812-4 数据中心
    PAT 顶级 1020 Delete At Most Two Characters (35 分)
    PAT 顶级 1020 Delete At Most Two Characters (35 分)
    Codeforces 1245C Constanze's Machine
    Codeforces 1245C Constanze's Machine
    CCF CSP 201712-4 行车路线
  • 原文地址:https://www.cnblogs.com/haoxiu1004/p/9660975.html
Copyright © 2011-2022 走看看