极光推送自定义扩展参数

极光推送自定义扩展参数,调用app相关页面。

示例:

public static void main(String... strings) {
		String APP_KEY = "d8dd8c016ca16701fdf0";
		String MASTER_SECRET = "6beddabd1c9983e";
		ClientConfig clientConfig = ClientConfig.getInstance();
		String host = (String) clientConfig.get(ClientConfig.PUSH_HOST_NAME);
		final NettyHttpClient client = new NettyHttpClient(ServiceHelper.getBasicAuthorization(APP_KEY, MASTER_SECRET),
				null, clientConfig);
		try {
			URI uri = new URI(host + clientConfig.get(ClientConfig.PUSH_PATH));
			PushPayload payload = buildPushObject_ios_tagAnd_alertWithExtrasAndMessage();
			client.sendRequest(HttpMethod.POST, payload.toString(), uri, new NettyHttpClient.BaseCallback() {
				@Override
				public void onSucceed(ResponseWrapper responseWrapper) {
					System.out.println("Got result: " + responseWrapper.responseContent);
				}
			});
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
	}

public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage() {
		   Map<String, String> extras = new HashMap<String, String>();
	        extras.put("key1", "value1");
	        extras.put("key2", "value2");
	        return PushPayload.newBuilder()
	                .setPlatform(Platform.android_ios())
	                //.setAudience(Audience.all())
	                .setAudience(Audience.registrationId("161a3797c8921c30705"))
	                .setNotification(Notification.newBuilder()
	                        .setAlert("alert content")
	                        .addPlatformNotification(AndroidNotification.newBuilder()
	                                .setTitle("Android Title")
	                                .addExtras(extras).build())
	                        .addPlatformNotification(IosNotification.newBuilder()
	                                .incrBadge(1)
	                                .setBadge(1)
	                                .setSound("default")
	                                //.addExtra("extra_key", "extra_value")
	                                .addExtras(extras)
	                                .build())
	                        .build())
	                .build();
	}

说明:appKey、masterSecret为极光推送平台添加应用后获得的相关参数。

使用API推送消息前,应确保使用极光控制台推送消息是正常的。

发送:

private void send(PushPayload payload) {
		ClientConfig clientConfig = ClientConfig.getInstance();
		String host = (String) clientConfig.get(ClientConfig.PUSH_HOST_NAME);
		NettyHttpClient client = new NettyHttpClient(ServiceHelper.getBasicAuthorization(appKey, masterSecret), null,
				clientConfig);
		try {
			URI uri = new URI(host + clientConfig.get(ClientConfig.PUSH_PATH));
			client.sendRequest(HttpMethod.POST, payload.toString(), uri, new NettyHttpClient.BaseCallback() {
				@Override
				public void onSucceed(ResponseWrapper responseWrapper) {
					LOG.info("Got result: " + responseWrapper.responseContent);
				}
			});
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
}

广播:

PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.android_ios()).setAudience(Audience.all())
				.setNotification(Notification.newBuilder().setAlert(content)
						.addPlatformNotification(
								AndroidNotification.newBuilder().setTitle(null).addExtras(extras).build())
						.addPlatformNotification(IosNotification.newBuilder().incrBadge(1).setBadge(1)
								.setSound("default").addExtras(extras).build())
						.build())
				.build();
		send(payload);

安卓:

public void android(String deviceId, String content, String title, Map<String, String> extras) {
		PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.android())
				.setAudience(Audience.registrationId(deviceId))
				.setNotification(Notification.newBuilder().setAlert(content)
						.addPlatformNotification(
								AndroidNotification.newBuilder().setTitle(title).addExtras(extras).build())
						.build())
				.build();
		send(payload);
	}

iOS:

public void ios(String deviceId, String content, Map<String, String> extras) {
		PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.ios())
				.setAudience(Audience.registrationId(deviceId))
				.setNotification(
						Notification
								.newBuilder().setAlert(content).addPlatformNotification(IosNotification.newBuilder()
										.incrBadge(1).setBadge(1).setSound("default").addExtras(extras).build())
								.build())
				.build();
		send(payload);
	}