如何使用java在iphone中发送推送通知

时间:2017-02-09 18:37:38

标签: java push-notification apple-push-notifications

我想使用push向iphone发送java通知。为此,我在这个名为relayrides pushy的库中进行了调查。但我不熟悉这个APNS事。

我想做什么 我想发送简单的信息,例如“你好世界”。以iphone作为通知。

我有什么 我有一个带密码的Certificates.p12文件。我还有设备令牌和gcn ID。

任何人都可以使用push向我展示一些连接和发送pushy通知的示例代码。我应该包含哪个maven依赖项?,因为每次访问某个站点时,我都会收到类似SimpleApnsPushNotification类错误的错误,或者找不到PushManager类。还有SLF4J与发送通知有什么关系,请提供一些相关的代码片段来处理这些问题,因为我完全搞砸了。

1 个答案:

答案 0 :(得分:0)

只是尝试帮助你一点点,我将包括一些实现pushy的代码。 send()方法接受一个令牌String(设备appId),您要发送的文本的String值,以及您可能想要发送的自定义属性(仅作为示例包含在您的内容中)。我真的希望这会帮助你。

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