New Server Libraries Released — Python, PHP, and Java

With the release of API v3 we're offering a lot of power to developers working with our push notification system. As part of our goal to make integrations easier and more successful, we've built some server-side libraries for our API.

Python

We've had a Python library for some time, but it's been maintained as a side project by myself. As part of preparing for the API V3 release we reorganized the codebase. The existing v1 push and rich push functionality was maintained with a DeprecationWarning, and new functionality was added to support adhoc segmentation features. In addition we upgraded the HTTP transport to use requests, adding automatic reuse of HTTP keep-alives.

The audience and notification payload creation is handled with a series of functions in the urbanairship.push subpackages, and exposed via the top level package. There is a full set of audience and notification helpers to build out push requests, but it's easy to drop down to creating dictionaries and lists directly. Here's an example of sending a push to a tag, with different 'extra' data on iOS and Android:

import urbanairship as ua
airship = ua.Airship('app key', 'master secret')

push = airship.create_push()
push.audience = ua.tag('sports')
push.notification = ua.notification(
    ios=ua.ios(
        alert='New sports update!',
        extra={'article_id': '12345'}),
    android=ua.android(
        alert='New sports update for our treasured Android users!',
        extra={'fetch_article': 'news/12345'})
)
push.device_types = ua.device_types('ios', 'android')
response = push.send()

The current release is 0.6 beta; a full release will be out soon. Check out the Python library documentation, and the code on GitHub.

PHP

Similarly to Python, we had a small PHP library in the past, but it was more of an example than a reusable component. For the API v3 release, the PHP library has been totally re-written to make use of modern PHP application development tools like Composer. Here's the same example above, using the PHP library:

require_once 'vendor/autoload.php'; use UrbanairshipAirship; use UrbanAirshipPush as P; $airship = new Airship("app key", "master secret"); $response = $airship->push() ->setAudience(P ag("sports")) ->setNotification(P otification(null, Pios("New sports update!", null, null, false, array('article_id'=>'12345')), Pandroid("New sports update for our treasured Android users!", null, null, null, array('fetch_article'=>'news/12345'))) ->setDeviceTypes(PdeviceTypes('ios', 'android')) ->send();

The current release is 0.1, and should be considered a beta release. We welcome feedback on the style, structure, and whether it fits your needs. Check out the PHP library documentation, and the code on GitHub.

Java

Last but certainly not least, we've added a Java library. Since the Java library is totally new, we've started with the capability of sending segmented notifications and rich app pages. Here is an example of using the Java library:

import com.urbanairship.api.client.*;
import com.urbanairship.api.push.model.Platform;
import com.urbanairship.api.push.model.PlatformData;
import com.urbanairship.api.push.model.PushPayload;
import com.urbanairship.api.push.model.audience.Selectors;
import com.urbanairship.api.push.model.notification.Notifications;

public void sendPush(){

    String key = "applicationKey";
    String secret = "applicationMasterSecret";

    // Build and configure an APIClient
    APIClient apiClient = APIClient.newBuilder()
            .setKey(key)
            .setSecret(secret)
            .build();

    // Setup a payload for the message you want to send
    PushPayload payload = PushPayload.newBuilder()
                                     .setAudience(Selectors.all())
                                     .setNotification(Notifications.alert("API v3"))
                                     .setPlatforms(PlatformData.of(Platform.IOS))
                                     .build();
    // Try/Catch for any issues, any non 200 response, or non library
    // related exceptions
    try {
        APIClientResponse response = apiClient.push(payload);
        logger.debug(String.format("Response %s", response.toString()));
    }
    catch (APIRequestException ex){
        logger.error(String.format("APIRequestException " + ex));
        logger.error("Something wrong with the request " + ex.toString());
    }
    catch (IOException e){
        logger.error("IOException in API request " + e.getMessage());
    }

}

The Java library is a developer preview. We wanted to see how the style and usage patterns fit your applications. Check out the Java library documentation, and the code on GitHub.

Feedback and Contributions

We'd love to hear what you think of these libraries, and what features you'd like added next; just send us a note at support@urbanairship.com.

If you have bug fixes or new additions, we'd love to bring them in. To make things easier we've updated our Contribution License Agreement to be simpler and easier to understand; just fill that out and post a pull request on GitHub.