use of com.azure.android.communication.common.CommunicationUserIdentifier in project azure-sdk-for-android by Azure.
the class MainActivity method createChatThreadAsyncClient.
public void createChatThreadAsyncClient() {
if (chatAsyncClient == null) {
createChatAsyncClient();
}
// A list of participants to start the thread with.
List<ChatParticipant> participants = new ArrayList<>();
// The display name for the thread participant.
String displayName = "First participant";
participants.add(new ChatParticipant().setCommunicationIdentifier(new CommunicationUserIdentifier(firstUserId)).setDisplayName(displayName));
// The topic for the thread.
final String topic = "General";
// Optional, set a repeat request ID.
final String repeatabilityRequestID = "";
// Options to pass to the create method.
CreateChatThreadOptions createChatThreadOptions = new CreateChatThreadOptions().setTopic(topic).setParticipants(participants).setIdempotencyToken(repeatabilityRequestID);
try {
CreateChatThreadResult createChatThreadResult = chatAsyncClient.createChatThread(createChatThreadOptions).get();
ChatThreadProperties chatThreadProperties = createChatThreadResult.getChatThreadProperties();
threadId = chatThreadProperties.getId();
logAndToast("Created thread with ID: " + threadId);
chatThreadAsyncClient = chatAsyncClient.getChatThreadClient(threadId);
Log.d(TAG, "Created ChatThreadAsyncClient");
} catch (InterruptedException | ExecutionException e) {
Log.e("ChatThreadAsyncClient creation failed", Objects.requireNonNull(e.getMessage()));
}
}
use of com.azure.android.communication.common.CommunicationUserIdentifier in project azure-sdk-for-android by Azure.
the class MainActivity method registerPushNotificationListener.
public void registerPushNotificationListener(View view) {
logAndToast("Register push notification listeners");
try {
chatAsyncClient.addPushNotificationHandler(CHAT_MESSAGE_RECEIVED, (ChatEvent payload) -> {
Log.i(TAG, "Push Notification CHAT_MESSAGE_RECEIVED.");
ChatMessageReceivedEvent event = (ChatMessageReceivedEvent) payload;
Log.i(TAG, "Message received!" + " ThreadId: " + event.getChatThreadId() + " MessageId: " + event.getId() + " Content: " + event.getContent() + " Priority: " + event.getPriority() + " SenderDisplayName: " + event.getSenderDisplayName() + " SenderMri: " + ((CommunicationUserIdentifier) event.getSender()).getId() + " Version: " + event.getVersion() + " CreatedOn: " + event.getCreatedOn() + " Type: " + event.getType() + " RecipientMri: " + ((CommunicationUserIdentifier) event.getRecipient()).getId());
});
chatAsyncClient.addPushNotificationHandler(CHAT_MESSAGE_EDITED, (ChatEvent payload) -> {
Log.i(TAG, "Push Notification CHAT_MESSAGE_EDITED.");
ChatMessageEditedEvent event = (ChatMessageEditedEvent) payload;
Log.i(TAG, "Message edited!" + " ThreadId: " + event.getChatThreadId() + " MessageId: " + event.getId() + " Content: " + event.getContent() + " SenderDisplayName: " + event.getSenderDisplayName() + " SenderMri: " + ((CommunicationUserIdentifier) event.getSender()).getId() + " Version: " + event.getVersion() + " CreatedOn: " + event.getCreatedOn() + " EditedOn: " + event.getEditedOn() + " RecipientMri: " + ((CommunicationUserIdentifier) event.getRecipient()).getId());
});
chatAsyncClient.addPushNotificationHandler(CHAT_MESSAGE_DELETED, (ChatEvent payload) -> {
Log.i(TAG, "Push Notification CHAT_MESSAGE_DELETED.");
ChatMessageDeletedEvent event = (ChatMessageDeletedEvent) payload;
Log.i(TAG, "Message deleted!" + " ThreadId: " + event.getChatThreadId() + " MessageId: " + event.getId() + " SenderDisplayName: " + event.getSenderDisplayName() + " SenderMri: " + ((CommunicationUserIdentifier) event.getSender()).getId() + " Version: " + event.getVersion() + " CreatedOn: " + event.getCreatedOn() + " DeletedOn: " + event.getDeletedOn() + " RecipientMri: " + ((CommunicationUserIdentifier) event.getRecipient()).getId());
});
chatAsyncClient.addPushNotificationHandler(CHAT_THREAD_CREATED, (ChatEvent payload) -> {
Log.i(TAG, "Push Notification CHAT_THREAD_CREATED.");
ChatThreadCreatedEvent event = (ChatThreadCreatedEvent) payload;
Log.i(TAG, "Thread Created!" + " ThreadId: " + event.getChatThreadId() + " Properties_Id: " + event.getProperties().getId() + " Properties_Topic: " + event.getProperties().getTopic() + " Properties_CreatedOn: " + event.getProperties().getCreatedOn() + " Properties_CreatedByMri: " + ((CommunicationUserIdentifier) event.getProperties().getCreatedByCommunicationIdentifier()).getId() + " Participants_size: " + event.getParticipants().size() + " Version: " + event.getVersion() + " CreatedOn: " + event.getCreatedOn() + " CreatedBy_DisplayName: " + event.getCreatedBy().getDisplayName() + " CreatedBy_Mri: " + ((CommunicationUserIdentifier) event.getCreatedBy().getCommunicationIdentifier()).getId());
});
chatAsyncClient.addPushNotificationHandler(CHAT_THREAD_PROPERTIES_UPDATED, (ChatEvent payload) -> {
Log.i(TAG, "Push Notification CHAT_THREAD_PROPERTIES_UPDATED.");
ChatThreadPropertiesUpdatedEvent event = (ChatThreadPropertiesUpdatedEvent) payload;
Log.i(TAG, "Thread Updated!" + " ThreadId: " + event.getChatThreadId() + " Properties_Id: " + event.getProperties().getId() + " Properties_Topic: " + event.getProperties().getTopic() + " Version: " + event.getVersion() + " UpdatedOn: " + event.getUpdatedOn() + " UpdatedBy_DisplayName: " + event.getUpdatedBy().getDisplayName() + " UpdatedBy_Mri: " + ((CommunicationUserIdentifier) event.getUpdatedBy().getCommunicationIdentifier()).getId());
});
chatAsyncClient.addPushNotificationHandler(CHAT_THREAD_DELETED, (ChatEvent payload) -> {
Log.i(TAG, "Push Notification CHAT_THREAD_DELETED.");
ChatThreadDeletedEvent event = (ChatThreadDeletedEvent) payload;
Log.i(TAG, "Thread Deleted!" + " ThreadId: " + event.getChatThreadId() + " Version: " + event.getVersion() + " DeletedOn: " + event.getDeletedOn() + " DeletedBy_DisplayName: " + event.getDeletedBy().getDisplayName() + " DeletedBy_Mri: " + ((CommunicationUserIdentifier) event.getDeletedBy().getCommunicationIdentifier()).getId());
});
chatAsyncClient.addPushNotificationHandler(PARTICIPANTS_ADDED, (ChatEvent payload) -> {
Log.i(TAG, "Push Notification PARTICIPANTS_ADDED.");
ParticipantsAddedEvent event = (ParticipantsAddedEvent) payload;
Log.i(TAG, "Participant Added!" + " ThreadId: " + event.getChatThreadId() + " ParticipantsAdded_size: " + event.getParticipantsAdded().size() + " Version: " + event.getVersion() + " AddedOn: " + event.getAddedOn() + " AddedBy_DisplayName: " + event.getAddedBy().getDisplayName() + " AddedBy_Mri: " + ((CommunicationUserIdentifier) event.getAddedBy().getCommunicationIdentifier()).getId());
});
chatAsyncClient.addPushNotificationHandler(PARTICIPANTS_REMOVED, (ChatEvent payload) -> {
Log.i(TAG, "Push Notification PARTICIPANTS_REMOVED.");
ParticipantsRemovedEvent event = (ParticipantsRemovedEvent) payload;
Log.i(TAG, "Participant Removed!" + " ThreadId: " + event.getChatThreadId() + " ParticipantsRemoved_size: " + event.getParticipantsRemoved().size() + " Version: " + event.getVersion() + " RemovedOn: " + event.getRemovedOn() + " RemovedBy_DisplayName: " + event.getRemovedBy().getDisplayName() + " RemovedBy_Mri: " + ((CommunicationUserIdentifier) event.getRemovedBy().getCommunicationIdentifier()).getId());
});
} catch (IllegalStateException error) {
Log.i(TAG, "Push Notification not start yet.");
}
}
use of com.azure.android.communication.common.CommunicationUserIdentifier in project azure-sdk-for-android by Azure.
the class MainActivity method listReadReceipts.
public void listReadReceipts(View view) {
if (chatThreadAsyncClient == null) {
createChatThreadAsyncClient();
}
if (chatThreadAsyncClient != null) {
// The maximum number of read receipts to be returned per page, optional.
int maxPageSize = 10;
// Skips participants up to a specified position in response.
int skip = 0;
// Options to pass to the list method.
ListReadReceiptOptions listReadReceiptOptions = new ListReadReceiptOptions().setMaxPageSize(maxPageSize).setSkip(skip);
try {
PagedAsyncStream<ChatMessageReadReceipt> readReceipts = chatThreadAsyncClient.listReadReceipts(listReadReceiptOptions, RequestContext.NONE);
readReceipts.forEach(readReceipt -> {
Log.d(TAG, "Got receipt for participant " + ((CommunicationUserIdentifier) readReceipt.getSenderCommunicationIdentifier()).getId() + " for message with id: " + readReceipt.getChatMessageId());
});
} catch (Exception e) {
logAndToast("Listing read receipts failed: " + e.getMessage());
}
} else {
logAndToast("ChatThreadAsyncClient creation failed");
}
}
use of com.azure.android.communication.common.CommunicationUserIdentifier in project azure-sdk-for-android by Azure.
the class CommunicationIdentifierConverter method convert.
/**
* Convert CommunicationIdentifierModel into CommunicationIdentifier
* @param identifier CommunicationIdentifierModel to be converted
* @return CommunicationIdentifier
*/
public static CommunicationIdentifier convert(CommunicationIdentifierModel identifier, ClientLogger logger) {
assertSingleType(identifier);
String rawId = identifier.getRawId();
if (identifier.getCommunicationUser() != null) {
final String userId = identifier.getCommunicationUser().getId();
if (userId == null) {
throw logger.logExceptionAsError(new NullPointerException("CommunicationIdentifierModel.CommunicationUserIdentifierModel.id"));
}
return new CommunicationUserIdentifier(userId);
}
if (identifier.getPhoneNumber() != null) {
PhoneNumberIdentifierModel phoneNumberModel = identifier.getPhoneNumber();
if (phoneNumberModel.getValue() == null) {
throw logger.logExceptionAsError(new NullPointerException("CommunicationIdentifierModel.PhoneNumberIdentifierModel.value"));
}
return new PhoneNumberIdentifier(phoneNumberModel.getValue()).setRawId(rawId);
}
if (identifier.getMicrosoftTeamsUser() != null) {
MicrosoftTeamsUserIdentifierModel teamsUserIdentifierModel = identifier.getMicrosoftTeamsUser();
final String userId = teamsUserIdentifierModel.getUserId();
if (userId == null) {
throw logger.logExceptionAsError(new NullPointerException("CommunicationIdentifierModel.MicrosoftTeamsUserIdentifierModel.userId"));
}
final CommunicationCloudEnvironmentModel cloud = teamsUserIdentifierModel.getCloud();
if (cloud == null) {
throw logger.logExceptionAsError(new NullPointerException("CommunicationIdentifierModel.MicrosoftTeamsUserIdentifierModel.cloud"));
}
if (rawId == null) {
throw logger.logExceptionAsError(new NullPointerException("CommunicationIdentifierModel.rawId"));
}
return new MicrosoftTeamsUserIdentifier(userId, teamsUserIdentifierModel.isAnonymous()).setRawId(rawId).setCloudEnvironment(CommunicationCloudEnvironment.fromString(cloud.toString()));
}
if (rawId == null) {
throw logger.logExceptionAsError(new NullPointerException("CommunicationIdentifierModel.rawId"));
}
return new UnknownIdentifier(rawId);
}
use of com.azure.android.communication.common.CommunicationUserIdentifier in project azure-sdk-for-android by Azure.
the class ChatThreadAsyncClientTest method cannotRemoveParticipantWithInvalidUser.
@ParameterizedTest
@MethodSource("com.azure.android.core.test.TestBase#getHttpClients")
public void cannotRemoveParticipantWithInvalidUser(HttpClient httpClient) throws ExecutionException, InterruptedException {
setupTest(httpClient);
ExecutionException executionException = assertThrows(ExecutionException.class, () -> {
this.chatThreadClient.removeParticipant(new CommunicationUserIdentifier("8:acs:invalidUserId")).get();
});
Throwable cause = executionException.getCause();
assertNotNull(cause);
assertTrue(cause instanceof ChatErrorResponseException);
ChatErrorResponseException exception = (ChatErrorResponseException) cause;
assertNotNull(exception.getResponse());
assertEquals(400, exception.getResponse().getStatusCode());
}
Aggregations