2017-02-09 5 views
0

Ich möchte push Benachrichtigung an iphone mit java senden. Dafür cam über diese Bibliothek relayrides pushy. Aber ich bin neu in dieser APNS Sache.So senden Sie Push-Benachrichtigung in iPhone mit Java

Was ich tun möchte Ich möchte eine einfache Nachricht wie "Hallo Welt" auf das iPhone als Benachrichtigung senden.

Was ich habe Ich habe eine, Certificates.p12 Datei mit Passwort. Ich habe auch Gerät Token und Gcn ID.

kann mir jemand einen Beispielcode zum Verbinden und Senden push Benachrichtigung mit pushy zeigen. Welche Mavenabhängigkeit sollte ich einbeziehen? Weil jedes Mal, wenn ich eine Site besuche, Fehler wie SimpleApnsPushNotification Klasse nicht in Bibliothek oder PushManager Klasse nicht gefunden wird. Und was SLF4J mit dem Senden von Benachrichtigungen zu tun hat, bitte geben Sie mir einige relevante Code-Snippets für die Behandlung dieser Probleme, weil ich total durcheinander bin.

+1

down-Wähler Bitte beachten Sie, dass es auf Stackoverflow weder viel Ressourcen zur Verfügung steht noch beim googlen einer anderen Website. – JPG

Antwort

0

Nur um zu versuchen und Ihnen ein wenig helfen, werde ich einige Code, der aufdringlich implementiert. Die Methode send() akzeptiert eine Token-Zeichenfolge (das Gerät appId), einen String-Wert des zu sendenden Texts und eine benutzerdefinierte Eigenschaft, die Sie möglicherweise mitschicken möchten (nur als Beispiel für Sie enthalten). Ich hoffe wirklich, dass dir das hilft.

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