码迷,mamicode.com
首页 > 编程语言 > 详细

极光推送java代码

时间:2017-11-02 15:49:17      阅读:578      评论:0      收藏:0      [点我收藏+]

标签:ext   sre   port   use   hresult   tap   cms   inf   try   

package com.zheng.cms.web.jpush.util;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.APIConnectionException;
import cn.jpush.api.common.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

public class JPushClientUtil {

    private static Logger logger = LoggerFactory.getLogger(JPushClientUtil.class);
    private static final String appKey = "466f7032ac604e02fb7bda89"; // 必填,例如466f7032ac604e02fb7bda89
    private static final String masterSecret = "13ac09b17715bd117163d8a1";// "13ac09b17715bd117163d8a1";//必填,每个应用都对应一个masterSecret
    protected static JPushClient jpush = null;
    private static final int MAX = Integer.MAX_VALUE;
    private static boolean flag=false;

    public static void main(String[] args) {
        //String msgTitle = "推送测试";
        String msgContent = "看到信息了么,看到就推送成功了!";
        //String userid="7753b9c538de44c791bb44eed1980d39";
        //pushMessage(msgContent);
        //       String[] alias={"143","143"};
                   /* Map<String,String> extras=new HashMap<String,String>();
                    extras.put("type", "3");
                    //pushNotification("title","tet","13611112222");
                    pushAndroidNotification("renz",extras,"3",alias );*/
        Map<String, String> extras = new HashMap<String, String>();
        // 添加附加信息
        extras.put("url", "htt://baidu.com");
        sendAllsetNotification(msgContent,extras);
    }

    private static void init(){
        if(!flag){
            jpush = new JPushClient(masterSecret, appKey);
            flag=true;
        }
    }



