Новый протокол регистрации токенов
All checks were successful
Build rosetta-wss / build (push) Successful in 1m48s

This commit is contained in:
RoyceDa
2026-03-31 17:44:09 +02:00
parent 1e00105d87
commit d2263c6b9a
14 changed files with 391 additions and 169 deletions

View File

@@ -0,0 +1,78 @@
package im.rosetta.service.dispatch.push;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import im.rosetta.database.entity.Device;
import im.rosetta.database.entity.PushToken;
import im.rosetta.database.repository.DeviceRepository;
import im.rosetta.packet.runtime.TokenType;
import im.rosetta.service.dispatch.push.dispatchers.FCM;
import im.rosetta.service.dispatch.push.dispatchers.VoIPApns;
import im.rosetta.service.services.DeviceService;
public class PushNotifyDispatcher {
private DeviceRepository deviceRepository = new DeviceRepository();
private DeviceService deviceService = new DeviceService(deviceRepository);
private final ExecutorService executor = Executors.newFixedThreadPool(10);
private final HashMap<TokenType, Pusher> pushers = new HashMap<>() {{
put(TokenType.FCM, new FCM());
put(TokenType.VoIPApns, new VoIPApns());
}};
private Pusher findPusher(TokenType tokenType){
return this.pushers.get(tokenType);
}
private List<PushToken> findPushTokens(String publicKey) {
List<Device> devices = this.deviceService.getDevicesByPK(publicKey);
List<PushToken> pushTokens = new java.util.ArrayList<>();
for(Device device : devices){
pushTokens.addAll(device.getTokens());
}
return pushTokens;
}
/**
* Отправить уведомление пользователю с publicKey, используя все его токены для отправки уведомления, если таковые имеются
* @param publicKey публичный ключ пользователя, которому нужно отправить уведомление
* @param data данные уведомления
*/
public void sendPush(String publicKey, HashMap<String, String> data) {
executor.execute(() -> {
List<PushToken> pushTokens = this.findPushTokens(publicKey);
for(PushToken pushToken : pushTokens){
Pusher pusher = this.findPusher(pushToken.getType());
if(pusher != null){
pusher.sendPush(pushToken.getToken(), data);
}
}
});
}
/**
* Отправить уведомление пользователям с publicKeys, используя все их токены для отправки уведомления, если таковые имеются
* @param publicKeys список публичных ключей пользователей, которым нужно отправить уведомление
* @param data данные уведомления
*/
public void sendPush(List<String> publicKeys, HashMap<String, String> data) {
executor.execute(() -> {
for(String publicKey : publicKeys){
List<PushToken> pushTokens = this.findPushTokens(publicKey);
for(PushToken pushToken : pushTokens){
Pusher pusher = this.findPusher(pushToken.getType());
if(pusher != null){
pusher.sendPush(pushToken.getToken(), data);
}
}
}
});
}
}