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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章