From 554618ab8cf9af501c47eea7a2f8999ae8f572ce Mon Sep 17 00:00:00 2001 From: heavyrian2012 Date: Fri, 18 Apr 2025 12:47:35 +0800 Subject: [PATCH] =?UTF-8?q?voip=20bye=E6=8E=A8=E9=80=81=E6=97=B6=E6=9B=BF?= =?UTF-8?q?=E6=8D=A2=E5=8E=9F=E6=9C=89call=20start=E7=9A=84=E6=8E=A8?= =?UTF-8?q?=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/wildfirechat/push/PushMessage.java | 1 + .../cn/wildfirechat/push/ios/ApnsServer.java | 59 +++++++++++++++++-- 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/src/main/java/cn/wildfirechat/push/PushMessage.java b/src/main/java/cn/wildfirechat/push/PushMessage.java index 740652b..2a9e807 100644 --- a/src/main/java/cn/wildfirechat/push/PushMessage.java +++ b/src/main/java/cn/wildfirechat/push/PushMessage.java @@ -31,6 +31,7 @@ public class PushMessage { public boolean isHiddenDetail; public String language; public long messageId; + public long callStartUid; //当消息被撤回/删除/更新时,这个值为true。 public boolean republish; diff --git a/src/main/java/cn/wildfirechat/push/ios/ApnsServer.java b/src/main/java/cn/wildfirechat/push/ios/ApnsServer.java index a8da3ef..79eec51 100644 --- a/src/main/java/cn/wildfirechat/push/ios/ApnsServer.java +++ b/src/main/java/cn/wildfirechat/push/ios/ApnsServer.java @@ -21,7 +21,7 @@ import org.springframework.util.StringUtils; import javax.annotation.PostConstruct; import java.io.File; -import java.util.Calendar; +import java.util.*; @Component public class ApnsServer { @@ -128,6 +128,40 @@ public class ApnsServer { return 0; } + private static class TimeUUID { + UUID uuid; + long timestamp; + + public TimeUUID(UUID uuid) { + this.uuid = uuid; + this.timestamp = System.currentTimeMillis(); + } + } + + private Map callPushId = new HashMap<>(); + + private synchronized void addCallPushId(long callId, UUID uuid) { + callPushId.put(callId, new TimeUUID(uuid)); + + //remove history record + Iterator> iterator = callPushId.entrySet().iterator(); + long now = System.currentTimeMillis(); + while (iterator.hasNext()) { + Map.Entry entry = iterator.next(); + if (now - entry.getValue().timestamp > 10*60*1000) { + iterator.remove(); + } + } + } + + private synchronized UUID getCallPushId(long callId) { + TimeUUID timeUUID = callPushId.remove(callId); + if (timeUUID != null) { + return timeUUID.uuid; + } + return null; + } + public void pushMessage(PushMessage pushMessage) { ApnsClient service; String sound = mConfig.alert; @@ -137,9 +171,14 @@ public class ApnsServer { collapseId = pushMessage.messageId + ""; } - if (pushMessage.pushMessageType == PushMessageType.PUSH_MESSAGE_TYPE_VOIP_INVITE) { + boolean isCallInvite = pushMessage.pushMessageType == PushMessageType.PUSH_MESSAGE_TYPE_VOIP_INVITE; + + if (isCallInvite) { sound = mConfig.voipAlert; } else if(pushMessage.pushMessageType == PushMessageType.PUSH_MESSAGE_TYPE_VOIP_BYE || pushMessage.pushMessageType == PushMessageType.PUSH_MESSAGE_TYPE_VOIP_ANSWER) { + if (pushMessage.pushMessageType == PushMessageType.PUSH_MESSAGE_TYPE_VOIP_BYE && pushMessage.callStartUid > 0) { + collapseId = pushMessage.callStartUid + ""; + } sound = null; } else if(pushMessage.pushMessageType == PushMessageType.PUSH_MESSAGE_TYPE_RECALLED || pushMessage.pushMessageType == PushMessageType.PUSH_MESSAGE_TYPE_DELETED) { sound = null; @@ -177,6 +216,12 @@ public class ApnsServer { Calendar c = Calendar.getInstance(); ApnsPushNotification pushNotification; + UUID apnsId = null; + + if(pushMessage.pushMessageType == PushMessageType.PUSH_MESSAGE_TYPE_VOIP_BYE && pushMessage.callStartUid > 0) { + apnsId = getCallPushId(pushMessage.callStartUid); + } + if (!mConfig.voipFeature || pushMessage.pushMessageType != PushMessageType.PUSH_MESSAGE_TYPE_VOIP_INVITE) { if (pushMessage.getPushType() == IOSPushType.IOS_PUSH_TYPE_DISTRIBUTION) { service = productSvc; @@ -186,7 +231,7 @@ public class ApnsServer { if(pushMessage.pushMessageType != PushMessageType.PUSH_MESSAGE_TYPE_VOIP_INVITE || StringUtils.isEmpty(pushMessage.getVoipDeviceToken())) { c.add(Calendar.MINUTE, 10); //普通推送 String payload = payloadBuilder.buildWithDefaultMaximumLength(); - pushNotification = new SimpleApnsPushNotification(pushMessage.deviceToken, pushMessage.packageName, payload, c.getTime(), DeliveryPriority.CONSERVE_POWER, PushType.ALERT, collapseId); + pushNotification = new SimpleApnsPushNotification(pushMessage.deviceToken, pushMessage.packageName, payload, c.getTime(), DeliveryPriority.CONSERVE_POWER, PushType.ALERT, collapseId, apnsId); } else { c.add(Calendar.MINUTE, 1); //voip通知,使用普通推送 payloadBuilder.setContentAvailable(true); @@ -194,7 +239,7 @@ public class ApnsServer { payloadBuilder.addCustomProperty("voip_type", pushMessage.pushMessageType); payloadBuilder.addCustomProperty("voip_data", pushMessage.pushData); String payload = payloadBuilder.buildWithDefaultMaximumLength(); - pushNotification = new SimpleApnsPushNotification(pushMessage.deviceToken, pushMessage.packageName, payload, c.getTime(), DeliveryPriority.IMMEDIATE, PushType.BACKGROUND, collapseId); + pushNotification = new SimpleApnsPushNotification(pushMessage.deviceToken, pushMessage.packageName, payload, c.getTime(), DeliveryPriority.IMMEDIATE, PushType.BACKGROUND, collapseId, apnsId); } } else { if (pushMessage.getPushType() == IOSPushType.IOS_PUSH_TYPE_DISTRIBUTION) { @@ -204,7 +249,7 @@ public class ApnsServer { } c.add(Calendar.MINUTE, 1); String payload = payloadBuilder.buildWithDefaultMaximumLength(); - pushNotification = new SimpleApnsPushNotification(pushMessage.voipDeviceToken, pushMessage.packageName + ".voip", payload, c.getTime(), DeliveryPriority.IMMEDIATE, PushType.VOIP, collapseId); + pushNotification = new SimpleApnsPushNotification(pushMessage.voipDeviceToken, pushMessage.packageName + ".voip", payload, c.getTime(), DeliveryPriority.IMMEDIATE, PushType.VOIP, collapseId, apnsId); } SimpleApnsPushNotification simpleApnsPushNotification = (SimpleApnsPushNotification)pushNotification; @@ -228,6 +273,10 @@ public class ApnsServer { } else { LOG.info("push success: {}", pushNotificationResponse.getApnsId().toString()); LOG.info("token invalidate timestamp: {}", pushNotificationResponse.getTokenInvalidationTimestamp()); + + if (isCallInvite) { + addCallPushId(pushMessage.messageId, pushNotificationResponse.getApnsId()); + } } } else { // Something went wrong when trying to send the notification to the