레이블이 자바인 게시물을 표시합니다. 모든 게시물 표시
레이블이 자바인 게시물을 표시합니다. 모든 게시물 표시

2012년 11월 20일 화요일

Javapns 2.2 버전에서 Push 보내기

Javapns 1.5 버전에서 개발하다가
"Client already exists" 에러가 자주 발생하게 되었다.
똑같은 Push를 애플 서버에 보내게 되면 이런 현상이 있구나~ 했는데..
운영으로 올려보니 그런 경우 이외에도 과부하 발생 시 계속 에러가 났다.

해결방법을 아무리 찾아봐도 찾질 못해서,
JAVAPNS 홈페이지에서 2.2로 최신 버전 업데이트를 하게 되었다 :(

2.2 library 를 올렸더니 이게 웬걸..
얼마나 업데이트 되었는지, 엄청난 에러가 발생했다.
영문으로 된 많은 설명서를 보면서 머리가  깨질 듯 아팠지만,
그나마 간단하게 정리된 wiki 페이지를 보고 이해할 수 있게 되었다.

참고 링크 : http://code.google.com/p/javapns/wiki/PushNotificationBasic 



# 다수의 디바이스, 각각의 전혀 다른 payload 그리고 Thread #

내가 하고자 하는 Push는
여러 디바이스에 해당하는 각각의 전혀 다른 메세지(payload)..
그리고 이걸 multi Thread를 사용해서 보내고자 했다.

위의 참고 링크를 보면 간단히 설명되어 있는 것 중,
다음의 방안을 찾아낼 수 있었다.

1) 사용 Payloads
payloads (keystore, password, production, numberOfThreads, payloadDevicePairs): use the built-in multithreaded transmission engine to push payloads to paired devices



2) 이중에서 payloadDevicePairs 변수가 궁금해서 찾아보니,

  • Object payloadDevicePairs: a list of paired of payloads and tokens to push. You can pass the following objects to this parameter:

    • javapns.notification.PayloadPerDevice[]: an array of PayloadPerDevice objects
    • java.util.List<PayloadPerDevice>: a list of PayloadPerDevice objects
    • javapns.notification.PayloadPerDevice: a single PayloadPerDevice object



3) 이렇단다..
이걸봐도 도무지 어떻게하면 "paired"해서 날릴 수 있는가 했더니,
정말 친절하게도 "자주 질문하는 내용" 으로 해결해 주었다. wow!

Frequently Asked Questions

  1. How to send a different payload for each device?: if each device should receive a different payload (even slightly different), you will need to create a PayloadPerDevice object for each device along with the payload it should receive. You can then use the Push.payloads(...)methods to push your payloads. 
  2. Example:
    List<PayloadPerDevice> payloadDevicePairs = new Vector<PayloadPerDevice>();
     
  3. <사용예제>
    payloadDevicePairs.add(new PayloadPerDevice(PushNotificationPayload.alert("Hello World 1!"), myDevice1)); 
    payloadDevicePairs.add(new PayloadPerDevice(PushNotificationPayload.alert("Hello World 2!"), myDevice2)); 
    payloadDevicePairs.add(new PayloadPerDevice(PushNotificationPayload.alert("Hello World 3!"), myDevice3)); 
    Push.payloads("keystore.p12", "keystore_password", false, payloadDevicePairs);


이제 개발할 수 있겠다..!!

2012년 6월 21일 목요일

Java에서 CharSequence 를 String 으로 변환하는 방법

String s = "a string";
CharSequence cs = new StringBuffer("a string");
String s2 = cs.toString();
System.out.println("s.equals(s2): " + s.equals(s2));
System.out.println("s == s2: " + (s == s2));

2012년 6월 18일 월요일

자바 int -> char 변환

예를 들어 int a = 1 이라고 했을 때
char형으로 형변환하는 것이 아닌 문자 1로 바꾸는 방법

Java 공부를 하다가 int 값을 "문자열"로 변경하는 방법은 인터넷에서 검색이 많이 되는데
유독 int값 그대로를 문자로 바꾸는 방법이 안나와서 약 15분간 삽질하다가 알아낸거라 
잊지 않기 위해 포스팅을 합니다.

java.lang 패키지의 Character 클래스의 메소드로 forDigit()가 있습니다.
API문서에서 정의하는 forDigit()의 설명을 보면

public static char forDigit(int digit, int radix)

Determines the character representation for a specific digit in the specified radix. If the value of radix is not a valid radix, or the value of digit is not a valid digit in the specified radix, the null character ('\u0000') is returned.

The radix argument is valid if it is greater than or equal to MIN_RADIX and less than or equal to MAX_RADIX. The digit argument is valid if 0 <=digit < radix.

If the digit is less than 10, then '0' + digit is returned. Otherwise, the value 'a' + digit - 10 is returned.


예제를 들어 설명하자면

  1. int a = 1;  
  2. char c = a;  
  3. System.out.println(c);  
  4.   
  5. 결과 : ^A  

를 할 경우 c의 값을 출력하게되면 ASCII값 1에 해당하는 ^A가 됩니다.
하지만 forDigit() 사용하게되면 문자 1에 해당하는 ASCII 코드 값인 49를 반환하게 됩니다.

  1. int a = 1;  
  2. char c = Character.forDigit(a, 10);  
  3. System.out.println(c);  
  4.   
  5. 결과 : 1  



갑자기 왜 뜬금없이 int형을 문자형 char로 변경하는 것에 관심을 가지게 되었냐면

  1. FileOutputStream fos = new FileOutputStrea("test.txt");  
  2. for(int i = 0; i < 10; i++)  
  3. {  
  4.     fos.write(i);  
  5. }  

test.txt에 0에서부터 9까지 기록하게 하기위함이었는데
막상 결과는 다르게 나와서 해결하려고 보니............