极光推送java代码

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 表示要推送开发环境   默认是开发
    }
}
时间: 2024-10-07 12:47:42

极光推送java代码的相关文章

JPush极光推送 Java调用服务器端API开发

   极光推送是:使得开发者可以即时地向其应用程序的用户推送通知或者消息,与用户保持互动,从而有效地提高留存率,提升用户体验.简单的说就是通过JPush后台管理网站进行app消息的推送.可以让用户及时的收到最新的消息提示. 但是往往有时候需要我们自己开发自己的后台管理网站实现推送的功能,这个时候就需要调用JPush提供的API接口,来进行消息的推送.这里我只讲一些核心API接口,客户端的网站上有例子大家可以自己下载下来看看. 下面是java后台的代码部分: public class JPushC

极光推送-java消息推送app

1.极光开发者文档:https://docs.jiguang.cn//jpush/client/Android/android_sdk/ 2.简介:JPush是手机端消息推送的免费第三方云平台.手机客户端侧,App 需要集成 JPush SDK,JPush SDK 创建到 JPush Cloud 的长连接,为 App 提供永远在线的能力.服务器端部分,开发者调用 JPush REST API 来进行推送 3.java后台服务器实现极光推送的两种实现方式:https://www.cnblogs.c

总结:极光推送java服务端(1)

遇到的问题: 1.怎么用极光推送 2.极光推送发送失败报错 返回{ } 3.透传和推送区别以及怎么设置 我的解决方案: 问题1.极光推送类里面有不同的方法,需要发给那些人就调用相应的方法.有安卓.ios.发给所有人绑定过此账户极光服务器的用户的方法.也可以使用别名.标签.注册的id. 问题2.服务端appkey和masterSecret要和客户端保持一致,可以使用别名.标签.注册id集合批量发. 问题3.推送有跳出提示弹框.声音等等,透传没有需要客户端编写代码接收.方法中有四个参数,前两个发推送

JPush极光推送Java服务器端API

// 对android和ios设备发送 JPushClient jpush = new JPushClient(masterSecret, appKey);   // 对android和ios设备发送,同时指定离线消息保存时间 JPushClient jpush = new JPushClient(masterSecret, appKey, timeToLive); // 指定某种设备发送 JPushClient jpush = new JPushClient(masterSecret, app

极光推送的代码实现

一:去官网下载SDK或者直接在maven项目中进行相应配置 <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springfram

JPush极光推送Java服务器端实例

import cn.jpush.api.JPushClient; import cn.jpush.api.common.resp.APIConnectionException; import cn.jpush.api.common.resp.APIRequestException; import cn.jpush.api.push.PushResult; import cn.jpush.api.push.model.Message; import cn.jpush.api.push.model.

邮件推送 Java代码

package mail; /** * @Description:邮件信息类 * * @ClassName: SimpleMail */ public class Mail { /** * 主题 */ private String subject; /** * 内容 */ private String content; /** * @return the subject */ public String getSubject() { return subject; } /** * @param

用JPUSH极光推送实现服务端向安装了APP应用的手机推送消息(C#服务端接口)

这次公司要我们做一个功能,就是当用户成功注册以后,他登录以后要收到消息,当然这个消息是安装了我们的手机APP应用的手机咯. 极光推送的网站的网址是:https://www.jpush.cn/ 极光推送的官方API以及帮助文档都在这里:http://docs.jpush.cn/display/dev/Index 其中服务端的接口以及示例代码都在这里:http://docs.jpush.cn/display/dev/Server-SDKs 大家有兴趣的可以看看,因为这次我做的不是客户端APP,所以一

在ionic/cordova中使用极光推送插件(jpush)

Stpe1:创建一个项目(此处使用的是tab类型的项目,创建方式可参照我前一篇如何离线创建Ionic1项目) Stpe2:修改项目信息 打开[config.xml]修改下图内容: Stpe3:创建极光项目 注册极光开发者账号,并新建项目并获取AppKey如下图: Stpe4:安装极光推送cordova插件(jpush-phonegap-plugin) 检查你的项目plugin目录中是否有[cordova-plugin-device]目录.若存在,建议先卸载他.卸载命令:ionic plugin