극광 푸 시 자바 배경 통합 인 스 턴 스

19779 단어 자바
1. 우선 극광 홈 페이지 에서 자바 대응 sdk, 홈 페이지 채널 다운로드:https://docs.jiguang.cn/jpush/resources/#sdk_1
2. 해당 하 는 jar 패 키 지 를 자신의 웹 프로젝트 의 lib 에 복사 하고 추가 합 니 다.
3. 그 다음 에 직접 사용 하면 됩 니 다. 오류 가 발생 할 수 있 지만 해결 하기 어렵 지 않 습 니 다. 아래 의 코드 는 거의 모든 상황 을 푸 시 하 는 것 을 포함 합 니 다.
public class JpushClientUtil {
private final static String masterSecret = "";
private final static String appKey = "";

private static JPushClient jPushClient = new JPushClient(masterSecret,appKey);

/**
 *             
 * @param registrationId     
 * @param notification_title       
 * @param msg_title       
 * @param msg_content     
 * @param extrasparam     
 * @return 0    ,1    
 */
public static int sendToRegistrationId(List registrationList,String notification_title, String msg_title, String msg_content, String extrasparam,String type) {
    int result = 0;
    try {
        PushPayload pushPayload=null;
        if(type.equals("0")){//     
        	pushPayload=JpushClientUtil.buildPushObject_all_registrationId_alertWithTitle(registrationList,notification_title,msg_title,msg_content,extrasparam);
            System.out.println(pushPayload);
        }else if(type.equals("1")){
        	pushPayload=JpushClientUtil.buildPushObject(registrationList,notification_title,msg_title,msg_content,extrasparam);
            System.out.println(pushPayload);
        }		
        PushResult pushResult=jPushClient.sendPush(pushPayload);
        System.out.println(pushResult);
        if(pushResult.getResponseCode()==200){
            result=1;
        }
    } catch (APIConnectionException e) {
        e.printStackTrace();

    } catch (APIRequestException e) {
        e.printStackTrace();
    }

     return result;
}

//iOS  
public static void testSendIosAlert() {
    JPushClient jpushClient = new JPushClient(masterSecret, appKey);
    IosAlert alert = IosAlert.newBuilder()
            .setTitleAndBody("test alert", "subtitle", "test ios alert json")
            .setActionLocKey("PLAY")
            .build();
    try {
        PushResult result = jpushClient.sendIosNotificationWithAlias(alert, new HashMap(), "alias1");
       System.out.println("Got result - " + result);
    } catch (APIConnectionException e) {
    	System.out.println("Connection error. Should retry later. "+e);
    } catch (APIRequestException e) {
    	System.out.println("Error response from JPush server. Should review and fix it. "+e);
        }
    }
/**
 *          
 * @param notification_title       
 * @param msg_title       
 * @param msg_content     
 * @param extrasparam     
 * @return 0    ,1    
 */
public static int sendToAllAndroid( String notification_title, String msg_title, String msg_content, String extrasparam) {
    int result = 0;
    try {
        PushPayload pushPayload= JpushClientUtil.buildPushObject_android_all_alertWithTitle(notification_title,msg_title,msg_content,extrasparam);
        System.out.println(pushPayload);
        PushResult pushResult=jPushClient.sendPush(pushPayload);
        System.out.println(pushResult);
        if(pushResult.getResponseCode()==200){
            result=1;
        }
    } catch (Exception e) {

        e.printStackTrace();
    }

     return result;
}

/**
 *      IOS  
 * @param notification_title       
 * @param msg_title       
 * @param msg_content     
 * @param extrasparam     
 * @return 0    ,1    
 */
public static int sendToAllIos(String notification_title, String msg_title, String msg_content, String extrasparam) {
    int result = 0;
    try {
        PushPayload pushPayload= JpushClientUtil.buildPushObject_ios_all_alertWithTitle(notification_title,msg_title,msg_content,extrasparam);
        System.out.println(pushPayload);
        PushResult pushResult=jPushClient.sendPush(pushPayload);
        System.out.println(pushResult);
        if(pushResult.getResponseCode()==200){
            result=1;
        }
    } catch (Exception e) {

        e.printStackTrace();
    }

     return result;
}

/**
 *        
 * @param notification_title       
 * @param msg_title       
 * @param msg_content     
 * @param extrasparam     
 * @return 0    ,1    
 */
public static int sendToAll( String notification_title, String msg_title, String msg_content, String extrasparam) {
    int result = 0;
    try {
        PushPayload pushPayload= JpushClientUtil.buildPushObject_android_and_ios(notification_title,msg_title,msg_content,extrasparam);
        System.out.println(pushPayload);
        PushResult pushResult=jPushClient.sendPush(pushPayload);
        System.out.println(pushResult);
        if(pushResult.getResponseCode()==200){
            result=1;
        }
    } catch (Exception e) {

        e.printStackTrace();
    }

    return result;
}



public static PushPayload buildPushObject_android_and_ios(String notification_title, String msg_title, String msg_content, String extrasparam) {
    return PushPayload.newBuilder()
            .setPlatform(Platform.android_ios())
            .setAudience(Audience.all())
            .setNotification(Notification.newBuilder()
                    .setAlert(notification_title)
                    .addPlatformNotification(AndroidNotification.newBuilder()
                            .setAlert(notification_title)
                            .setTitle(notification_title)
                            //        ,        。                 ,    key         (value)
                            .addExtra("androidNotification extras key",extrasparam)
                            .build()
                    )
                    .addPlatformNotification(IosNotification.newBuilder()
                            //   IosAlert  ,  apns title、title、subtitle 
                            .setAlert(notification_title)
                            //   alert
                            //         badge   1
                            .incrBadge(1)
                            //     default        ; sound.caf             sound.caf     ,
                            //                    ;          ,iOS9            ,          
                            .setSound("sound.caf")
                            //        ,        。                 ,    key         (value)
                            .addExtra("iosNotification extras key",extrasparam)
                            //          background  ,   background :http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
                            // .setContentAvailable(true)

                            .build()
                    )
                    .build()
            )
            //Platform                            。 jpush      ,
            // sdk        ,       。     http://docs.jpush.io/guideline/faq/ 
            // [             ?]             
            .setMessage(Message.newBuilder()
                    .setMsgContent(msg_content)
                    .setTitle(msg_title)
                    .addExtra("message extras key",extrasparam)
                    .build())

            .setOptions(Options.newBuilder()
                    //                 apns  ,false    ,true    ; android         
                    .setApnsProduction(false)
                    //               ,           
                    .setSendno(1)
                    //                    ,              ,        ,    
                    .setTimeToLive(86400)
                    .build()
            )
            .build();
}

private static PushPayload buildPushObject_all_registrationId_alertWithTitle(List registrationList,String notification_title, String msg_title, String msg_content, String extrasparam) {

    System.out.println("----------buildPushObject_all_all_alert");
    //    IosAlert  ,   APNs alert、title   
    IosAlert alert = IosAlert.newBuilder()
            .setTitleAndBody(msg_title,notification_title,"")
            .setActionLocKey("PLAY")
            .build();
   
    return PushPayload.newBuilder()

            //        ,all              ,    android     
            .setPlatform(Platform.all())
            //         ,all     ,            tag alias                registration id
            .setAudience(Audience.registrationId(registrationList))
            //jpush   ,android  jpush    ,iOS  apns     ,Winphone  mpns  
            .setNotification(Notification.newBuilder()
//                		  //       android  
//                        .addPlatformNotification(AndroidNotification.newBuilder()
//                                .setAlert(notification_title)
//                                .setTitle(msg_title)
//                                //        ,        。                 ,    key         (value)
//                                .addExtra("extrasparam",extrasparam)
//                                .build())
                 //       iOS  
                        .addPlatformNotification(IosNotification.newBuilder()
                                //   IosAlert  ,  apns title、title、subtitle 
                                .setAlert(alert)
                                //   alert
                                //         badge   1
                                .incrBadge(0)
                                //     default        ; sound.caf             sound.caf     ,
                                //                    ;          ,iOS9            ,          
                                .setSound("default")
                                //        ,        。                 ,    key         (value)
                                .addExtra("extrasparam",extrasparam)
                                //          background  ,   background :http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
                                //     ,     ios          
                                // .setContentAvailable(true)
 
                                .build()).build())
 
                //Platform                            。 jpush      ,
                // sdk        ,       。     http://docs.jpush.io/guideline/faq/ 
                // [             ?]             
                .setMessage(Message.newBuilder()
                        .setMsgContent(notification_title)
                        .setTitle(msg_title)
                        .addExtra("extrasparam",extrasparam)
                        .addExtra("userId",msg_content.replaceAll("    ", ""))
                        .build())
 
                .setOptions(Options.newBuilder()
                        //                 apns  ,false    ,true    ; android         
                        .setApnsProduction(false)
                        //               ,           
                        .setSendno(1)
                        //                    ,              ,        ;
                        .setTimeToLive(86400)
 
                        .build())
 
                .build();
 
    }
    
