push api v3 java_java后台,极光推送jpush v3版java示例

本文介绍了如何使用Java通过极光JPush API v3发送推送通知。首先创建JPushClient对象,然后构建PushPayload,包括设置平台、目标受众、通知内容和附加信息。最后发送PushPayload并捕获可能的错误。
摘要由CSDN通过智能技术生成

首先自己尝试过自己发http请求,调用极光推消息,虽然消息成功了,但是app收不到,尴尬,还是用官网现成的jar吧

流程原理:

第一是创建一个链接对象,  即 PushClient;

第二是创建推送对象 。即 PushPayload 。

步骤:

第一步导入jar包:

执行main:

package com.test.core.commons.utils;

import cn.jiguang.common.ClientConfig;

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.IosNotification;

import cn.jpush.api.push.model.notification.Notification;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

public class JPushClientUtil {

//在极光注册上传应用的 appKey 和 masterSecret

private static final String appKey ="7b6c132dd2xxxxff37bfxxx0";必填,例如466f7032ac604e02fb7bda89

private static final String masterSecret = "284f35c2xxxxc912a74dxxx0";//必填,每个应用都对应一个masterSecret

public static void main(String[] args) throws Exception {

JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, ClientConfig.getInstance());

// For push, all you need do is to build PushPayload object.

PushPayload payload = buildPushObject_ios_tagAnd_alertWithExtrasAndMessage();

try {

PushResult result = jpushClient.sendPush(payload);

System.out.println(result.msg_id);

System.out.println(result);

} catch (APIConnectionException e) {

// Connection error, should retry later

//LOG.error("Connection error, should retry later", e);

} catch (APIRequestException e) {

// Should review the error, and fix the request

//LOG.error("Should review the error, and fix the request", e);

//LOG.info("HTTP Status: " + e.getStatus());

//LOG.info("Error Code: " + e.getErrorCode());

//LOG.info("Error Message: " + e.getErrorMessage());

}

}

public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage() {

return PushPayload.newBuilder()

.setPlatform(Platform.ios())

.setAudience(Audience.alias("1647af0e50e4444dbe05b3d7b79dadfd"))

.setNotification(Notification.newBuilder()

.addPlatformNotification(IosNotification.newBuilder()

.setAlert("大家好,我来推个消息")

.setSound("happy")

.addExtra("issueId", "xxx")

.addExtra("type","我是问题")

.build())

.build())

.setMessage(Message.content("hgg推车了"))

.setOptions(Options.newBuilder()

.setApnsProduction(false)

.build())

.build();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值