极光推送工具类

工作中经常会遇到服务器向App推送消息的需求,一般企业中选择用极光推送的比较多,在集成极光时发现极光的文档并不完整,网上的文章也很多不能直接使用,这里列出我在工作中集成极光的全部代码,只需要按照如下代码保证一次性实现。

1.pom.xml
<!-- 极光推送 begin -->
<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>
<!-- 极光推送 end -->
2.application.yml
jpush:
  appKey: xxx
  masterSecret: xxxx
  apnsProduction: false   # 是否生成环境,true表示生成环境
  
3.MyJPushClient
package com.sinosoft.sss.cloud.notification.utils;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.ServiceHelper;
import cn.jiguang.common.connection.NettyHttpClient;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jiguang.common.resp.ResponseWrapper;
import cn.jpush.api.JPushClient;
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.IosAlert;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import cn.jpush.api.schedule.ScheduleResult;
import com.sinosoft.sss.cloud.notification.config.JpushConfig;
import com.sinosoft.sss.commons.transfer.SingleDataResult;
import io.netty.handler.codec.http.HttpMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;

@Component
public class JpushUtils {

    @Value("${jpush.appKey}")
    private  String appKey;
    @Value("${jpush.masterSecret}")
    private  String masterSecret;

    @Value("${jpush.appKeyIOS}")
    private  String appKeyIOS;
    @Value("${jpush.masterSecretIOS}")
    private  String masterSecretIOS;

    private static JPushClient jPushClient = null;

    private static JPushClient jPushClientIOS = null;

    public JPushClient getJPushClient() {
        if (jPushClient == null) {
            jPushClient = new JPushClient(masterSecret, appKey);
        }

        return jPushClient;
    }
    public JPushClient getJPushClientIOS() {
        if (jPushClientIOS == null) {
            jPushClientIOS = new JPushClient(masterSecretIOS, appKeyIOS);
        }
        return jPushClientIOS;
    }


    private static final Logger logger = LoggerFactory.getLogger(JpushUtils.class);

    @Resource
    private JpushConfig jpushConfig;

    /**
     * 推送给所有的用户(定时)
     *
     * @param title    推送标题
     * @param text     推送内容
     * @param sendTime 推送时间
     * @param extras   传输内容
     * @param alias    标签
     * @return boolean
     */
    public SingleDataResult sendToAllTiming(String title, String text, String sendTime, Map<String, String> extras, String[] alias) {
        PushPayload pushPayload = buildPushObject_All(title, text, null);
        return this.sendPushTiming(pushPayload, sendTime);
    }



    /**
     * 推送给所有的用户(立即)
     *
     * @param title    推送标题
     * @param text     推送内容
     * @param extras   传输内容
     * @return boolean
     */
    public SingleDataResult sendToAll(String title, String text, Map<String, String> extras) {
        PushPayload pushPayload = buildPushObject_All(title, text, null);
        return this.sendPush(pushPayload);
    }

    /**
     * 发送给所有安卓用户
     *
     * @param title     通知标题
     * @param content   通知内容
     * @param extrasMap 自定义通知信息(不在app上显示,作为内容传输的数据)
     * @param alias     标签组
     * @return boolean
     */
    public SingleDataResult sendToAllAndroid(String title, String content, Map<String, String> extrasMap, String... alias) {
        PushPayload pushPayload = buildPushObject_android_all_alertWithTitle(title, content, extrasMap, alias);
        return this.sendPush(pushPayload);
    }

    /**
     * 发送给所有IOS用户
     *
     * @param title     通知标题
     * @param content   通知内容
     * @param extrasMap 自定义通知信息(不在app上显示,作为内容传输的数据)
     * @param alias     标签组
     * @return boolean
     */
    public SingleDataResult sendToAllIOS(String title, String content, Map<String, String> extrasMap, String... alias) {
        PushPayload pushPayload = buildPushObject_ios_all_alertWithTitle(title, content, extrasMap, alias);
        return this.sendPush(pushPayload);
    }

