java 实现极光推送 单推模板

极光推送 推送给个人模板 首先 在app端通过用户手机号 调用 极光sdk 可以获取 一个极光推送的
push_id 然后保存到后台

				后台实现代码  
private static JPushClient getJPushClient() {
		return new JPushClient(JPUSH_MASTER_SECRET, JPUSH_APP_KEY, null, ClientConfig.getInstance());
	}

实现功能代码

public static void sendIosAndrodMsg(String pushId,String text) {
		JPushClient jpushClient =getJPushClient();
		Map<String, String> extras = new HashMap<String, String>();
		extras.put("System", "qjbh");
		Audience ad=Audience.newBuilder()
				.addAudienceTarget(AudienceTarget.tag("push1"))
				//备注   push1  就是一个tag  推送时 需要app订阅
				.addAudienceTarget(AudienceTarget.alias(pushId))
				.build();
		@SuppressWarnings("static-access")
		PushPayload payload=PushPayload.newBuilder()
				.setPlatform(Platform.android_ios())
				.setAudience(ad)
				//.setAudience(Audience.registrationId(pushId))
				.setNotification(Notification.newBuilder()
				.addPlatformNotification(IosNotification.newBuilder()
				// text  推送内容
				.setAlert(IosAlert.newBuilder().setTitleAndBody("", "", text).build())
				.setBadge(1).addExtras(extras).build())
				.addPlatformNotification(AndroidNotification.newBuilder().build().alert(text))
						.build())
				.setMessage(Message.newBuilder().setTitle("").setMsgContent(text).addExtras(extras).build())
				//JPUSH_DEPLOY_STATUS   模式选择   苹果有开发者模式和产品模式之分
				.setOptions(Options.newBuilder().setApnsProduction(JPUSH_DEPLOY_STATUS).build()).build();
		try {
			PushResult result = jpushClient.sendPush(payload);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

测试代码

public static void main(String[] args) throws Exception {
	
		sendIosAndrodMsg("通过app获取的push_id","推送给个人的 极光推送");
	}

需要的 jar包

<dependency>
			<groupId>cn.jpush.api</groupId>
			<artifactId>jpush-client</artifactId>
			<version>3.3.0</version>
		</dependency>

猜你喜欢

转载自blog.csdn.net/qq_41684939/article/details/83013303
今日推荐