OStack程序员社区-中国程序员成长平台

标题: java - 在 APNS 服务器中知道未注册 token 的方法(使用 Spring Server) [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 10:14
标题: java - 在 APNS 服务器中知道未注册 token 的方法(使用 Spring Server)

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

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

请检查我的代码以在 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 推送通知。



Best Answer-推荐答案


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

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/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4