少しでもお手伝いしたいと思っていますが、私はプッシーを実装するコードをいくつか含めるつもりです。 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;
}
}
down投票者の皆様にはstackoverflowで利用可能なリソースが他にありません。 – JPG