    /**
     +     * 推送所有信息
     +     * @param msgContent
     +     */
    public static void pushMessage(String msgContent) {

        init();
        PushResult pushResult;
        try {
            pushResult = jpush.sendMessageAll(msgContent);
            if(pushResult.isResultOK()){
                logger.info("发送成功");
            }
        } catch (APIConnectionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (APIRequestException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    /**
     * 给所有平台的所有用户发通知
     */
    public static void sendAllsetNotification(String message,Map<String, String> extras)
    {

        init();
        PushPayload payload = buildPushObject_all_alias_alert(message, extras);
        try
        {
            PushResult result = jpush.sendPush(payload);
            logger.info(result.toString());
        }
        catch (APIConnectionException e)
        {
            logger.info("推送失败!",e);
        }
        catch (APIRequestException e)
        {
            logger.info("APIRequestException" + e);
            logger.info("Error Message: " + e.getErrorMessage());
            System.out.println("Error Message: " + e.getErrorMessage());
        }
    }


    /**
     * 推送 通知
     */
    public static void pushNotification(String title,String alert,String...alias ) {

        init();
        Map extras= new HashMap();
        PushResult pushResult1;
        PushResult pushResult2;
        try {
            pushResult1 = jpush.sendAndroidNotificationWithAlias(title, alert, extras, alias);
            pushResult2 = jpush.sendIosNotificationWithAlias(alert, extras, alias);
            //jpush.
            if(pushResult1.isResultOK()){
                logger.info("android发送成功");
            }else{
                logger.info("android发送失败");
            }
            if(pushResult2.isResultOK()){
                logger.info("Ios发送成功");
            }
        } catch (APIConnectionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (APIRequestException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 推送ANDROID客户端通知
     */
    public static void pushAndroidNotification(String title,Map<String,String> extras,String alert,String...alias ) {
        init();
        PushResult pushResult1;
        try {
            pushResult1 = jpush.sendAndroidNotificationWithAlias(title, alert, extras, alias);
            if(pushResult1.isResultOK()){
                logger.info("android发送成功");
            }else{
                logger.info("android发送失败");
            }

        } catch (APIConnectionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (APIRequestException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    /**
     * 推送Ios通知
     */
    public static void pushIosNotification(String alert,Map<String,String> extras,String...alias ) {
        init();
        PushResult pushResult2;
        try {
            pushResult2 = jpush.sendIosNotificationWithAlias(alert, extras, alias);

            if(pushResult2.isResultOK()){
                logger.info("Ios发送成功");
            }else{
                logger.info("Ios发送失败");
            }
        } catch (APIConnectionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (APIRequestException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    /**
     * 推送 通知
     */
    public static void pushNotification(String alert,String alias ) {
        init();
        Map extras= new HashMap();
        extras.put("1", "1");
        PushResult pushResult2;
        try {
            pushResult2 = jpush.sendIosNotificationWithAlias(alert, extras, alias);
            if(pushResult2.isResultOK()){
                logger.info("android发送成功");
            }else{
                logger.info("android发送失败");
            }
        } catch (APIConnectionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (APIRequestException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    /**
     * 发送通知
     *
     * @param message
     * @param extras
     * @return
     * @author WangMeng
     * @date 2017年1月13日
     */
    private static PushPayload buildPushObject_all_alias_alert(String message,
                                                               Map<String, String> extras)
    {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                // 设置平台
                .setAudience(Audience.all())
                // 按什么发送 tag alia
                .setNotification(
                        Notification
                                .newBuilder()
                                .setAlert(message)
                                .addPlatformNotification(
                                        AndroidNotification.newBuilder().addExtras(extras).build())
                                .addPlatformNotification(
                                        IosNotification.newBuilder().addExtras(extras).build())
                                .build())
                // 发送消息
                .setOptions(Options.newBuilder().setApnsProduction(false).build()).build();
        //设置ios平台环境  True 表示推送生产环境,False 表示要推送开发环境   默认是开发
    }
}
package com.zheng.cms.web.jpush.util;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.APIConnectionException;
import cn.jpush.api.common.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

public class JPushClientUtil {

    private static Logger logger = LoggerFactory.getLogger(JPushClientUtil.class);
    private static final String appKey = "466f7032ac604e02fb7bda89"; // 必填,例如466f7032ac604e02fb7bda89
    private static final String masterSecret = "13ac09b17715bd117163d8a1";// "13ac09b17715bd117163d8a1";//必填,每个应用都对应一个masterSecret
    protected static JPushClient jpush = null;
    private static final int MAX = Integer.MAX_VALUE;
    private static boolean flag=false;

    public static void main(String[] args) {
        //String msgTitle = "推送测试";
        String msgContent = "看到信息了么,看到就推送成功了!";
        //String userid="7753b9c538de44c791bb44eed1980d39";
        //pushMessage(msgContent);
        //       String[] alias={"143","143"};
                   /* Map<String,String> extras=new HashMap<String,String>();
                    extras.put("type", "3");
                    //pushNotification("title","tet","13611112222");
                    pushAndroidNotification("renz",extras,"3",alias );*/
        Map<String, String> extras = new HashMap<String, String>();
        // 添加附加信息
        extras.put("url", "htt://baidu.com");
        sendAllsetNotification(msgContent,extras);
    }

    private static void init(){
        if(!flag){
            jpush = new JPushClient(masterSecret, appKey);
            flag=true;
        }
    }



    /**
     +     * 推送所有信息
     +     * @param msgContent
     +     */
    public static void pushMessage(String msgContent) {

        init();
        PushResult pushResult;
        try {
            pushResult = jpush.sendMessageAll(msgContent);
            if(pushResult.isResultOK()){
                logger.info("发送成功");
            }
        } catch (APIConnectionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (APIRequestException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    /**
     * 给所有平台的所有用户发通知
     */
    public static void sendAllsetNotification(String message,Map<String, String> extras)
    {

        init();
        PushPayload payload = buildPushObject_all_alias_alert(message, extras);
        try
        {
            PushResult result = jpush.sendPush(payload);
            logger.info(result.toString());
        }
        catch (APIConnectionException e)
        {
            logger.info("推送失败!",e);
        }
        catch (APIRequestException e)
        {
            logger.info("APIRequestException" + e);
            logger.info("Error Message: " + e.getErrorMessage());
            System.out.println("Error Message: " + e.getErrorMessage());
        }
    }


    /**
     * 推送 通知
     */
    public static void pushNotification(String title,String alert,String...alias ) {

        init();
        Map extras= new HashMap();
        PushResult pushResult1;
        PushResult pushResult2;
        try {
            pushResult1 = jpush.sendAndroidNotificationWithAlias(title, alert, extras, alias);
            pushResult2 = jpush.sendIosNotificationWithAlias(alert, extras, alias);
            //jpush.
            if(pushResult1.isResultOK()){
                logger.info("android发送成功");
            }else{
                logger.info("android发送失败");
            }
            if(pushResult2.isResultOK()){
                logger.info("Ios发送成功");
            }
        } catch (APIConnectionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (APIRequestException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 推送ANDROID客户端通知
     */
    public static void pushAndroidNotification(String title,Map<String,String> extras,String alert,String...alias ) {
        init();
        PushResult pushResult1;
        try {
            pushResult1 = jpush.sendAndroidNotificationWithAlias(title, alert, extras, alias);
            if(pushResult1.isResultOK()){
                logger.info("android发送成功");
            }else{
                logger.info("android发送失败");
            }

        } catch (APIConnectionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (APIRequestException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    /**
     * 推送Ios通知
     */
    public static void pushIosNotification(String alert,Map<String,String> extras,String...alias ) {
        init();
        PushResult pushResult2;
        try {
            pushResult2 = jpush.sendIosNotificationWithAlias(alert, extras, alias);

            if(pushResult2.isResultOK()){
                logger.info("Ios发送成功");
            }else{
                logger.info("Ios发送失败");
            }
        } catch (APIConnectionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (APIRequestException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    /**
     * 推送 通知
     */
    public static void pushNotification(String alert,String alias ) {
        init();
        Map extras= new HashMap();
        extras.put("1", "1");
        PushResult pushResult2;
        try {
            pushResult2 = jpush.sendIosNotificationWithAlias(alert, extras, alias);
            if(pushResult2.isResultOK()){
                logger.info("android发送成功");
            }else{
                logger.info("android发送失败");
            }
        } catch (APIConnectionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (APIRequestException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    /**
     * 发送通知
     *
     * @param message
     * @param extras
     * @return
     * @author WangMeng
     * @date 2017年1月13日
     */
    private static PushPayload buildPushObject_all_alias_alert(String message,
                                                               Map<String, String> extras)
    {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                // 设置平台
                .setAudience(Audience.all())
                // 按什么发送 tag alia
                .setNotification(
                        Notification
                                .newBuilder()
                                .setAlert(message)
                                .addPlatformNotification(
                                        AndroidNotification.newBuilder().addExtras(extras).build())
                                .addPlatformNotification(
                                        IosNotification.newBuilder().addExtras(extras).build())
                                .build())
                // 发送消息
                .setOptions(Options.newBuilder().setApnsProduction(false).build()).build();
        //设置ios平台环境  True 表示推送生产环境,False 表示要推送开发环境   默认是开发
    }
}

 

极光推送java代码

标签:ext   sre   port   use   hresult   tap   cms   inf   try   

原文地址:http://www.cnblogs.com/zhaoyan001/p/7771992.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!