Jiguang push maven e código, sem necessidade de modificar, colar e usar

maven :

<! - 极光 推送 -> 
<dependency> 
   <groupId> cn.jpush.api </groupId> 
   <artifactId> jpush-client </artifactId> 
   <version> 3.3.10 </version> 
</dependency> 
<dependency > 
   <groupId> cn.jpush.api </groupId> 
   <artifactId> jiguang-common </artifactId> 
   <version> 1.1.4 </version> 
</dependency> 
<dependency> 
   <groupId> io.netty </groupId> 
   <artifactId> netty-all </artifactId> 
   <version> 4.1.6.Final </version> 
   <scope> compilar </scope> 
</dependency> 
<dependency> 
   <groupId> com.google.code.gson </groupId> 
   <artifactId> gson </artifactId> 
</dependency> 
<dependency>
   <groupId> org.slf4j </groupId> 
   <artifactId> slf4j-api </artifactId> 
   <version> 1.7.7 </version> 
</dependency> 
<! - 极光 推送 结束 ->

 

Código oficial:

import java.util.HashMap; 
import java.util.Map; 


import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 


import cn.jiguang.common.ClientConfig; 
import cn.jiguang.common.TimeUnit; 
import cn.jiguang.common.resp.APIConnectionException; 
import cn.jiguang.common.resp.APIRequestException; 
import cn.jpush.api.JPushClient; 
import cn.jpush.api.push.PushResult; 
import cn.jpush.api.push.model.Message; 
import cn.jpush.api.push.model.Platform; 
import cn.jpush.api.push.model.PushPayload; 
import cn.jpush.api.push.model.audience.Audience; 
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification; 
import cn.jpush.api.push.model.notification.Notification;
import cn.jpush.api.push.model.notification.WinphoneNotification; 
import cn.jpush.api.report.ReceivedsResult; 
import cn.jpush.api.report.ReceivedsResult.Received; 
import cn.jpush.api.report.UsersResult; 

public class tuisong { 
          logger final estático protegido = LoggerFactory.getLogger (tuisong.class); 
        
