繁体   English   中英

我如何使用Java在UrbanAirship API中添加额外的密钥

[英]How can i add extra key in UrbanAirship API using java

我正在使用Java使用UrbanAirship API发送推送通知。

这是文档: http//docs.urbanairship.com/api/

我想发送带有自定义键/值的推送通知。 例如,我要发送到以下Android / iOS设备

name: "Jack"

String appKey = "appKey";
String appSecret = "appSecret";

// Setup an authenticated APIClient with your application key and
// application master secret.
APIClient apiClient = APIClient.newBuilder()
        .setKey(appKey)
        .setSecret(appSecret)
        .build();

// Setup a push payload to send to the API with our handy builders
PushPayload payload = PushPayload.newBuilder()
        .setAudience(Selectors.all())
        .setNotification(Notifications.notification("UA Push"))
        .setDeviceTypes(DeviceTypeData.of(DeviceType.IOS))
        .build();
// Try and send, handle anything that comes up
try {
    APIClientResponse<APIPushResponse> response = apiClient.push(payload);
    logger.info("Sent a push message!");
}
// Non 200 responses throw an APIRequestException. Check the documentation
// to debug your request.
catch (APIRequestException ex){
    logger.error("Non 200 request, checking error details and taking action");
}
// An underlying error occurred, most likely outside of the scope of the
// UA library, do some HTTP debugging
catch (IOException e){
    logger.error("Broken pipe what?");
}

这是android的代码参考-https: //github.com/urbanairship/java-library/blob/master/src/test/java/com/urbanairship/api/push/model/notification/android/AndroidDevicePayloadTest.java

如何使用AndroidDevicePayload发送带有自定义键/值的推送通知?

您可以这样创建通知:

public PushPayload createPushPayloadCustom(String namedUser, String message) {
    Notification notification = Notification.newBuilder()
            .addDeviceTypeOverride(DeviceType.IOS, IOSDevicePayload.newBuilder()
                    .setAlert(message)
                    .build())
            .addDeviceTypeOverride(DeviceType.ANDROID, AndroidDevicePayload.newBuilder()
                    .setAlert(message)
                    .build())
            .build();       

    return PushPayload.newBuilder()
            .setAudience(Selectors.namedUser(namedUser))
            .setNotification(notification)
            .setDeviceTypes(DeviceTypeData.of(DeviceType.ANDROID, DeviceType.IOS))
            .build();

}

您可以将任何键/值添加到“ extras”对象中:

        DeviceTypeData deviceTypeData = DeviceTypeData.of(DeviceType.IOS, DeviceType.ANDROID);

    IOSDevicePayload iosPayload = IOSDevicePayload.newBuilder()
            .setAlert(message)
            .addExtraEntry("custom_ios_key", "custom value for IOS")
            .build();

    AndroidDevicePayload androidPayload = AndroidDevicePayload.newBuilder()
            .setAlert(message)
            .addExtraEntry("custom_android_key", "custom value for Android")
            .build();

    PushPayload payload = PushPayload.newBuilder()
            .setAudience(Selectors.namedUser(email))
            .setNotification(Notifications.notification(iosPayload,androidPayload))
            .setDeviceTypes(deviceTypeData)
            .build();

然后,在收到的推送中,您将找到对象:

在此处输入图片说明

有关更多详细信息,请参见urbanairship官方文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM