Galet
Galet

Reputation: 6299

How can i add extra key in UrbanAirship API using java

I am sending push notifications using UrbanAirship API using java.

Here is the doc: http://docs.urbanairship.com/api/

I want to send a push notifications with custom key/value. For example, I want send to following to Android/iOS device

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?");
}

Here is the code reference for android - https://github.com/urbanairship/java-library/blob/master/src/test/java/com/urbanairship/api/push/model/notification/android/AndroidDevicePayloadTest.java

How can i do send push notification with custom key/value using AndroidDevicePayload ?

Upvotes: 0

Views: 573

Answers (2)

Alon Rosenfeld
Alon Rosenfeld

Reputation: 1469

You can add to the "extras" object any key/value:

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

Then in the received push, you will find the object:

enter image description here

For more details go to urbanairship official documentation

Upvotes: 0

Saureco
Saureco

Reputation: 81

You can create your notification like this:

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

}

Upvotes: 0

Related Questions