优化apns推送

This commit is contained in:
imhao183
2021-04-12 10:10:18 +08:00
parent 9edb2caf93
commit 8587582c42

View File

@@ -90,25 +90,6 @@ public class ApnsServer {
public void pushMessage(PushMessage pushMessage) { public void pushMessage(PushMessage pushMessage) {
ApnsClient service; ApnsClient service;
if (pushMessage.getPushType() == IOSPushType.IOS_PUSH_TYPE_DISTRIBUTION) {
if (!mConfig.voipFeature || pushMessage.pushMessageType == PushMessageType.PUSH_MESSAGE_TYPE_NORMAL || StringUtils.isEmpty(pushMessage.getVoipDeviceToken())) {
service = productSvc;
} else {
service = productVoipSvc;
}
} else {
if (!mConfig.voipFeature || pushMessage.pushMessageType == PushMessageType.PUSH_MESSAGE_TYPE_NORMAL || StringUtils.isEmpty(pushMessage.getVoipDeviceToken())) {
service = developSvc;
} else {
service = developVoipSvc;
}
}
if (service == null) {
LOG.error("Service not exist!!!!");
return;
}
String sound = mConfig.alert; String sound = mConfig.alert;
String pushContent = pushMessage.getPushContent(); String pushContent = pushMessage.getPushContent();
@@ -197,23 +178,19 @@ public class ApnsServer {
payloadBuilder.setBadgeNumber(badge); payloadBuilder.setBadgeNumber(badge);
payloadBuilder.setSound(sound); payloadBuilder.setSound(sound);
final String token;
if (service == productVoipSvc || service == developVoipSvc) {
token = pushMessage.voipDeviceToken;
} else {
token = pushMessage.deviceToken;
}
Calendar c = Calendar.getInstance(); Calendar c = Calendar.getInstance();
ApnsPushNotification pushNotification; ApnsPushNotification pushNotification;
if (!mConfig.voipFeature || pushMessage.pushMessageType == PushMessageType.PUSH_MESSAGE_TYPE_NORMAL || StringUtils.isEmpty(pushMessage.getVoipDeviceToken())) { if (!mConfig.voipFeature || pushMessage.pushMessageType == PushMessageType.PUSH_MESSAGE_TYPE_NORMAL || StringUtils.isEmpty(pushMessage.getVoipDeviceToken())) {
if (pushMessage.getPushType() == IOSPushType.IOS_PUSH_TYPE_DISTRIBUTION) {
service = productSvc;
} else {
service = developSvc;
}
if(pushMessage.pushMessageType == PushMessageType.PUSH_MESSAGE_TYPE_NORMAL || StringUtils.isEmpty(pushMessage.getVoipDeviceToken())) { if(pushMessage.pushMessageType == PushMessageType.PUSH_MESSAGE_TYPE_NORMAL || StringUtils.isEmpty(pushMessage.getVoipDeviceToken())) {
c.add(Calendar.MINUTE, 10); //普通推送 c.add(Calendar.MINUTE, 10); //普通推送
String payload = payloadBuilder.buildWithDefaultMaximumLength(); String payload = payloadBuilder.buildWithDefaultMaximumLength();
pushNotification = new SimpleApnsPushNotification(token, pushMessage.packageName, payload, c.getTime(), DeliveryPriority.CONSERVE_POWER, PushType.ALERT); pushNotification = new SimpleApnsPushNotification(pushMessage.deviceToken, pushMessage.packageName, payload, c.getTime(), DeliveryPriority.CONSERVE_POWER, PushType.ALERT);
} else { } else {
c.add(Calendar.MINUTE, 1); //voip通知,使用普通推送 c.add(Calendar.MINUTE, 1); //voip通知,使用普通推送
payloadBuilder.setContentAvailable(true); payloadBuilder.setContentAvailable(true);
@@ -221,14 +198,23 @@ public class ApnsServer {
payloadBuilder.addCustomProperty("voip_type", pushMessage.pushMessageType); payloadBuilder.addCustomProperty("voip_type", pushMessage.pushMessageType);
payloadBuilder.addCustomProperty("voip_data", pushMessage.pushData); payloadBuilder.addCustomProperty("voip_data", pushMessage.pushData);
String payload = payloadBuilder.buildWithDefaultMaximumLength(); String payload = payloadBuilder.buildWithDefaultMaximumLength();
pushNotification = new SimpleApnsPushNotification(token, pushMessage.packageName, payload, c.getTime(), DeliveryPriority.IMMEDIATE, PushType.BACKGROUND); pushNotification = new SimpleApnsPushNotification(pushMessage.deviceToken, pushMessage.packageName, payload, c.getTime(), DeliveryPriority.IMMEDIATE, PushType.BACKGROUND);
} }
} else { } else {
if (pushMessage.getPushType() == IOSPushType.IOS_PUSH_TYPE_DISTRIBUTION) {
service = productVoipSvc;
} else {
service = developVoipSvc;
}
c.add(Calendar.MINUTE, 1); c.add(Calendar.MINUTE, 1);
String payload = payloadBuilder.buildWithDefaultMaximumLength(); String payload = payloadBuilder.buildWithDefaultMaximumLength();
pushNotification = new SimpleApnsPushNotification(token, pushMessage.packageName + ".voip", payload, c.getTime(), DeliveryPriority.IMMEDIATE, PushType.VOIP); pushNotification = new SimpleApnsPushNotification(pushMessage.voipDeviceToken, pushMessage.packageName + ".voip", payload, c.getTime(), DeliveryPriority.IMMEDIATE, PushType.VOIP);
} }
if (service == null) {
LOG.error("Service not exist!!!!");
return;
}
final PushNotificationFuture<ApnsPushNotification, PushNotificationResponse<ApnsPushNotification>> final PushNotificationFuture<ApnsPushNotification, PushNotificationResponse<ApnsPushNotification>>
sendNotificationFuture = service.sendNotification(pushNotification); sendNotificationFuture = service.sendNotification(pushNotification);
@@ -241,13 +227,15 @@ public class ApnsServer {
if (future.isSuccess()) { if (future.isSuccess()) {
final PushNotificationResponse<ApnsPushNotification> pushNotificationResponse = final PushNotificationResponse<ApnsPushNotification> pushNotificationResponse =
sendNotificationFuture.getNow(); sendNotificationFuture.getNow();
if(!pushNotificationResponse.isAccepted()) {
// Handle the push notification response as before from here. LOG.error("apns push failure: {}", pushNotificationResponse.getRejectionReason());
}
} else { } else {
// Something went wrong when trying to send the notification to the // Something went wrong when trying to send the notification to the
// APNs gateway. We can find the exception that caused the failure // APNs gateway. We can find the exception that caused the failure
// by getting future.cause(). // by getting future.cause().
future.cause().printStackTrace(); future.cause().printStackTrace();
LOG.error("apns push failure: {}", future.cause().getLocalizedMessage());
} }
} }
}); });