This commit is contained in:
@@ -2,6 +2,7 @@ package im.rosetta.service.dispatch;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
@@ -11,10 +12,8 @@ import com.google.firebase.FirebaseApp;
|
||||
import com.google.firebase.FirebaseOptions;
|
||||
import com.google.firebase.messaging.ApnsConfig;
|
||||
import com.google.firebase.messaging.Aps;
|
||||
import com.google.firebase.messaging.ApsAlert;
|
||||
import com.google.firebase.messaging.FirebaseMessaging;
|
||||
import com.google.firebase.messaging.Message;
|
||||
import com.google.firebase.messaging.Notification;
|
||||
|
||||
import im.rosetta.database.repository.UserRepository;
|
||||
import im.rosetta.service.services.UserService;
|
||||
@@ -54,7 +53,29 @@ public class FirebaseDispatcher {
|
||||
}
|
||||
}
|
||||
|
||||
public void sendPushNotification(String publicKey, String title, String messageText, String senderPublicKey) {
|
||||
private Message buildMessage(String token, HashMap<String, String> data) {
|
||||
return Message.builder().putAllData(data)
|
||||
.setApnsConfig(ApnsConfig.builder()
|
||||
.setAps(Aps.builder()
|
||||
/**
|
||||
* content-available=1 будит приложение на iOS
|
||||
*/
|
||||
.setContentAvailable(true)
|
||||
.setSound("default")
|
||||
.build())
|
||||
.build())
|
||||
/**
|
||||
* Высокий приоритет чтобы разбудить приложение на Android
|
||||
*/
|
||||
.setAndroidConfig(
|
||||
com.google.firebase.messaging.AndroidConfig.builder()
|
||||
.setPriority(com.google.firebase.messaging.AndroidConfig.Priority.HIGH)
|
||||
.build())
|
||||
.setToken(token)
|
||||
.build();
|
||||
}
|
||||
|
||||
public void sendPushNotification(String publicKey, HashMap<String, String> data) {
|
||||
executor.submit(() -> {
|
||||
try {
|
||||
List<String> tokens = userService.getNotificationsTokens(publicKey);
|
||||
@@ -64,25 +85,7 @@ public class FirebaseDispatcher {
|
||||
|
||||
for (String token : tokens) {
|
||||
try {
|
||||
Message message = Message.builder()
|
||||
.setNotification(Notification.builder()
|
||||
.setTitle(title)
|
||||
.setBody(messageText)
|
||||
.build())
|
||||
.setApnsConfig(ApnsConfig.builder()
|
||||
.setAps(Aps.builder()
|
||||
.setMutableContent(true)
|
||||
.setSound("default")
|
||||
.setAlert(ApsAlert.builder()
|
||||
.setTitle(title)
|
||||
.setBody(messageText)
|
||||
.build())
|
||||
.build())
|
||||
.build())
|
||||
.putData("sender_public_key", senderPublicKey)
|
||||
.setToken(token)
|
||||
.build();
|
||||
|
||||
Message message = this.buildMessage(token, data);
|
||||
FirebaseMessaging.getInstance().send(message);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
@@ -94,45 +97,20 @@ public class FirebaseDispatcher {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Отправляет push-уведомление пользователю с данным публичным ключом (асинхронно)
|
||||
* @param publicKey публичный ключ пользователя
|
||||
* @param title заголовок уведомления
|
||||
* @param messageText текст уведомления
|
||||
*/
|
||||
public void sendPushNotification(String publicKey, String title, String messageText) {
|
||||
sendPushNotification(publicKey, title, messageText, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Отправляет push-уведомление нескольким пользователям (асинхронно)
|
||||
* @param publicKeys список публичных ключей пользователей
|
||||
* @param title заголовок уведомления
|
||||
* @param messageText текст уведомления
|
||||
* @param data данные уведомления
|
||||
*/
|
||||
public void sendPushNotification(List<String> publicKeys, String title, String messageText, String senderPublicKey) {
|
||||
public void sendPushNotification(List<String> publicKeys, HashMap<String, String> data) {
|
||||
executor.submit(() -> {
|
||||
for (String publicKey : publicKeys) {
|
||||
sendPushNotificationSync(publicKey, title, messageText, senderPublicKey);
|
||||
sendPushNotificationSync(publicKey, data);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Отправляет push-уведомление нескольким пользователям (асинхронно)
|
||||
* @param publicKeys список публичных ключей пользователей
|
||||
* @param title заголовок уведомления
|
||||
* @param messageText текст уведомления
|
||||
*/
|
||||
public void sendPushNotification(List<String> publicKeys, String title, String messageText) {
|
||||
executor.submit(() -> {
|
||||
for (String publicKey : publicKeys) {
|
||||
sendPushNotificationSync(publicKey, title, messageText, null);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void sendPushNotificationSync(String publicKey, String title, String messageText, String senderPublicKey) {
|
||||
private void sendPushNotificationSync(String publicKey, HashMap<String, String> data) {
|
||||
try {
|
||||
List<String> tokens = userService.getNotificationsTokens(publicKey);
|
||||
if (tokens == null || tokens.isEmpty()) {
|
||||
@@ -141,24 +119,7 @@ public class FirebaseDispatcher {
|
||||
|
||||
for (String token : tokens) {
|
||||
try {
|
||||
Message message = Message.builder()
|
||||
.setNotification(Notification.builder()
|
||||
.setTitle(title)
|
||||
.setBody(messageText)
|
||||
.build())
|
||||
.setApnsConfig(ApnsConfig.builder()
|
||||
.setAps(Aps.builder()
|
||||
.setMutableContent(true)
|
||||
.setSound("default")
|
||||
.setAlert(ApsAlert.builder()
|
||||
.setTitle(title)
|
||||
.setBody(messageText)
|
||||
.build())
|
||||
.build())
|
||||
.build())
|
||||
.putData("sender_public_key", senderPublicKey != null ? senderPublicKey : "")
|
||||
.setToken(token)
|
||||
.build();
|
||||
Message message = this.buildMessage(token, data);
|
||||
|
||||
FirebaseMessaging.getInstance().send(message);
|
||||
} catch (Exception e) {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package im.rosetta.service.dispatch;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import im.rosetta.client.ClientManager;
|
||||
@@ -98,14 +99,26 @@ public class MessageDispatcher {
|
||||
}
|
||||
if(packet instanceof Packet7Read){
|
||||
/**
|
||||
* Если это пакет прочтения, то не отправляем пуш уведомление, так как это может привести к спаму пушами при чтении сообщений
|
||||
* Если это пакет прочтения, то отправляем тихий пуш, что диалог прочитан, отправляем тому, кто читает диалог, чтобы
|
||||
* клиент мог очистить пуши для этого диалога
|
||||
*/
|
||||
this.firebaseDispatcher.sendPushNotification(fromPublicKey, new HashMap<>(){
|
||||
{
|
||||
put("type", "read");
|
||||
put("dialog", toPublicKey);
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
/**
|
||||
* Отправляем PUSH уведомление
|
||||
*/
|
||||
this.firebaseDispatcher.sendPushNotification(groupMembersPublicKeys, "Rosetta", "New message in group", toPublicKey.replace("#group:", ""));
|
||||
this.firebaseDispatcher.sendPushNotification(groupMembersPublicKeys, new HashMap<>(){
|
||||
{
|
||||
put("type", "group_message");
|
||||
put("from", toPublicKey.replace("#group:", ""));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -156,15 +169,26 @@ public class MessageDispatcher {
|
||||
}
|
||||
if(packet instanceof Packet7Read){
|
||||
/**
|
||||
* Если это пакет прочтения, то не отправляем пуш уведомление,
|
||||
* так как это может привести к спаму пушами при чтении сообщений
|
||||
* Если это пакет прочтения, то отправляем тихий пуш, что диалог прочитан, отправляем тому, кто читает диалог, чтобы
|
||||
* клиент мог очистить пуши для этого диалога
|
||||
*/
|
||||
this.firebaseDispatcher.sendPushNotification(fromPublicKey, new HashMap<>(){
|
||||
{
|
||||
put("type", "read");
|
||||
put("dialog", toPublicKey);
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
/**
|
||||
* Отправляем PUSH уведомление получателю
|
||||
*/
|
||||
this.firebaseDispatcher.sendPushNotification(toPublicKey, user.getTitle(), "New message", fromPublicKey);
|
||||
this.firebaseDispatcher.sendPushNotification(toPublicKey, new HashMap<>(){
|
||||
{
|
||||
put("type", "personal_message");
|
||||
put("from", fromPublicKey);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user