    /**
     * 发送给指定的用户推送
     */
    public SingleDataResult sendToRegistrationIds(String content, List<String> registrationIds) {
        PushPayload pushPayload = buildPushObject_registrationIds_alertWithTitle(content, registrationIds);
        return this.sendPush(pushPayload);
    }
    /**
     * 发送给指定的用户推送(立即)
     */
    public SingleDataResult sendToRegistrationIds(String title,String content, List<String> registrationIds) {
        PushPayload pushPayload = buildPushObject_registrationIds_alertWithTitle(title,content, registrationIds);
        return this.sendPush(pushPayload);
    }
    /**
     * 发送给指定的用户推送(定时)
     */
    public SingleDataResult sendToRegistrationIdsTiming(String title,String content,String sendTime,List<String> registrationIds) {
        PushPayload pushPayload = buildPushObject_registrationIds_alertWithTitle(title,content, registrationIds);
        return this.sendPushTiming(pushPayload,sendTime);
    }



    /**
     * 异步请求推送方式
     * 使用NettyHttpClient,异步接口发送请求,通过回调函数可以获取推送成功与否情况
     *
     * @param title     通知栏标题
     * @param content   通知栏内容(为了单行显示全,尽量保持在22个汉字以下)
     * @param extrasMap 额外推送信息(不会显示在通知栏,传递数据用)
     * @param alias     需接收的用户别名数组(为空则所有用户都推送)
     */
    public void sendPushWithCallback(String title, String content, Map<String, String> extrasMap, String... alias) {
        ClientConfig clientConfig = ClientConfig.getInstance();
        clientConfig.setTimeToLive(Long.valueOf(jpushConfig.getLiveTime()));
        String host = (String) clientConfig.get(ClientConfig.PUSH_HOST_NAME);
        NettyHttpClient client = new NettyHttpClient(
                ServiceHelper.getBasicAuthorization(jpushConfig.getAppkey(), jpushConfig.getMasterSecret()), null,
                clientConfig);
        try {
            URI uri = new URI(host + clientConfig.get(ClientConfig.PUSH_PATH));
            PushPayload payload = buildPushObject_Android_And_Ios(title, content, extrasMap, alias);
            client.sendRequest(HttpMethod.POST, payload.toString(), uri, new NettyHttpClient.BaseCallback() {
                @Override
                public void onSucceed(ResponseWrapper responseWrapper) {
                    if (200 == responseWrapper.responseCode) {
                        logger.info("极光推送成功");
                    } else {
                        logger.info("极光推送失败,返回结果: " + responseWrapper.responseContent);
                    }
                }
            });
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } finally {
            // 需要手动关闭Netty请求进程,否则会一直保留
            client.close();
        }

    }

    /**
     * 设置、更新、设备的 tag, alias 信息。
     *
     * @param registrationId 设备的registrationId
     * @param alias          更新设备的别名属性
     * @param tagsToAdd      添加设备的tag属性
     * @param tagsToRemove   移除设备的tag属性
     */
    public void UpdateDeviceTagAlias(String registrationId, String alias, Set<String> tagsToAdd, Set<String> tagsToRemove) throws APIConnectionException, APIRequestException {
        JPushClient jpushClient = new JPushClient(jpushConfig.getMasterSecret(), jpushConfig.getAppkey());
        jpushClient.updateDeviceTagAlias(registrationId, alias, tagsToAdd, tagsToRemove);
    }


