2017-02-09 78 views
0

我想使用java向iphone發送push通知。爲此,我在這個名爲relayrides pushy的圖書館中拍攝。但我對這個APNS的新東西很陌生。如何使用java在iphone中發送推送通知

我想做什麼 我想發送像'hello world'這樣的簡單消息給iphone作爲通知。

我有什麼 我有一個,Certificates.p12文件與密碼。我也有設備令牌和gcn ID。

任何人都可以給我看一些使用pushy來連接和發送push通知的示例代碼。我應該包括哪些maven dependency ?,因爲我每次訪問某個站點時,都會收到錯誤,如SimpleApnsPushNotification類中未找到的類或PushManager類未找到。還有SLF4J與發送通知有什麼關係,請提供一些相關的代碼片段來處理這些問題,因爲我完全搞砸了。

+1

向下選民請注意,沒有太多的資源可用於此計算器上,也沒有谷歌搜索任何其他網站。 – JPG

回答

0

只是爲了嘗試和幫助你一點點,我將包含一些實現pushy的代碼。 send()方法需要一個令牌字符串(設備appId),您希望發送的文本的字符串值以及您可能想要發送的自定義屬性(僅作爲示例供您使用)。我真的希望這能幫助你。

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; 
     } 
    }