2017-02-09 21 views
0

java을 사용하여 push 알림을 iphone에 보내고 싶습니다. 이것을 위해 나는 relayrides pushy라고 불리는이 도서관을 가로 지른다. 그러나 나는이 APNS 것을 처음 사용합니다.java를 사용하여 iPhone에서 푸시 알림을 보내는 방법

내가 원하는 것 알림으로 iphone에 'hello world'와 같은 간단한 메시지를 보내고 싶습니다.

내가 가지고있는 것 나는 암호가있는 Certificates.p12 파일이 있습니다. 장치 토큰과 gcn ID도 있습니다.

어떤 사람이 pushy을 사용하여 push 알림을 연결하고 보내는 샘플 코드를 보여줄 수 있습니다. 내가 어떤 사이트를 방문 할 때마다 SimpleApnsPushNotification 클래스가 라이브러리에서 발견되지 않거나 PushManager 클래스를 찾을 수 없기 때문에 어떤 종속성을 포함시켜야합니까? 또한 SLF4J는 알림 전송과 관련이 있습니다. 이러한 문제를 처리하기위한 관련 코드 조각을 제공 해주시기 바랍니다. 왜냐하면 완전히 엉망이 되었기 때문입니다.

+1

down 유권자 stackoverflow에서 사용할 수있는 리소스가 많지 않으며 다른 사이트를 검색하는 동안주의해야합니다. – JPG

답변

0

조금만 있으면 도움을 받으려고 노력하는데, 나는 푸시를 구현하는 몇 가지 코드를 포함시킬 것입니다. send() 메서드는 토큰 String (장치 appId), 보내려는 텍스트의 String 값 및 보내려는 사용자 정의 속성 (사용자의 예제로만 포함됨)을 사용합니다. 나는 이것이 당신을 도와주기를 정말로 바란다.

public class PushNotificationManager { 
    private static final Log log = LogFactory.getLog(PushNotificationManager.class); 
    private static final Object SINGLETON_LOCK = new Object(); 
    private static final Object PUSH_MANAGER_LOCK = new Object(); 
    private static PushNotificationManager singleton; 
    private final SSLContext ssl; 
    private PushManager<SimpleApnsPushNotification> pushManager; 

    private PushNotificationManager() throws UnrecoverableKeyException, KeyManagementException, KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException { 
     File certificateFile = new File(PropertyUtil.getInstance().get("apns", "file")); 
     ssl = SSLContextUtil.createDefaultSSLContext(certificateFile.getAbsolutePath(), "yourCertPassword"); 
    } 

    private PushManager<SimpleApnsPushNotification> getPushManager() { 
     if (pushManager == null || pushManager.isShutDown()) { 
      synchronized (PUSH_MANAGER_LOCK) { 
       if (pushManager == null || pushManager.isShutDown()) { 
        ApnsEnvironment apnsEnviroment = ApnsEnvironment.getSandboxEnvironment(); 
        PushManagerConfiguration config = new PushManagerConfiguration(); 
        ApnsConnectionConfiguration connectionConfiguration = new ApnsConnectionConfiguration(); 
        config.setConnectionConfiguration(connectionConfiguration); 
        PushManager<SimpleApnsPushNotification> newPushManager = new PushManager<SimpleApnsPushNotification>(apnsEnviroment, ssl, null, null, null, config, "ExamplePushManager"); 
        newPushManager.start(); 
        pushManager = newPushManager; 
       } 
      } 
     } 
     return pushManager; 
    } 

    public static PushNotificationManager getInstance() { 
     if (singleton == null) { 
      synchronized (SINGLETON_LOCK) { 
       if (singleton == null) { 
        try { 
         singleton = new PushNotificationManager(); 
        } catch (UnrecoverableKeyException | KeyManagementException | KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException e) { 
         log.error("Error loading key.", e); 
        } 

       } 
      } 
     } 
     return singleton; 
    } 

    public void send(String tokenString, String text, String customProperty) throws MalformedTokenStringException, InterruptedException { 
     try { 
      final byte[] token = TokenUtil.tokenStringToByteArray(tokenString); 
      final ApnsPayloadBuilder payloadBuilder = new ApnsPayloadBuilder(); 
      payloadBuilder.setAlertBody(text); 
      payloadBuilder.addCustomProperty("customProperty", customProperty); 
      getPushManager().getQueue().put(new SimpleApnsPushNotification(token, payloadBuilder.buildWithDefaultMaximumLength())); 
     } catch (Exception e) { 
      pushManager = null; 
      throw e; 
     } 
    } 
관련 문제