    private static PushPayload buildPushObject(List registrationList,String notification_title, String msg_title, String msg_content, String extrasparam) {
    	 
        System.out.println("----------buildPushObject_all_all_alert");
        //    IosAlert  ,   APNs alert、title   
        IosAlert alert = IosAlert.newBuilder()
                .setTitleAndBody("test alert", "subtitle", "test ios alert json")
                .setActionLocKey("PLAY")
                .build();
        return PushPayload.newBuilder()
                //        ,all              ,    android     
                .setPlatform(Platform.all())
                //         ,all     ,            tag alias                registration id
                .setAudience(Audience.registrationId(registrationList))
                //jpush   ,android  jpush    ,iOS  apns     ,Winphone  mpns  
                .setNotification(Notification.newBuilder()
                        //       android  
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setAlert(notification_title)
                                .setTitle(msg_title)
                                //        ,        。                 ,    key         (value)
                                .addExtra("extrasparam",extrasparam)
                                .build())
                        //       iOS  
                        .addPlatformNotification(IosNotification.newBuilder()
                                //   IosAlert  ,  apns title、title、subtitle 
                                .setAlert(alert)
                                //   alert
                                //         badge   1
                                .incrBadge(1)
                                //     default        ; sound.caf             sound.caf     ,
                                //                    ;          ,iOS9            ,          
                                .setSound("default")
                                //        ,        。                 ,    key         (value)
                                .addExtra("extrasparam",extrasparam)
                                //          background  ,   background :http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
                                //     ,     ios          
                                // .setContentAvailable(true)
 
                                .build())
 
 
                        .build())
                //Platform                            。 jpush      ,
                // sdk        ,       。     http://docs.jpush.io/guideline/faq/ 
                // [             ?]             
                .setMessage(Message.newBuilder()
 
                        .setMsgContent(msg_content)
 
                        .setTitle(msg_title)
 
                        .addExtra("message extras key",extrasparam)
 
                        .build())
 
                .setOptions(Options.newBuilder()
                        //                 apns  ,false    ,true    ; android         
                        .setApnsProduction(false)
                        //               ,           
                        .setSendno(1)
                        //                    ,              ,        ;
                        .setTimeToLive(86400)
                        .build())
// 
                .build();
 
    }
 
    private static PushPayload buildPushObject_android_all_alertWithTitle(String notification_title, String msg_title, String msg_content, String extrasparam) {
        System.out.println("----------buildPushObject_android_registrationId_alertWithTitle");
        return PushPayload.newBuilder()
                //        ,all              ,    android     
                .setPlatform(Platform.android())
                //         ,all     ,            tag alias                registration id
                .setAudience(Audience.all())
                //jpush   ,android  jpush    ,iOS  apns     ,Winphone  mpns  
                .setNotification(Notification.newBuilder()
                        //       android  
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setAlert(notification_title)
                                .setTitle(notification_title)
                                //        ,        。                 ,    key         (value)
                                .addExtra("androidNotification extras key",extrasparam)
                                .build())
                        .build()
                )
                //Platform                            。 jpush      ,
                // sdk        ,       。     http://docs.jpush.io/guideline/faq/ 
                // [             ?]             
                .setMessage(Message.newBuilder()
                        .setMsgContent(msg_content)
                        .setTitle(msg_title)
                        .addExtra("message extras key",extrasparam)
                        .build())
 
                .setOptions(Options.newBuilder()
                        //                 apns  ,false    ,true    ; android         
                        .setApnsProduction(false)
                        //               ,           
                        .setSendno(1)
                        //                    ,              ,        ,    
                        .setTimeToLive(86400)
                        .build())
                .build();
    }
 
    private static PushPayload buildPushObject_ios_all_alertWithTitle( String notification_title, String msg_title, String msg_content, String extrasparam) {
        System.out.println("----------buildPushObject_ios_registrationId_alertWithTitle");
       
        
        IosAlert alert = IosAlert.newBuilder()
               .setTitleAndBody(notification_title, msg_title, msg_content)
               .setActionLocKey("PLAY")
               .build();

       return PushPayload.newBuilder()
                //        ,all              ,    android     
                .setPlatform(Platform.ios())
                //         ,all     ,            tag alias                registration id
                .setAudience(Audience.all())
                //jpush   ,android  jpush    ,iOS  apns     ,Winphone  mpns  
                .setNotification(Notification.newBuilder()
                        //       android  
                        .addPlatformNotification(IosNotification.newBuilder()
                                //   IosAlert  ,  apns title、title、subtitle 
                                .setAlert(alert)
                                //   alert
                                //         badge   1
                                .incrBadge(1)
                                //     default        ; sound.caf             sound.caf     ,
                                //                    ;          ,iOS9            ,          
                                .setSound("sound.caf")
                                //        ,        。                 ,    key         (value)
                                .addExtra("iosNotification extras key",extrasparam)
                                //          background  ,   background :http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
                               // .setContentAvailable(true)
 
                                .build())
                        .build()
                )
                //Platform                            。 jpush      ,
                // sdk        ,       。     http://docs.jpush.io/guideline/faq/ 
                // [             ?]             
                .setMessage(Message.newBuilder()
                        .setMsgContent(msg_content)
                        .setTitle(msg_title)
                        .addExtra("message extras key",extrasparam)
                        .build())
 
                .setOptions(Options.newBuilder()
                        //                 apns  ,false    ,true    ; android         
                        .setApnsProduction(false)
                        //               ,           
                        .setSendno(1)
                        //                    ,              ,        ,    
                        .setTimeToLive(86400)
                        .build())
                .build();
    }
   
}

좋은 웹페이지 즐겨찾기