极光推送

现在的需求是给iOS和Android用户推送消息

应用信息找到自己的

https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/,先看一遍接口文档

然后发现需要设置tag和alias啥的(主要是为了分组推送消息和推送消息给单个用户)https://docs.jiguang.cn/jpush/server/push/rest_api_v3_device/,发现官方文档挺清楚的哈,那就直接点上代码:

        <!-- 极光推送 -->
        <dependency>
            <groupId>cn.jpush.api</groupId>
            <artifactId>jpush-client</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>cn.jpush.api</groupId>
            <artifactId>jiguang-common</artifactId>
            <version>1.0.8</version>
        </dependency>
package com.xmjbq.jxmvip.thirdApiUtil;

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.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 com.xmjbq.jxmvip.util.LogUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author: zby
 * @Description: 极光推送
 */
@Component
public class JiPushUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(JiPushUtil.class);

    private final static String appKey = "******";

    private final static String masterSecret = "******";

    //true-推送生产环境 false-推送开发环境(测试使用参数)
    private final static Boolean ApnsProduction = false;

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

    public static PushResult buildPushObject_android_ios(String[] userList,String notification) {
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.alias(userList))
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .addExtra("type", "infomation")
                                .setTitle(notification)
                                .setAlert(notification)
                                .build())
                        .addPlatformNotification(IosNotification.newBuilder()
                                .addExtra("type", "infomation")
                                .setAlert(IosAlert.newBuilder().setTitleAndBody("zby", null, "5201314zby").build())
                                .setSound("detault").setBadge(0)
                                .build())
                        .build())
                .setOptions(Options.newBuilder()
                        .setApnsProduction(ApnsProduction)
                        .setTimeToLive(90)//消息在JPush服务器的失效时间(测试使用参数)
                        .build())
                .build();
        PushResult result = new PushResult();
        try {

            result = jPushClient.sendPush(payload);
        } catch (APIConnectionException e) {
            LogUtil.error("Connection error, should retry later", e);
            e.printStackTrace();e.fillInStackTrace();
            return null;
        } catch (APIRequestException e) {
            // Should review the error, and fix the request
            LogUtil.error("Should review the error, and fix the request", e);
            e.printStackTrace();e.fillInStackTrace();
            return null;
        }
        return result;

    }

   
    public static void sendMsg(String[] userids, String msg) {
        buildPushObject_android_ios(userids, msg);
    }

}

入坑点:IosAlert.newBuilder().setTitleAndBody("zby", null, "5201314zby").build(),iOS需要借助IosAlert类,安卓可以直接设置

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZBY52031

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值