菜鸟教程小白 发表于 2022-12-12 10:14:49

java - 在 APNS 服务器中知道未注册 token 的方法(使用 Spring Server)


                                            <p><p>在 android 中成功完成推送通知。现在处理 ios 推送通知。</p>

<p>在<code>gcm</code>服务器推送通知的情况下,服务器通知 token 未注册或无效等。同样有任何方法可以进入<code>apns</code>服务器。</code> p>

<p>请检查我的代码以在 <code>APNS</code></p> 中发送推送通知

<pre><code> public void pushMessage() {
      ApnsService service = null;
      try {
            // get the certificate
            InputStream certStream = this.getClass().getClassLoader().getResourceAsStream(&#34;your_certificate.p12&#34;);
            service = APNS.newService().withCert(certStream, &#34;your_cert_password&#34;).withSandboxDestination().build();
            // or
            // service = APNS.newService().withCert(certStream,
            // &#34;your_cert_password&#34;).withProductionDestination().build();
            service.start();
             // read your user list
            List&lt;User&gt; userList = userDao.readUsers();
            for (User user : userList) {
                try {
                  // we had a daily update here, so we need to know how many
                  //days the user hasn&#39;t started the app
                  // so that we get the number of updates to display it as the badge.
                  int days = (int) ((System.currentTimeMillis() - user.getLastUpdate()) / 1000 / 60 / 60 / 24);
                  PayloadBuilder payloadBuilder = APNS.newPayload();
                  payloadBuilder = payloadBuilder.badge(days).alertBody(&#34;some message you want to send here&#34;);
                  // check if the message is too long (it won&#39;t be sent if it is)
                  //and trim it if it is.
                  if (payloadBuilder.isTooLong()) {
                        payloadBuilder = payloadBuilder.shrinkBody();
                  }
                  String payload = payloadBuilder.build();
                  String token = user.getToken();
                  service.push(token, payload);
                } catch (Exception ex) {
                  // some logging stuff
                }
            }
      } catch (Exception ex) {
            // more logging
      } finally {
            // check if the service was successfull initialized and stop it here, if it was
            if (service != null) {
                service.stop();
            }

      }
    }
</code></pre>

<p>我使用 <code>com.notnoop.apns</code> 库发送 <code>APNS</code> 推送通知。 </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您必须从列表中删除不再使用的设备,请使用以下代码</p>

<pre><code>Map&lt;String, Date&gt; inactiveDevices = service.getInactiveDevices();
            for (String deviceToken : inactiveDevices.keySet()) {
               // delete from table
            }
</code></pre>

<p>这里的<code>service</code>是<code>ApnsService</code>的对象。</p>

<pre><code>ApnsService service;
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于java - 在 APNS 服务器中知道未注册 token 的方法(使用 Spring Server),我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/34915399/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/34915399/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: java - 在 APNS 服务器中知道未注册 token 的方法(使用 Spring Server)