    /**
     * 根据标签推送相应的消息
     *
     * @param title   推送消息标题
     * @param content 推送消息内容
     * @param map     推送额外信息
     * @param tags    推送的目标标签
     * @return PushPayload
     */
    public PushPayload buildPushLoadByTags(String title, String content, Map<String, String> map, String... tags) {
        if (map.isEmpty()) {
            map = new HashMap<>();
        }
        //批量删除数组中的空元素
        String[] newTags = removeArrayEmptyElement(tags);
        return PushPayload.newBuilder()
                //设置推送平台为安卓
                .setPlatform(Platform.android())
                //设置标签
                .setAudience(Audience.tag(tags))
                //设置 推送的标签标题
                // 设置通知方式(以alert方式提醒)
                .setNotification(Notification.newBuilder()
                        .setAlert(content)
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setTitle(title)
                                .addExtras(map)
                                .build())
                        .build())
                //sendno int 可选 推送序号 纯粹用来作为 API 调用标识
                //离线消息保留时长 推送当前用户不在线时,为该用户保留多长时间的离线消息(默认 86400 (1 天),最长 10 天。设置为 0 表示不保留离线消息,只有推送当前在线的用户可以收到)
                //apns_production boolean 可选 APNs是否生产环境  True 表示推送生产环境,False 表示要推送开发环境; 如果不指定则为推送生产环境
                //big_push_duration int 可选 定速推送时长(分钟) 又名缓慢推送,把原本尽可能快的推送速度,降低下来,在给定的 n 分钟内,均匀地向这次推送的目标用户推送。最大值为 1440。未设置则不是定速推送
//            .setOptions(Options.newBuilder().setApnsProduction(false).setTimeToLive(8600).setBigPushDuration(1).build())
                //设置通知内容
//            .setMessage(Message.newBuilder().setTitle("").setMsgContent("").setContentType("").build())
                .build();
    }

    /**
     * 删除别名中的空元素(需删除如:null,""," ")
     *
     * @param strArray strArray
     * @return String[]
     */
    private String[] removeArrayEmptyElement(String... strArray) {
        if (null == strArray || strArray.length == 0) {
            return null;
        }
        List<String> tempList = Arrays.asList(strArray);
        List<String> strList = new ArrayList<String>();
        Iterator<String> iterator = tempList.iterator();
        while (iterator.hasNext()) {
            String str = iterator.next();
            // 消除空格后再做比较
            if (null != str && !"".equals(str.trim())) {
                strList.add(str);
            }
        }
        // 若仅输入"",则会将数组长度置为0
        String[] newStrArray = strList.toArray(new String[strList.size()]);
        return newStrArray;
    }


    /**
     * 推送方法
     *
     * @param pushPayload 推送内容
     * @return 是否推送成功
     */
    private SingleDataResult sendPush(PushPayload pushPayload) {
        SingleDataResult singleDataResult = new SingleDataResult();
        boolean flag = false;
        logger.info("pushPayload={}", pushPayload);
        PushResult pushResult = null;
        PushResult pushResultIOS = null;
        try {
            pushResult = this.getJPushClient().sendPush(pushPayload);
            pushResultIOS = this.getJPushClientIOS().sendPush(pushPayload);
            if (pushResult.getResponseCode() == 200 || pushResultIOS.getResponseCode() == 200) {
                flag = true;
            }
            logger.info("[极光推送]scheduleResult result is " + pushResult);
            logger.info("[极光推送]scheduleResult result is " + pushResultIOS);
        } catch (APIConnectionException e) {
            logger.error("[极光推送]Connection error. Should retry later. ", e);
        } catch (APIRequestException e) {
            logger.error("[极光推送]Error response from JPush server. Should review and fix it. ", e);
            logger.info("[极光推送]HTTP Status: " + e.getStatus());
            logger.info("[极光推送]Error Code: " + e.getErrorCode());
            logger.info("[极光推送]Error Message: " + e.getErrorMessage());
            singleDataResult.setMessage(e.getErrorMessage());
        }
        singleDataResult.setSuccess(flag);
        return singleDataResult;
    }

    /**
     * 推送方法
     *
     * @param pushPayload 推送内容(定时)
     * @return 是否推送成功
     */
    private SingleDataResult sendPushTiming(PushPayload pushPayload, String sendTime) {
        SingleDataResult singleDataResult = new SingleDataResult();
        boolean flag = false;
        logger.info("pushPayload={}", pushPayload);
        ScheduleResult pushResult = null;
        ScheduleResult pushResultIOS = null;
        try {
            String sendPushName = "sendNo_" + pushPayload.getSendno();
            pushResult = this.getJPushClient().createSingleSchedule(sendPushName, sendTime, pushPayload);
            pushResultIOS = this.getJPushClientIOS().createSingleSchedule(sendPushName, sendTime, pushPayload);
            if (pushResult.getResponseCode() == 200 || pushResultIOS.getResponseCode() == 200) {
                flag = true;
            }
            logger.info("[极光推送]scheduleResult result is " + pushResult.toString());
            logger.info("[极光推送]scheduleResult result is " + pushResultIOS.toString());
        } catch (APIConnectionException e) {
            logger.error("[极光推送]Connection error. Should retry later. ", e);
        } catch (APIRequestException e) {
            logger.error("[极光推送]Error response from JPush server. Should review and fix it. ", e);
            logger.info("[极光推送]HTTP Status: " + e.getStatus());
            logger.info("[极光推送]Error Code: " + e.getErrorCode());
            logger.info("[极光推送]Error Message: " + e.getErrorMessage());
            singleDataResult.setMessage(e.getErrorMessage());
        }
        singleDataResult.setSuccess(flag);
        return singleDataResult;
    }

    /**
     * 整合给特定的用户
     *
     * @param content         通知内容
     * @param registrationIds 推送的用户ids
     * @return PushPayload
     */
    private PushPayload buildPushObject_registrationIds_alertWithTitle(String content, List<String> registrationIds) {

        return PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.registrationId(registrationIds))
                .setNotification(Notification.newBuilder().setAlert(content).build())
                .build();
    }


    /**
     * 整合给特定的用户
     *@param title 通知标题
     * @param content         通知内容
     * @param registrationIds 推送的用户ids
     * @return PushPayload
     */
    private PushPayload buildPushObject_registrationIds_alertWithTitle(String title,String content, List<String> registrationIds) {
       //System.out.println("----------buildPushObject_all_all_alert");
        //创建一个IosAlert对象,可指定APNs的alert、title等字段
        IosAlert.Builder builder = IosAlert.newBuilder();
        builder.setTitleAndBody(title,"",content);
        IosAlert iosAlert =  builder.build();
        return PushPayload.newBuilder()
                //指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台
                .setPlatform(Platform.all())
                //指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id
                .setAudience(Audience.registrationId(registrationIds))
                //jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发
                .setNotification(Notification.newBuilder()
                        //指定当前推送的android通知
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setAlert(content)
                                .setTitle(title)
                                //此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
                                .build())
                        //指定当前推送的iOS通知
                        .addPlatformNotification(IosNotification.newBuilder()
                                //传一个IosAlert对象,指定apns title、title、subtitle等
                                .setAlert(iosAlert)
                                //直接传alert
                                //此项是指定此推送的badge自动加1
                                .incrBadge(1)
                                //此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,
                                // 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音
                                .setSound("sound.caf")
                                //此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
                                //此项说明此推送是一个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(content)

                        .setTitle(title)

                        .build())

                .setOptions(Options.newBuilder()
                        //此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
                        .setApnsProduction(true)
                        //此字段是给开发者自己给推送编号,方便推送者分辨推送记录
                        .setSendno(1)

                        .build())

                .build();

    }

    /**
     * 向IOS平台所有用户推送消息
     *
     * @param title     通知标题
     * @param content   通知内容
     * @param extrasMap 数据传输
     * @param alias     发送人群标签
     * @return PushPayload
     */
    private PushPayload buildPushObject_ios_all_alertWithTitle(String title, String content, Map<String, String> extrasMap, String... alias) {
        if (extrasMap == null || extrasMap.isEmpty()) {
            extrasMap = new HashMap<String, String>();
        }
        // 批量删除数组中空元素
        String[] newAlias = removeArrayEmptyElement(alias);
        return PushPayload.newBuilder()
                // 指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台
                .setPlatform(Platform.ios())
                // 指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id
                // 别名为空,全员推送;别名不为空,按别名推送
                .setAudience((null == newAlias || newAlias.length == 0) ? Audience.all() : Audience.alias(alias))
                // jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发
                .setNotification(Notification.newBuilder()
                        // 指定当前推送的IOS通知
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(content)
                                .incrBadge(1)
                                .setSound("default")
                                // 此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
                                .addExtras(extrasMap)
                                // 此项说明此推送是一个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(content)
                        .setTitle(title)
                        .addExtras(extrasMap)
                        .build())

                .setOptions(Options.newBuilder()
                        // 此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
                        .setApnsProduction(true)
                        // 此字段是给开发者自己给推送编号,方便推送者分辨推送记录
                        .setSendno(1)
                        // 此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒
                        .setTimeToLive(86400)
                        .build())
                .build();
    }

    /**
     * 向android平台所有用户推送消息
     *
     * @param title     通知标题
     * @param content   通知内容
     * @param extrasMap 数据传输
     * @param alias     发送人群标签
     * @return PushPayload
     */
    private PushPayload buildPushObject_android_all_alertWithTitle(String title, String content, Map<String, String> extrasMap, String... alias) {
        if (extrasMap == null || extrasMap.isEmpty()) {
            extrasMap = new HashMap<String, String>();
        }
        // 批量删除数组中空元素
        String[] newAlias = removeArrayEmptyElement(alias);
        return PushPayload.newBuilder()
                // 指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台
                .setPlatform(Platform.android())
                // 指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id
                // 别名为空,全员推送;别名不为空,按别名推送
                .setAudience((null == newAlias || newAlias.length == 0) ? Audience.all() : Audience.alias(alias))
                // jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发
                .setNotification(Notification.newBuilder()
                        // 指定当前推送的android通知
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setAlert(content)
                                .setTitle(title)
                                // 此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
                                .addExtras(extrasMap)
                                .build())
                        .build()
                )
                // Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。jpush的自定义消息,
                // sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的
                // [通知与自定义消息有什么区别?]了解通知和自定义消息的区别
                .setMessage(Message.newBuilder()
                        .setMsgContent(content)
                        .setTitle(title)
                        .addExtras(extrasMap)
                        .build())

                .setOptions(Options.newBuilder()
                        // 此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
                        .setApnsProduction(true)
                        // 此字段是给开发者自己给推送编号,方便推送者分辨推送记录
                        .setSendno(1)
                        // 此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒
                        .setTimeToLive(86400)
                        .build())
                .build();
    }

    /**
     * 构建Android和IOS的推送通知对象
     *
     * @return PushPayload
     */
    private PushPayload buildPushObject_Android_And_Ios(String title, String content, Map<String, String> extrasMap, String... alias) {
        if (extrasMap == null || extrasMap.isEmpty()) {
            extrasMap = new HashMap<String, String>();
        }
        // 批量删除数组中空元素
        String[] newAlias = removeArrayEmptyElement(alias);
        return PushPayload.newBuilder().setPlatform(Platform.android_ios())
                // 别名为空,全员推送;别名不为空,按别名推送
                .setAudience((null == newAlias || newAlias.length == 0) ? Audience.all() : Audience.alias(alias))
//                .setAudience(Audience.registrationId("d"))
                .setNotification(Notification.newBuilder().setAlert(content)
                        .addPlatformNotification(
                                AndroidNotification.newBuilder().setTitle(title).addExtras(extrasMap).build())
                        .addPlatformNotification(
                                IosNotification.newBuilder().incrBadge(1).addExtras(extrasMap).build())
                        .build())
                .build();
    }

    /**
     * 构建所有用户的对象
     *
     * @return PushPayload
     */
    private PushPayload buildPushObject_All(String title, String content, Map<String, String> extrasMap) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.all())
                .setNotification(Notification.newBuilder()
                                .setAlert(content)
                                .addPlatformNotification(AndroidNotification.newBuilder()
                                                .setAlert(content)
                                                .setTitle(title)
                                                //此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
                                .addExtras(extrasMap)
                                                .build()
                                )
                                .addPlatformNotification(IosNotification.newBuilder()
                                                //传一个IosAlert对象,指定apns title、title、subtitle等
                                                .setAlert(content)
                                                //直接传alert
                                                //此项是指定此推送的badge自动加1
                                                .incrBadge(1)
                                                //此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,
                                                // 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音
                                                .setSound("default")
                                                //此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
//                                .addExtras(extrasMap)
                                                //此项说明此推送是一个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(content)
                        .setTitle(title)
//                        .addExtra("url",extrasparam)
                        .build())

                .setOptions(Options.newBuilder()
                        //此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
                        .setApnsProduction(true)
                        //此字段是给开发者自己给推送编号,方便推送者分辨推送记录
                        .setSendno(1)
                        //此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒
                        .setTimeToLive(86400)
                        .build()
                )

                .build();
    }


}

 转载:极光推送工具类_jiguang-common_健身小白的博客-CSDN博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值