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

标签 java android ios spring google-cloud-messaging

在android中成功完成推送通知。现在处理 ios 推送通知。

gcm服务器推送通知的情况下,服务器通知 token 未注册或无效等。同样,有任何方法可以在apns服务器中获取。

请检查我的代码以在 APNS 中发送推送通知

 public void pushMessage() {
        ApnsService service = null;
        try {
            // get the certificate
            InputStream certStream = this.getClass().getClassLoader().getResourceAsStream("your_certificate.p12");
            service = APNS.newService().withCert(certStream, "your_cert_password").withSandboxDestination().build();
            // or
            // service = APNS.newService().withCert(certStream,
            // "your_cert_password").withProductionDestination().build();
            service.start();
             // read your user list
            List<User> 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'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("some message you want to send here");
                    // check if the message is too long (it won'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();
            }

        }
    }

我使用com.notnoop.apns库来发送APNS推送通知。

最佳答案

您必须从列表中删除不再使用的设备,请使用以下代码

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

这里serviceApnsService的对象。

ApnsService service;

关于java - 获取APNS服务器中未注册 token 的方法(使用Spring Server),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34915399/

相关文章:

带有 TextFields 问题的 Java 插入查询

java - 使用hibernate处理大量数据

android - 发布适用于 Android 9 (Android API 28) 的 Cordova 应用程序出现互联网连接错误

android - 如何使用 Android 提高复杂 ListView 创建的(感知)速度?

ios - 这个月有多少个工作日

java - JavaFX validator

java - Eclipse 应用程序在 Ubuntu 18.04 上崩溃

java - 使用应用程序发送 HTTP 请求 - Android Studio

ios - 在 NSAttributedString 中垂直居中两种不同大小的字体

ios - ObjectMapper:在映射函数中使用反射