菜鸟教程小白 发表于 2022-12-12 14:00:06

java - APNS 接受推送消息,但它们永远不会成为目标


                                            <p><p>我正在尝试为我的后端服务器服务添加苹果推送通知,但我遇到了一个我无法理解的问题。<br/>
我正在使用 Pushy 库 <a href="https://github.com/relayrides/pushy" rel="noreferrer noopener nofollow">(com.relayriders.pushy)</a> .<br/>
我的代码就像他们的 github 页面推荐的一样。<br/>
代码是有效的,没有异常(exception),push 看起来像是正确形成的。推送发送到 APNS,但永远不会到达设备。<br/>
设备 token 和证书是正确的,我的 friend 通过他的测试程序向目标设备发送了推送。<br/>
我也测试了<a href="https://github.com/notnoop/java-apns" rel="noreferrer noopener nofollow">com.notnoop.apns</a>具有相同结果的库 - 没有异常(exception),仍然没有推送设备<br/>
这是我的发件人类:<br/></p>

<pre><code>package ua.asprelis.communicator.push;

import com.relayrides.pushy.apns.ApnsEnvironment;
import com.relayrides.pushy.apns.FailedConnectionListener;
import com.relayrides.pushy.apns.PushManager;
import com.relayrides.pushy.apns.PushManagerConfiguration;
import com.relayrides.pushy.apns.RejectedNotificationListener;
import com.relayrides.pushy.apns.RejectedNotificationReason;
import com.relayrides.pushy.apns.util.ApnsPayloadBuilder;
import com.relayrides.pushy.apns.util.MalformedTokenStringException;
import com.relayrides.pushy.apns.util.SSLContextUtil;
import com.relayrides.pushy.apns.util.SimpleApnsPushNotification;
import com.relayrides.pushy.apns.util.TokenUtil;
import java.io.IOException;
import java.net.URL;
import java.net.URLDecoder;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.SSLHandshakeException;


public class ApplePushNotifier
{
    private final PushManager&lt;SimpleApnsPushNotification&gt; pushManager;

    private final ApnsPayloadBuilder payloadBuilder = new ApnsPayloadBuilder();

    public ApplePushNotifier() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException, KeyManagementException, IOException {
      URL url = getClass().getResource(&#34;/certpath/cert.p12&#34;);
      this.pushManager = new PushManager&lt;&gt;(
            ApnsEnvironment.getSandboxEnvironment(),
            SSLContextUtil.createDefaultSSLContext(URLDecoder.decode(url.getPath(), &#34;UTF-8&#34;), &#34;password&#34;),
            null, // Optional: custom event loop group
            null, // Optional: custom ExecutorService for calling listeners
            null, // Optional: custom BlockingQueue implementation
            new PushManagerConfiguration(),
            &#34;ExamplePushManager&#34;);
      pushManager.start();
      pushManager.registerRejectedNotificationListener(new MyRejectedNotificationListener());
      pushManager.registerFailedConnectionListener(new MyFailedConnectionListener());
    }

    public void sendpush(String message, byte[] token) throws InterruptedException {
      String payload = payloadBuilder.setAlertBody(message).setSoundFileName(&#34;ring-ring.aiff&#34;).buildWithDefaultMaximumLength();
      pushManager.getQueue().put(new SimpleApnsPushNotification(token, payload));
    }

    public void sendpush(String message, String stoken) throws InterruptedException, MalformedTokenStringException {
      byte[]token = TokenUtil.tokenStringToByteArray(stoken);
      String payload = payloadBuilder.setAlertBody(message).setSoundFileName(&#34;ring-ring.aiff&#34;).buildWithDefaultMaximumLength();
      SimpleApnsPushNotification notification = new SimpleApnsPushNotification(token, payload);
      pushManager.getQueue().put(notification);
      System.out.println(&#34;Queued: &#34;+notification);
    }

    public void closeSender() {
      if(pushManager!=null) {
            try {
                pushManager.shutdown();
            } catch (InterruptedException ex) {
                Logger.getLogger(ApplePushNotifier.class.getName()).log(Level.SEVERE, null, ex);
            }
      }
    }

    private class MyRejectedNotificationListener implements RejectedNotificationListener&lt;SimpleApnsPushNotification&gt; {
      @Override
      public void handleRejectedNotification(
            final PushManager&lt;? extends SimpleApnsPushNotification&gt; pushManager,
            final SimpleApnsPushNotification notification,
            final RejectedNotificationReason reason) {
            System.out.format(&#34;%s was rejected with rejection reason %s\n&#34;, notification, reason);
      }
    }

    private class MyFailedConnectionListener implements FailedConnectionListener&lt;SimpleApnsPushNotification&gt; {
      @Override
      public void handleFailedConnection(
            final PushManager&lt;? extends SimpleApnsPushNotification&gt; pushManager,
            final Throwable cause) {
            if (cause instanceof SSLHandshakeException)
                // This is probably a permanent failure, and we should shut down the PushManager.
            else
                System.out.println(cause);
      }
    }
}
</code></pre>

<p>以及运行推送的测试类:<br/></p>

<pre><code>@Test
public void testPushMessage() {
    Random random = new Random();
    String testMessage = &#34;Hello Test! &#34;+random.nextInt(1000);
    String testToken = &#34;e6878d3993abfaec48220b9d4d3ea0b576c22351c7fbbb5faeb5449bf7f24452&#34;;
                      //e6878d39 93abfaec 48220b9d 4d3ea0b5 76c22351 c7fbbb5f aeb5449b f7f24452

    ApplePushNotifier notifier=null;
    try {
      notifier = new ApplePushNotifier();
      notifier.sendpush(testMessage, testToken);
    } catch(Exception ex) {
      Logger.getLogger(applePushNotifierTest.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
      if (notifier!=null)
            notifier.closeSender();
    }
}
</code></pre>

<p>我的输出:</p>

<pre><code> INFO com.relayrides.pushy.apns.PushManager - ExamplePushManager starting.
    Queued: SimpleApnsPushNotification , payload={&#34;aps&#34;:{&#34;alert&#34;:&#34;Hello Test! 955&#34;,&#34;sound&#34;:&#34;ring-ring.aiff&#34;}}, invalidationTime=null, priority=IMMEDIATE]
    INFO com.relayrides.pushy.apns.PushManager - ExamplePushManager shutting down.
</code></pre>

<p>我觉得问题出在一些小块上,我看不到。如果两个测试库都不起作用,那就奇怪了</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>正如我所说 - 我犯了一个愚蠢的错误。我真是个笨蛋。我用过</p>

<blockquote>
<p>ApnsEnvironment.getSandboxEnvironment()</p>
</blockquote>

<p>而不是</p>

<blockquote>
<p>ApnsEnvironment.getProductionEnvironment()</p>
</blockquote>

<p>在两个库中。</p>

<p>那是我第一次使用 APNS。</p></p>
                                   
                                                <p style="font-size: 20px;">关于java - APNS 接受推送消息,但它们永远不会成为目标,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/28220275/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/28220275/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: java - APNS 接受推送消息,但它们永远不会成为目标