package javapns; import java.util.*; import javapns.communication.exceptions.*; import javapns.devices.*; import javapns.devices.exceptions.*; import javapns.devices.implementations.basic.*; import javapns.feedback.*; import javapns.notification.*; import javapns.notification.transmission.*; /** *
Main class for easily interacting with the Apple Push Notification System
* *This is the best starting point for pushing simple or custom notifications, * or for contacting the Feedback Service to cleanup your list of devices.
* *The JavaPNS library also includes more advanced options such as * multithreaded transmission, special payloads, and more. * See the library's documentation at http://code.google.com/p/javapns/ * for more information.
* * @author Sylvain Pedneault * @see NotificationThreads */ public class Push { private Push() { } /** * Push a simple alert to one or more devices. * * @param message the alert message to push. * @param keystore a keystore containing your private key and the certificate signed by Apple ({@link java.io.File}, {@link java.io.InputStream}, byte[], {@link java.security.KeyStore} or {@link java.lang.String} for a file path) * @param password the keystore's password. * @param production true to use Apple's production servers, false to use the sandbox servers. * @param devices a list or an array of tokens or devices: {@link java.lang.String String[]}, {@link java.util.List}<{@link java.lang.String}>, {@link javapns.devices.Device Device[]}, {@link java.util.List}<{@link javapns.devices.Device}>, {@link java.lang.String} or {@link javapns.devices.Device} * @return a list of pushed notifications, each with details on transmission results and error (if any) * @throws KeystoreException thrown if an error occurs when loading the keystore * @throws CommunicationException thrown if an unrecoverable error occurs while trying to communicate with Apple servers */ public static PushedNotifications alert(String message, Object keystore, String password, boolean production, Object devices) throws CommunicationException, KeystoreException { return sendPayload(PushNotificationPayload.alert(message), keystore, password, production, devices); } /** * Push a simple badge number to one or more devices. * * @param badge the badge number to push. * @param keystore a keystore containing your private key and the certificate signed by Apple ({@link java.io.File}, {@link java.io.InputStream}, byte[], {@link java.security.KeyStore} or {@link java.lang.String} for a file path) * @param password the keystore's password. * @param production true to use Apple's production servers, false to use the sandbox servers. * @param devices a list or an array of tokens or devices: {@link java.lang.String String[]}, {@link java.util.List}<{@link java.lang.String}>, {@link javapns.devices.Device Device[]}, {@link java.util.List}<{@link javapns.devices.Device}>, {@link java.lang.String} or {@link javapns.devices.Device} * @return a list of pushed notifications, each with details on transmission results and error (if any) * @throws KeystoreException thrown if an error occurs when loading the keystore * @throws CommunicationException thrown if an unrecoverable error occurs while trying to communicate with Apple servers */ public static PushedNotifications badge(int badge, Object keystore, String password, boolean production, Object devices) throws CommunicationException, KeystoreException { return sendPayload(PushNotificationPayload.badge(badge), keystore, password, production, devices); } /** * Push a simple sound name to one or more devices. * * @param sound the sound name (stored in the client app) to push. * @param keystore a keystore containing your private key and the certificate signed by Apple ({@link java.io.File}, {@link java.io.InputStream}, byte[], {@link java.security.KeyStore} or {@link java.lang.String} for a file path) * @param password the keystore's password. * @param production true to use Apple's production servers, false to use the sandbox servers. * @param devices a list or an array of tokens or devices: {@link java.lang.String String[]}, {@link java.util.List}<{@link java.lang.String}>, {@link javapns.devices.Device Device[]}, {@link java.util.List}<{@link javapns.devices.Device}>, {@link java.lang.String} or {@link javapns.devices.Device} * @return a list of pushed notifications, each with details on transmission results and error (if any) * @throws KeystoreException thrown if an error occurs when loading the keystore * @throws CommunicationException thrown if an unrecoverable error occurs while trying to communicate with Apple servers */ public static PushedNotifications sound(String sound, Object keystore, String password, boolean production, Object devices) throws CommunicationException, KeystoreException { return sendPayload(PushNotificationPayload.sound(sound), keystore, password, production, devices); } /** * Push a notification combining an alert, a badge and a sound. * * @param message the alert message to push (set to null to skip). * @param badge the badge number to push (set to -1 to skip). * @param sound the sound name to push (set to null to skip). * @param keystore a keystore containing your private key and the certificate signed by Apple ({@link java.io.File}, {@link java.io.InputStream}, byte[], {@link java.security.KeyStore} or {@link java.lang.String} for a file path) * @param password the keystore's password. * @param production true to use Apple's production servers, false to use the sandbox servers. * @param devices a list or an array of tokens or devices: {@link java.lang.String String[]}, {@link java.util.List}<{@link java.lang.String}>, {@link javapns.devices.Device Device[]}, {@link java.util.List}<{@link javapns.devices.Device}>, {@link java.lang.String} or {@link javapns.devices.Device} * @return a list of pushed notifications, each with details on transmission results and error (if any) * @throws KeystoreException thrown if an error occurs when loading the keystore * @throws CommunicationException thrown if an unrecoverable error occurs while trying to communicate with Apple servers */ public static PushedNotifications combined(String message, int badge, String sound, Object keystore, String password, boolean production, Object devices) throws CommunicationException, KeystoreException { return sendPayload(PushNotificationPayload.combined(message, badge, sound), keystore, password, production, devices); } /** * Push a content-available notification for Newsstand. * * @param keystore a keystore containing your private key and the certificate signed by Apple ({@link java.io.File}, {@link java.io.InputStream}, byte[], {@link java.security.KeyStore} or {@link java.lang.String} for a file path) * @param password the keystore's password. * @param production true to use Apple's production servers, false to use the sandbox servers. * @param devices a list or an array of tokens or devices: {@link java.lang.String String[]}, {@link java.util.List}<{@link java.lang.String}>, {@link javapns.devices.Device Device[]}, {@link java.util.List}<{@link javapns.devices.Device}>, {@link java.lang.String} or {@link javapns.devices.Device} * @return a list of pushed notifications, each with details on transmission results and error (if any) * @throws KeystoreException thrown if an error occurs when loading the keystore * @throws CommunicationException thrown if an unrecoverable error occurs while trying to communicate with Apple servers */ public static PushedNotifications contentAvailable(Object keystore, String password, boolean production, Object devices) throws CommunicationException, KeystoreException { return sendPayload(NewsstandNotificationPayload.contentAvailable(), keystore, password, production, devices); } /** * Push a special test notification with an alert message containing useful debugging information. * * @param keystore a keystore containing your private key and the certificate signed by Apple ({@link java.io.File}, {@link java.io.InputStream}, byte[], {@link java.security.KeyStore} or {@link java.lang.String} for a file path) * @param password the keystore's password. * @param production true to use Apple's production servers, false to use the sandbox servers. * @param devices a list or an array of tokens or devices: {@link java.lang.String String[]}, {@link java.util.List}<{@link java.lang.String}>, {@link javapns.devices.Device Device[]}, {@link java.util.List}<{@link javapns.devices.Device}>, {@link java.lang.String} or {@link javapns.devices.Device} * @return a list of pushed notifications, each with details on transmission results and error (if any) * @throws KeystoreException thrown if an error occurs when loading the keystore * @throws CommunicationException thrown if an unrecoverable error occurs while trying to communicate with Apple servers */ public static PushedNotifications test(Object keystore, String password, boolean production, Object devices) throws CommunicationException, KeystoreException { return sendPayload(PushNotificationPayload.test(), keystore, password, production, devices); } /** * Push a preformatted payload to a list of devices. * * @param payload a simple or complex payload to push. * @param keystore a keystore containing your private key and the certificate signed by Apple ({@link java.io.File}, {@link java.io.InputStream}, byte[], {@link java.security.KeyStore} or {@link java.lang.String} for a file path) * @param password the keystore's password. * @param production true to use Apple's production servers, false to use the sandbox servers. * @param devices a list or an array of tokens or devices: {@link java.lang.String String[]}, {@link java.util.List}<{@link java.lang.String}>, {@link javapns.devices.Device Device[]}, {@link java.util.List}<{@link javapns.devices.Device}>, {@link java.lang.String} or {@link javapns.devices.Device} * @return a list of pushed notifications, each with details on transmission results and error (if any) * @throws KeystoreException thrown if an error occurs when loading the keystore * @throws CommunicationException thrown if an unrecoverable error occurs while trying to communicate with Apple servers */ public static PushedNotifications payload(Payload payload, Object keystore, String password, boolean production, Object devices) throws CommunicationException, KeystoreException { return sendPayload(payload, keystore, password, production, devices); } /** * Push a preformatted payload to a list of devices. * * @param payload a simple or complex payload to push. * @param keystore a keystore containing your private key and the certificate signed by Apple ({@link java.io.File}, {@link java.io.InputStream}, byte[], {@link java.security.KeyStore} or {@link java.lang.String} for a file path) * @param password the keystore's password. * @param production true to use Apple's production servers, false to use the sandbox servers. * @param devices a list or an array of tokens or devices: {@link java.lang.String String[]}, {@link java.util.List}<{@link java.lang.String}>, {@link javapns.devices.Device Device[]}, {@link java.util.List}<{@link javapns.devices.Device}>, {@link java.lang.String} or {@link javapns.devices.Device} * @return a list of pushed notifications, each with details on transmission results and error (if any) * @throws KeystoreException thrown if an error occurs when loading the keystore * @throws CommunicationException thrown if an unrecoverable error occurs while trying to communicate with Apple servers */ private static PushedNotifications sendPayload(Payload payload, Object keystore, String password, boolean production, Object devices) throws CommunicationException, KeystoreException { PushedNotifications notifications = new PushedNotifications(); if (payload == null) return notifications; PushNotificationManager pushManager = new PushNotificationManager(); try { AppleNotificationServer server = new AppleNotificationServerBasicImpl(keystore, password, production); pushManager.initializeConnection(server); ListRetrieve a list of devices that should be removed from future notification lists.
* *Devices in this list are ones that you previously tried to push a notification to, * but to which Apple could not actually deliver because the device user has either * opted out of notifications, has uninstalled your application, or some other conditions.
* *Important: Apple's Feedback Service always resets its list of inactive devices * after each time you contact it. Calling this method twice will not return the same * list of devices!
* *Please be aware that Apple does not specify precisely when a device will be listed * by the Feedback Service. More specifically, it is unlikely that the device will * be listed immediately if you uninstall the application during testing. It might * get listed after some number of notifications couldn't reach it, or some amount of * time has elapsed, or a combination of both.
* *Further more, if you are using Apple's sandbox servers, the Feedback Service will * probably not list your device if you uninstalled your app and it was the last one * on your device that was configured to receive notifications from the sandbox. * See the library's wiki for more information.
* * @param keystore a keystore containing your private key and the certificate signed by Apple ({@link java.io.File}, {@link java.io.InputStream}, byte[], {@link java.security.KeyStore} or {@link java.lang.String} for a file path) * @param password the keystore's password. * @param production true to use Apple's production servers, false to use the sandbox servers. * @return a list of devices that are inactive. * @throws KeystoreException thrown if an error occurs when loading the keystore * @throws CommunicationException thrown if an unrecoverable error occurs while trying to communicate with Apple servers */ public static List