Nur um zu versuchen und Ihnen ein wenig helfen, werde ich einige Code, der aufdringlich implementiert. Die Methode send()
akzeptiert eine Token-Zeichenfolge (das Gerät appId), einen String-Wert des zu sendenden Texts und eine benutzerdefinierte Eigenschaft, die Sie möglicherweise mitschicken möchten (nur als Beispiel für Sie enthalten). Ich hoffe wirklich, dass dir das hilft.
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-Wähler Bitte beachten Sie, dass es auf Stackoverflow weder viel Ressourcen zur Verfügung steht noch beim googlen einer anderen Website. – JPG