-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added notification service + update mock. Currently notifications are…
… working only in mock mode
- Loading branch information
Showing
25 changed files
with
608 additions
and
79 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
backend-model/src/main/java/com/chariot/backend/model/ChannelNotification.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.chariot.backend.model; | ||
|
||
import java.util.HashMap; | ||
|
||
public class ChannelNotification { | ||
private final String title; | ||
private final String body; | ||
private final HashMap<String, Object> data; | ||
|
||
public ChannelNotification(String title, String body, HashMap<String, Object> data) { | ||
this.title = title; | ||
this.body = body; | ||
this.data = data; | ||
} | ||
|
||
public ChannelNotification() { | ||
this("", "", new HashMap<>()); | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public String getBody() { | ||
return body; | ||
} | ||
|
||
public HashMap<String, Object> getData() { | ||
return data; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
.../src/main/java/com/chariot/backend/schema/notification/NotificationRegistrationToken.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.chariot.backend.schema.notification; | ||
|
||
public class NotificationRegistrationToken { | ||
private String token; | ||
private String username; | ||
|
||
public NotificationRegistrationToken(String token, String username) { | ||
this.token = token; | ||
this.username = username; | ||
} | ||
|
||
public NotificationRegistrationToken() { | ||
} | ||
|
||
public String getToken() { | ||
return token; | ||
} | ||
|
||
public void setToken(String token) { | ||
this.token = token; | ||
} | ||
|
||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
public void setUsername(String username) { | ||
this.username = username; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
backend-utils/src/main/java/com/chariot/backend/utils/data/FloatingPointComparator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.chariot.backend.utils.data; | ||
|
||
public class FloatingPointComparator { | ||
public static double PRECISION_LEVEL = 0.00000000000001; | ||
|
||
public boolean isWithinBounds(double value, double upperBound, double lowerBound) { | ||
return leftIsGreaterThanRight(upperBound, value) && | ||
leftIsGreaterThanRight(value, lowerBound); | ||
} | ||
|
||
public boolean leftIsGreaterThanRight(double left, double right) { | ||
return Math.abs(left - right) > PRECISION_LEVEL; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
.../src/main/java/com/chariot/backend/gateway/webapp/rest/impl/mock/NotificationMockApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.chariot.backend.gateway.webapp.rest.impl.mock; | ||
|
||
import com.chariot.backend.model.ChannelNotification; | ||
import com.chariot.backend.schema.notification.NotificationRegistrationToken; | ||
import com.kinoroy.expo.push.*; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.io.IOException; | ||
import java.util.*; | ||
|
||
@Service | ||
public class NotificationMockApi { | ||
private Map<String, String> tokens = new HashMap<>(); | ||
|
||
public void registerToken(NotificationRegistrationToken notificationRegistrationToken) { | ||
tokens.put(notificationRegistrationToken.getUsername(), notificationRegistrationToken.getToken()); | ||
} | ||
|
||
public void pushNotification(ChannelNotification channelNotification) throws Throwable { | ||
Collection<String> somePushTokens = tokens.values(); | ||
List<Message> messages = new ArrayList<>(); | ||
for (String token : somePushTokens) { | ||
if (!ExpoPushClient.isExpoPushToken(token)) { | ||
System.out.println(token + " is not a valid Expo Push Token!"); | ||
} | ||
} | ||
for (String token : somePushTokens) { | ||
|
||
HashMap<String, Object> data = new HashMap<>(); | ||
data.put("key", "value"); | ||
|
||
|
||
Message message = new Message.Builder() | ||
.to(token) | ||
.title(channelNotification.getTitle()) | ||
.body(channelNotification.getBody()) | ||
.data(channelNotification.getData()) | ||
.build(); | ||
messages.add(message); | ||
} | ||
|
||
List<List<Message>> chunks = ExpoPushClient.chunkItems(messages); | ||
|
||
for (List<Message> chunk : chunks) { | ||
try { | ||
PushTicketResponse response = ExpoPushClient.sendPushNotifications(messages); | ||
List<ExpoError> errors = response.getErrors(); | ||
if (errors != null) { | ||
for (ExpoError error : errors) { | ||
// Handle the errors | ||
} | ||
} | ||
List<PushTicket> tickets = response.getTickets(); | ||
if (tickets != null) { | ||
for (PushTicket ticket : tickets) { | ||
if (ticket.getStatus() == Status.OK) { | ||
String id = ticket.getId(); | ||
} else { | ||
PushError e = ticket.getDetails().getError(); | ||
switch (e) { | ||
case MESSAGE_TOO_BIG: | ||
case INVALID_CREDENTIALS: | ||
case DEVICE_NOT_REGISTERED: | ||
case MESSAGE_RATE_EXCEEDED: | ||
} | ||
|
||
} | ||
} | ||
} | ||
} catch (IOException e) { | ||
System.out.println(e.getMessage()); | ||
} | ||
} | ||
|
||
} | ||
|
||
|
||
} |
Oops, something went wrong.