Push IOS using javapns

Source: Internet
Author: User

Start

Package com. jynine. javapns; import java. io. fileNotFoundException; import java. io. IOException; import java. io. inputStream; import java. util. arrayList; import java. util. list; import java. util. map; import java. util. properties; import javapns. push; import javapns. communication. exceptions. communicationException; import javapns. communication. exceptions. keystoreException; import javapns. devices. device; import javapns. Devices. devices; import javapns. notification. payloadPerDevice; import javapns. notification. pushNotificationPayload; import javapns. notification. transmission. pushQueue; import org. apache. commons. lang. stringUtils; import org. json. JSONException; public class IosPushUtil {public static String keystore = null; public static String password = null; public static String host = null; public static Boolean produ Ction = true; // true: production false: sandbox public static final int numberOfThreads = 8; static {Properties propertie = new Properties (); InputStream inputStream; try {inputStream = IosPushUtil. class. getClassLoader (). getResourceAsStream ("push. properties "); propertie. load (inputStream); keystore = propertie. getProperty ("certificatePath"); password = propertie. getProperty ("certificatePassword", "123456"); h Ost = propertie. getProperty ("host", "gateway.push.apple.com"); production = Boolean. valueOf (propertie. getProperty ("production", "true"); inputStream. close ();} catch (FileNotFoundException ex) {ex. printStackTrace ();} catch (IOException ex) {ex. printStackTrace () ;}} public static void main (String [] args) throws Exception {// pushMsgNotification ("hello !!! 2 ", true," iostoken "); // pushbadgenoication ication (1," iostoken "); String [] devs = new String [10000]; for (int I = 0; I <devs. length; I ++) {devs [I] = "iostoken" ;}list <Device> devices = Devices. asDevices (devs); System. out. println (devices. size (); // pushPayLoadByThread (devices, "Hello 2222222", 1, null, null); // pushPayloadDevicePairs (devices, "Hello 111111111", 1, null, null); // pushPayloadDevicePairs (devices ," Hello ++ ", 1, null, null); queue (devices," Hello 2222222 ", 1, null, null );} /*** push a simple message * @ param msg message * @ param devices device * @ throws CommunicationException * @ throws KeystoreException */public static void pushMsgNotification (String msg, Object devices) throws CommunicationException, KeystoreException {Push. alert (msg, keystore, password, production, devices);}/*** push a tag * @ param badge tag * @ param Devices device * @ throws CommunicationException * @ throws KeystoreException */public static void pushBadgeNotification (int badge, Object devices) throws CommunicationException, KeystoreException {Push. badge (badge, keystore, password, production, devices );} /*** push a voice ** @ param sound voice * @ param devices device * @ throws CommunicationException * @ throws KeystoreException */public static void pushSoundNotific Ation (String sound, Object devices) throws CommunicationException, KeystoreException {Push. sound (sound, keystore, password, production, devices );} /*** push an alert + badge + sound notification * @ param message * @ param badge mark * @ param sound * @ param devices device * @ throws CommunicationException * @ throws KeystoreException */public static void pushCombinedNotification (String message, int badge, String sound, Objec T devices) throws CommunicationException, KeystoreException {Push. combined (message, badge, sound, keystore, password, production, devices );} /*** notify Apple magazine content * @ param devices device * @ throws CommunicationException * @ throws KeystoreException */public static void contentAvailable (Object devices) throws CommunicationException, KeystoreException {Push. contentAvailable (keystore, password, production, devi Ces);}/*** push useful debugging information * @ param devices device * @ throws CommunicationException * @ throws KeystoreException */public static void test (Object devices) throws CommunicationException, keystoreException {Push. test (keystore, password, production, devices );} /*** push custom load ** @ param devices * @ param msg * @ param badge * @ param sound * @ param map * @ throws JSONException * @ throws CommunicationException * @ throws KeystoreException */public static void pushPayload (List <Device> devices, String msg, Integer badge, String sound, Map <String, String> map) throws JSONException, CommunicationException, keystoreException {PushNotificationPayload payload = customPayload (msg, badge, sound, map); Push. payload (payload, keystore, password, production, devices);}/*** use a built-in thread to push load information * @ param devices * @ param msg * @ param badge * @ Param sound * @ param map * @ throws Exception */public static void pushPayLoadByThread (List <Device> devices, String msg, Integer badge, String sound, Map <String, string> map) throws Exception {PushNotificationPayload payload = customPayload (msg, badge, sound, map); Push. payload (payload, keystore, password, production, numberOfThreads, devices);}/*** push pairing information ** @ param devices * @ param msg * @ param badge * @ Param sound * @ param map * @ throws JSONException * @ throws CommunicationException * @ throws KeystoreException */public static void pushPayloadDevicePairs (List <Device> devices, String msg, Integer badge, string sound, Map <String, String> map) throws JSONException, CommunicationException, KeystoreException {List <PayloadPerDevice> payloadDevicePairs = new ArrayList <PayloadPerDevice> (); PayloadPerDevice pe RDevice = null; for (int I = 0; I <devices. size (); I ++) {perDevice = new PayloadPerDevice (customPayload (msg + "--->" + I, badge, sound, map), devices. get (I); payloadDevicePairs. add (perDevice);} Push. payloads (keystore, password, production, payloadDevicePairs );} /*** use a thread to push the pairing information * @ param devices * @ param msg * @ param badge * @ param sound * @ param map * @ throws Exception */public static void pushPayloadDevic EPairsByThread (List <Device> devices, String msg, Integer badge, String sound, Map <String, String> map) throws Exception {List <PayloadPerDevice> payloadDevicePairs = new ArrayList <PayloadPerDevice> (); PayloadPerDevice perDevice = null; for (int I = 0; I <devices. size (); I ++) {perDevice = new PayloadPerDevice (customPayload (msg + "--->" + I, badge, sound, map), devices. get (I); payloadDevicePairs. add (perDevice );} Push. payloads (keystore, password, production, numberOfThreads, payloadDevicePairs );} /*** queue multi-thread push * @ param devices * @ param msg * @ param badge * @ param sound * @ param map * @ throws KeystoreException * @ throws JSONException */public static void queue (List <Device> devices, string msg, Integer badge, String sound, Map <String, String> map) throws KeystoreException, JSONException {PushQueue queue = Push. queue (Keystore, password, production, numberOfThreads); queue. start (); PayloadPerDevice perDevice = null; for (int I = 0; I <devices. size (); I ++) {perDevice = new PayloadPerDevice (customPayload (msg + "--->" + I, badge, sound, map), devices. get (I); queue. add (perDevice );}} /*** custom load ** @ param msg * @ param badge * @ param sound * @ param map custom dictionary * @ return * @ throws JSONException */private static PushNotification Payload customPayload (String msg, Integer badge, String sound, Map <String, String> map) throws JSONException {PushNotificationPayload payload = PushNotificationPayload. complex (); if (StringUtils. isNotEmpty (msg) {payload. addAlert (msg);} if (badge! = Null) {payload. addBadge (badge);} payload. addSound (StringUtils. defaultIfEmpty (sound, "default"); if (map! = Null &&! Map. isEmpty () {Object [] keys = map. keySet (). toArray (); Object [] vals = map. values (). toArray (); if (keys! = Null & vals! = Null & keys. length = vals. length) {for (int I = 0; I <map. size (); I ++) {payload. addCustomDictionary (String. valueOf (keys [I]), String. valueOf (vals [I]) ;}} return payload ;}}

  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.