2017-02-09 18 views
0

を使用してpush通知をiphoneに送信します。このため私はrelayrides pushyと呼ばれるこの図書館を横切っています。しかし、私はこのAPNS事に初めてです。iPhoneでプッシュ通知を送信する方法

私がしたいこと 通知としてiphoneに「hello world」のような簡単なメッセージを送信します。

私が持っているもの 私にはパスワードがあるCertificates.p12ファイルが1つあります。私もデバイストークンとgcn IDを持っています。

いずれか1つは、pushyを使用して、push通知を接続して送信するサンプルコードを表示できます。私はいくつかのサイトにアクセスするたびにSimpleApnsPushNotificationクラスがライブラリに見つからないか、PushManagerクラスが見つからないため、どのmaven依存関係を含めるべきですか?また、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; 
     } 
    } 
関連する問題