美文网首页
JPush Java SDK 示例代码与简要注释

JPush Java SDK 示例代码与简要注释

作者: jiminy | 来源:发表于2016-05-05 19:39 被阅读3981次

    建议使用JPush 服务端SDK的人都先看下接口文档,了解推送api,具体链接:JPush v3 推送api文档

    本人下载的是JPush Java 3.2.9版本的SDK源码(下载链接:JPush Java SDK 源码),在原PushExample类里面稍微改了下buildPushObject_all_all_alert方法,具体看代码:

    
    public static void main(String[] args) {
    
    testSendPush();
    
    }
    
    public static void testSendPush() {
    
    JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3);
    PushPayload payload = buildPushObject_all_all_alert();
    try {
    
    PushResult result = jpushClient.sendPush(payload);
    
    LOG.info("Got result - " + result);
    
    } catch (APIConnectionException e) {
    
    LOG.error("Connection error. Should retry later. ", e);
    
    } catch (APIRequestException e) {
    
    LOG.error("Error response from JPush server. Should review and fix it. ", e);
    
    LOG.info("HTTP Status: " + e.getStatus());
    
    LOG.info("Error Code: " + e.getErrorCode());
    
    LOG.info("Error Message: " + e.getErrorMessage());
    
    LOG.info("Msg ID: " + e.getMsgId());
    
    }
    
    }
    
    public static PushPayload buildPushObject_all_all_alert() {
    
    LOG.info("----------buildPushObject_all_all_alert");
    IosAlert iosAlert =  IosAlert.newBuilder().setTitleAndBody("title", "alert body").build();//创建一个IosAlert对象,可指定APNs的alert、title等字段
    
    return PushPayload.newBuilder()
    
    .setPlatform(Platform.all())//指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台
    
    .setAudience(Audience.all())//指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id
    
    .setNotification(Notification.newBuilder()//jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发
    
    .addPlatformNotification(AndroidNotification.newBuilder()//指定当前推送的android通知
    
    .setAlert("android notification alert")
    
    .setTitle("android notification title")
    
    .setBuilderId(1)//设置Android 通知样式,需要客户端先定义、设置了该样式才有效
    
    .addExtra("extras key", "extras value")//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
    
    .build())
    
    .addPlatformNotification(IosNotification.newBuilder()//指定当前推送的iOS通知
    
    .setAlert(iosAlert)//传一个IosAlert对象,指定apns title、title、subtitle等
    //.setAlert("ios notification alert")//直接传alert
    
    .incrBadge(1)//此项是指定此推送的badge自动加1。如果想直接传具体值,可把incrBadge换成setBadge。
    
    .setSound("sound.caf")//此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音
    
    .addExtra("iOS 的extras1", "JPush111")//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
    
    .setContentAvailable(true)//此项说明此推送是一个background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
    
    .build())
    
    .addPlatformNotification(WinphoneNotification.newBuilder()
    
    .setAlert("WinPhone notification alert")
    
    .setOpenPage("xxxx.cs")//指定点击打开的页面(类),具体后缀忘记,错了请指正
    
    .addExtra("WinPhone extras key", "WinPhone extras value")
    
    .build())
    
    .build())
    
    .setMessage(Message.newBuilder()//Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的 [通知与自定义消息有什么区别?]了解通知和自定义消息的区别
    
    .setMsgContent("message content")
    
    .setTitle("message titile")
    
    .addExtra("message extras key", "message extras value")
    
    .build())
    
    .setOptions(Options.newBuilder()
    
    .setApnsProduction(false)//此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
    
    .setSendno(1)//此字段是给开发者自己给推送编号,方便推送者分辨推送记录
    
    .setTimeToLive(86400)//此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天;
    
    .build())
    
    .build();
    
    }
    
    

    请求结果如下:

    16/05/05 18:45:58 INFO connection.NativeHttpClient: Created instance with connectionTimeout 5,000, readTimeout 30,000, maxRetryTimes 3, SSL Version TLS
    
    16/05/05 18:45:58 INFO connection.NativeHttpClient: Created instance with connectionTimeout 5,000, readTimeout 30,000, maxRetryTimes 3, SSL Version TLS
    
    16/05/05 18:45:58 INFO connection.NativeHttpClient: Created instance with connectionTimeout 5,000, readTimeout 30,000, maxRetryTimes 3, SSL Version TLS
    
    16/05/05 18:45:58 INFO connection.NativeHttpClient: Created instance with connectionTimeout 5,000, readTimeout 30,000, maxRetryTimes 3, SSL Version TLS
    
    16/05/05 18:45:58 INFO examples.PushExample: ----------buildPushObject_all_all_alert
    
    16/05/05 18:45:58 DEBUG connection.NativeHttpClient: Send request - POST https://api.jpush.cn/v3/push
    
    16/05/05 18:45:58 DEBUG connection.NativeHttpClient: Request Content - {"platform":"all","audience":"all","notification":{"android":{"alert":"android notification alert","extras":{"extras key":"extras value"},"title":"android notification title"},"winphone":{"alert":"WinPhone notification alert","extras":{"WinPhone extras key":"WinPhone extras value"},"_open_page":"xxxx.cs"},"ios":{"alert":"ios notification alert","extras":{"from":"JPush"},"badge":"+1","sound":"sound.caf","content-available":1}},"message":{"title":"message titile","msg_content":"message content","extras":{"message extras key":"message extras value"}},"options":{"sendno":1,"time_to_live":86400,"apns_production":false}}
    
    16/05/05 18:46:04 DEBUG resp.ResponseWrapper: JPush API Rate Limiting params - quota:600, remaining:599, reset:60
    
    16/05/05 18:46:04 DEBUG connection.NativeHttpClient: Succeed to get response OK - responseCode:200
    
    16/05/05 18:46:04 DEBUG connection.NativeHttpClient: Response Content - {"sendno":"1","msg_id":"1588446452"}
    
    16/05/05 18:46:04 INFO examples.PushExample: Got result - {"msg_id":1588446452,"sendno":1}
    

    msg_id是jpush中此推送的唯一标识,如果对某推送有疑问可以向他们提供具体msg_id。

    注:本人并不擅长写文档,此文仅给刚使用jpush 服务端sdk 开发者参考。如果写的不好,请谅解,提出改进建议。

    相关文章

      网友评论

          本文标题:JPush Java SDK 示例代码与简要注释

          本文链接:https://www.haomeiwen.com/subject/cfwdrttx.html