조금만 있으면 도움을 받으려고 노력하는데, 나는 푸시를 구현하는 몇 가지 코드를 포함시킬 것입니다. 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