Javapns를 사용한 apn 샘플코드

1. deviceToken을 하나씩 활용했는데 list로 활용하는게 좀더 실용적이다.

2. advanced payload를 활용함. 기존 Push 보다 좀더 기능성이 좋다. 

3. 전송 실패시 exception이 잡히지 않는다. 별도로 처리가 필요함.


@Service

@Scope("singleton")

public class ApnConnector {

    @Value("${apn.cert.path}") //인증서 경로

    private String apnCertPath;

    @Value("${apn.cert.password}") //인증서 패스워드

    private String apnCertPassword;


    private Logger logger = LoggerFactory.getLogger(getClass());


    public boolean sendMessage(String deviceToken, String locKey, List<String> args, String customMessage) throws Exception {

        try {

            PushNotificationPayload adPayload = PushNotificationPayload.complex();

            adPayload.addCustomAlertLocKey(locKey);

            adPayload.addCustomAlertLocArgs(args);

            adPayload.addCustomDictionary("msg", customMessage);

            PushedNotifications noti = Push.payload(adPayload, apnCertPath, apnCertPassword, false, new String[] {deviceToken});

            logger.debug("sendMessage success : " + noti.getSuccessfulNotifications().size());

            logger.debug("sendMessage fail : " + noti.getFailedNotifications().size());

        } catch (CommunicationException | KeystoreException e) {

            logger.error("sendMessage faild. :" + e.getMessage());

            throw new Exception();

        } catch (Exception e) {

            logger.error("sendMessage faild. :" + e.getMessage());

            throw new Exception();

        }

        return true;

    }


}



'java' 카테고리의 다른 글

Java 7 try catch 문법. autocloseable  (0) 2014.06.20
코드 품질 관리  (0) 2014.04.17
if 비교 연산자 우선순위  (0) 2014.04.01
abstract 와 interface 차이  (0) 2014.03.26
객체를 인자로 보내서 값을 설정하는것  (0) 2014.03.18

+ Recent posts