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

问题描述 投票:1回答:2

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

这是doc: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发送带有自定义键/值的推送通知?

java android urbanairship.com
2个回答
0
投票

您可以像这样创建通知:

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();

}

0
投票

您可以向“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();

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

enter image description here

有关更多详细信息,请访问urbanairship official documentation

© www.soinside.com 2019 - 2024. All rights reserved.