          // demonstração do aplicativo definido em resources / jpush-api.conf 
          private static final String APPKEY = "58854e6e9763f66fd3d3d245"; 
          private static final String MASTERSECRET = "f7935b875f2eb06fb904f9d8"; 
          public static JPushClient jpushClient = null; 
         
        
          / ** 
   * 推送 通知 接口 
   * @ 设备 标识
   * @ tag 数组
   * @ 推送 标题
   * @ 推送 类型
   * @ Conteúdo push 
   * / 
          public static void main (String [] args) { 
  sendPushNotice ("160a3797c870b06d4e0", null, "title", "type", "Mingkai push message test", "content"); 
// sendPushNotice ( null, null, "title", "type", "test alert", "content"); 
} 
 
         
          public static void sendPushNotice (String registrationId, String [] tags, String title, String btype, String alert, String content) { 
    jpushClient = new JPushClient (MASTERSECRET, APPKEY); 
    PushPayload payload = null; 
    // Gerar o conteúdo do push, aqui primeiro testamos todos os pushes 
    // Informações do prompt de notificação 
    if (content! = null) { 
      Map <String, String> map = new HashMap <String, String> (); 
      if (registrationId! = Null && tags == null) {
      map.put ("btype", btype);
      // 根据 别名 推送 
    tente {
        carga útil = buldPushObject_all_all_alias (registrationId, título, alerta, conteúdo, mapa); 
      } else if (registrationId == null && tags! = null) {// 根据 tag [] 推送
        payload = buldPushObject_all_all_tag (tags, título, alerta, conteúdo, mapa); 
      } else if (registrationId! = null && tags! = null) {// 别名 和 tags [] 推送 通知
        payload = buldPushObject_all_all_aliasAndTag (registrationId, tags, título, alerta, conteúdo, mapa); 
      } else if (registrationId == null && tags == null) { 
        payload = buldPushObject_all_all (title, alert, content, map); 
      } 
    } else { 
      log.info ("Sem notificação -" + conteúdo); 
    }
      System.out.println (payload.toString ()); 
      Resultado de PushResult = jpushClient.sendPush (carga útil); 
      System.out.println ("推送 结果 :" + result.getResponseCode ()); 
      log.info ("Resultado obtido -" + resultado); 
    } catch (APIConnectionException e) { 
      log.error ("Erro de conexão. Deve tentar novamente mais tarde.", e); 
    } catch (APIRequestException e) { 
      log.error ("Resposta de erro do servidor JPush. Deve revisar e corrigir.", e); 
      log.info ("Status HTTP:" + e.getStatus ()); 
      log.info ("Código de erro:" + e.getErrorCode ()); 
      log.info ("Mensagem de erro:" + e.getErrorMessage ()); 
      log.info ("ID de mensagem:" + e.getMsgId ());
             
       * 推送 自 定义 消息 接口.
       * @param content Push content 
       * / 
              public static void sendPushMessage (String alias, String content) { 
    jpushClient = new JPushClient (MASTERSECRET, APPKEY); 
    PushPayload payload = null; 
    // Para push, tudo que você precisa fazer é construir o objeto PushPayload. 
    // PushPayload payload = buildPushObject_all_all_alert (); 
    // Push para modificar a tag apenas quando for julgado que o alias do usuário e a tag não estão vazios 
    se (content! = Null && alias! 
      = Null ) { payload = PushPayload.newBuilder () 
          . setAudience (Audience.alias (alias)) 
          .setPlatform (Platform.all ()) 
          .setMessage (Message.content (content)). build (); 
    } else {
      log.info ("Sem notificação -" + conteúdo); 
    } 
    tente { 
      System.out.println (payload.toString ()); 
      Resultado de PushResult = jpushClient.sendPush (carga útil); 
      System.out.println (resultado + "................................"); 
      log.info ("Resultado obtido -" + resultado); 
    } catch (APIConnectionException e) { 
      log.error ("Erro de conexão. Deve tentar novamente mais tarde.", e); 
    } catch (APIRequestException e) { 
      log.error ("Resposta de erro do servidor JPush. Deve revisar e corrigir.", e); 
      log.info ("Status HTTP:" + e.getStatus ()); 
      log.info ("Código de erro:" + e.getErrorCode ()); 
      log.info ("
      log.info ("ID de mensagem:" + e.getMsgId ()); 
    } 
            
       * @param mid 
       * / 
              public static void countPush (String mid) { 
    jpushClient = new JPushClient (MASTERSECRET, APPKEY); 
    Carga útil PushPayload = null; 
    tente { 
      ReceivedsResult result = jpushClient.getReportReceiveds (mid); 
      Recebido recebido = result.received_list.get (0); 
      System.out.println ("android_received:" + received.android_received 
                              + "\ nios:" + received.ios_apns_sent); 
      log.debug ("Resultado obtido -" + resultado); 
    } catch (APIConnectionException e) { 
      // Erro de conexão, deve tentar novamente mais tarde 
      log.error ("Erro de conexão, deve tentar novamente mais tarde", e); 
    } catch (APIRequestException e) {
      // Deve revisar o erro e corrigir a solicitação 
      log.error ("Deve revisar o erro e corrigir a solicitação", e); 
      log.info ("Status HTTP:" + e.getStatus ()); 
      log.info ("Código de erro:" + e.getErrorCode ()); 
      log.info ("Mensagem de erro:" + e.getErrorMessage ()); 
    } 
  } 
            
              / ** 
       * 统计 用户 数据。 需要 vip 用户 才能 访问
       * / 
              public static void getReportUser () { 
    jpushClient = new JPushClient (MASTERSECRET, APPKEY); 
    Carga útil PushPayload = null; 
      // Recebido recebido = resultado
    experimentar {
      UsersResult result = jpushClient.getReportUsers (TimeUnit.DAY, "2015-04-28", 8); 
      // System.out.println ("android_received:" + received.android_received + "\ nios:" + received.ios_apns_sent); 
      log.debug ("Resultado obtido -" + resultado); 
    } catch (APIConnectionException e) { 
      // Erro de conexão, deve tentar novamente mais tarde 
      log.error ("Erro de conexão, deve tentar novamente mais tarde", e); 
    } catch (APIRequestException e) { 
      // Deve revisar o erro e corrigir a solicitação 
      log.error ("Deve revisar o erro e corrigir a solicitação", e); 
      log.info ("Status HTTP:" + e.getStatus ()); 
      log.info ("Código de erro:" + e.getErrorCode ());
    } 
  } 
            
       * @return 
       * / 
              public static PushPayload buldPushObject_all_all_alias (String registrationId, String title, String alert, String content, Map <String, String> map) { 
    return PushPayload 
        .newBuilder () 
        .setPlatform (Platform.all ()) 
        .setAudience (Audience.registrationId (registrationId)) 
        .setNotification ( 
                            Notification 
                                .newBuilder () 
                .addPlatformNotification ( 
                                    IosNotification.newBuilder () 
                                        .setAlert (alert)
                        .addExtra ("content", content) 
                        .addExtras (map) .build ()) 
                .addPlatformNotification ( 
                                    AndroidNotification.newBuilder () 
                                        .setAlert (alert) 
                        .addExtra ("content", content) 
                        .setTitle (title) .addExtras (map ) 
                        .build ()) 
                .addPlatformNotification ( 
                                    WinphoneNotification.newBuilder () 
                                        .setAlert (alert) 
                        .addExtra ("conteúdo", conteúdo) 
                        .addExtras (mapa).Construir())
                .build ()). build (); 
  } 
             
              / ** 
       * Push de acordo com a notificação de tag 
       * @ alias 
       * @ alert Push content
       * @return 
       * / 
              public static PushPayload buldPushObject_all_all_tag (tags String [], Título da string, Alerta da string, Conteúdo da string, Map <String, String> map) { 
    return PushPayload 
        .newBuilder () 
        .setPlatform (Platform.all ()) 
        . setAudience (Audience.tag (tags)) 
        .setNotification ( 
                            Notification 
                                .newBuilder () 
                .addPlatformNotification ( 
                                    IosNotification.newBuilder () 
                                        .setAlert (alert)
                        .addExtra ("content", content) 
                        .addExtras (mapa).Construir())
                        .addExtras (map) .build ()) 
                .addPlatformNotification ( 
                                    AndroidNotification.newBuilder () 
                                        .setAlert (alert) 
                        .addExtra ("content", content) 
                        .setTitle (title) .addExtras (map) 
                        .build ()) 
                .addPlatformNotification ( 
                                    WinphoneNotification.newBuilder () 
                                        .setAlert (alert) 
                        .addExtra ("conteúdo", conteúdo) 
                .build ()). Build (); 
  } 
            
              / ** 
       * 根据 tag 通知 推送
       * @ alias 别名
       * @ alert 推送 内容
       * @return 
       * / 
              public static PushPayload buldPushObject_all_all_aliasAndTag (String registrationId, String [] tags, String alert, String title, String content, Map <String, String> map) { 
    return PushPayload 
        .newBuilder ( ) 
        .setPlatform (Platform.all ()) 
        .setAudience (Audience.registrationId (registrationId)) 
        .setAudience (Audience.tag (tags)) 
        .setNotification ( 
                            Notification 
                                .newBuilder () 
                .addPlatformNotification (
                                    IosNotification.newBuilder ()
                                    WinphoneNotification.newBuilder ()
                                        .setAlert (alert) 
                        .addExtra ("content", content) 
                        .addExtras (map) .build ()) 
                .addPlatformNotification ( 
                                    AndroidNotification.newBuilder () 
                                        .setAlert (alert) 
                        .addExtra ("content", content) 
                        .setTitle (título ) .addExtras (map) 
                        .build ()) 
                .addPlatformNotification ( 
                        .addExtra ("content", content) 
                        .addExtras (map) .build ())
                                        .setAlert (alert) 
                .build ()). build (); 
  } 
            
              / ** 
       * 根据 通知 推送
       * @ alias 别名
       * @ alert 推送 内容
       * @return 
       * / 
              public static PushPayload buldPushObject_all_all (String title, String alert, String content, Map <String, String> map) { 
    return PushPayload 
        .newBuilder ( ) 
        .setPlatform (Platform.all ()) 
        .setAudience (Audience.all ()) 
        .setNotification ( 
                            Notification 
                                .newBuilder () 
                .addPlatformNotification ( 
                                    IosNotification.newBuilder () 
                                        .setAlert (alert) 
                        .addExtra ("content", content) 
                        .addExtras (map) .build ()) 
                .addPlatformNotification ( 
                                    AndroidNotification.newBuilder () 
                                        .setAlert (alert) 
                        .addExtra ("content", conteúdo ) 
                        .setTitle (title) .addExtras (map) 
                        .build ()) 
                .addPlatformNotification ( 
                                    WinphoneNotification.newBuilder ()
                                        .setAlert (alert) 
                        .addExtra ("content", content) 
                        .addExtras (map) .build ())
                .build ()). build (); 
  } 
            
}

Acho que você gosta

Origin blog.csdn.net/feng8403000/article/details/97814932
Recomendado
Clasificación