Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Push Notification GA release #1335

Merged
merged 10 commits into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions sdk/communication/AzureCommunicationChat/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Release History

## 1.3.0 (2022-09-13)
### New Features
- `ChatClient` now supports Push Notifications for Chat events
- Following methods added to `ChatClient`:
- `startPushNotifications(deviceToken:)`
- `stopPushNotifications()`
- Added the prototol `PushNotificationKeyStorage` and the class `AppGroupPushNotificationKeyStorage` to support PushNotification Encryption Key Management

## 1.3.0-beta.1 (2022-07-25)
### New Features
- `ChatClient` now supports Push Notifications for Chat events
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class ChatClient {
private var realTimeNotificationDisconnectedHandler: TrouterEventHandler?
private var pushNotificationClient: PushNotificationClient?
internal var registrationId: String
public weak var pushNotificationKeyHandler: PushNotificationKeyHandler?
public weak var pushNotificationKeyStorage: PushNotificationKeyStorage?

// MARK: Initializers

Expand Down Expand Up @@ -405,11 +405,11 @@ public class ChatClient {
let encryptionKey: String

// Persist the key if the Contoso intends to implement encryption
if pushNotificationKeyHandler != nil {
if pushNotificationKeyStorage != nil {
// Persist the key if the Contoso intends to implement encryption
encryptionKey = generateEncryptionKey()
do {
try pushNotificationKeyHandler?.onPersistKey(
try pushNotificationKeyStorage?.onPersistKey(
encryptionKey,
expiryTime: Date(timeIntervalSinceNow: 45 * 60)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
//
// --------------------------------------------------------------------------

import AzureCommunicationCommon
import AzureCore
import Foundation
import Trouter

Expand Down Expand Up @@ -68,3 +70,66 @@ func isGcch(id: String) -> Bool {
let gcchAcsUserPrefix = "gcch-acs:"
return (id.starts(with: gcchTeamsUserPrefix) || id.starts(with: gcchAcsUserPrefix))
}

/// Parses out the id/phone number portion of a user id.
/// - Parameters:
/// - id: The string id.
/// - prefix: The id prefix.
/// - Returns: The part of the id after the prefix that corresponds to the user id or phone number of a user.
private func parse(id: String, prefix: String) -> String {
let index = id.index(id.startIndex, offsetBy: prefix.count)
return String(id.suffix(from: index))
}

/// Constructs a CommunicationIdentifier from a string id.
/// - Parameter id: The string id.
/// - Returns: The CommunicationIdentifier.
internal func getIdentifier(from id: String) -> CommunicationIdentifier {
let publicTeamsUserPrefix = "8:orgid:"
let dodTeamsUserPrefix = "8:dod:"
let gcchTeamsUserPrefix = "8:gcch:"
let teamsVisitorUserPrefix = "8:teamsvisitor:"
let phoneNumberPrefix = "4:"
let acsUserPrefix = "8:acs:"
let spoolUserPrefix = "8:spool:"
let dodAcsUserPrefix = "8:dod-acs:"
let gcchAcsUserPrefix = "8:gcch-acs:"

if id.starts(with: publicTeamsUserPrefix) {
return MicrosoftTeamsUserIdentifier(
userId: parse(id: id, prefix: publicTeamsUserPrefix),
isAnonymous: false,
rawId: id,
cloudEnvironment: CommunicationCloudEnvironment.Public
)
} else if id.starts(with: dodTeamsUserPrefix) {
return MicrosoftTeamsUserIdentifier(
userId: parse(id: id, prefix: dodTeamsUserPrefix),
isAnonymous: false,
rawId: id,
cloudEnvironment: CommunicationCloudEnvironment.Dod
)
} else if id.starts(with: gcchTeamsUserPrefix) {
return MicrosoftTeamsUserIdentifier(
userId: parse(id: id, prefix: gcchTeamsUserPrefix),
isAnonymous: false,
rawId: id,
cloudEnvironment: CommunicationCloudEnvironment.Gcch
)
} else if id.starts(with: teamsVisitorUserPrefix) {
return MicrosoftTeamsUserIdentifier(
userId: parse(id: id, prefix: teamsVisitorUserPrefix),
isAnonymous: true
)
} else if id.starts(with: phoneNumberPrefix) {
return PhoneNumberIdentifier(
phoneNumber: parse(id: id, prefix: phoneNumberPrefix),
rawId: id
)
} else if id.starts(with: acsUserPrefix) || id.starts(with: spoolUserPrefix) || id
.starts(with: dodAcsUserPrefix) || id.starts(with: gcchAcsUserPrefix) {
return CommunicationUserIdentifier(id)
} else {
return UnknownIdentifier(id)
}
}
angellan-msft marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@
import AzureCore
import Foundation

public class AppGroupPushNotificationKeyHandler: PushNotificationKeyHandler {
public class AppGroupPushNotificationKeyStorage: PushNotificationKeyStorage {
var sharedDefault: UserDefaults
var keyTag: String

public init?(appGroupId: String, keyTag: String) throws {
guard let shareDefault = UserDefaults(suiteName: appGroupId) else {
throw AzureError
.client(
"Failed to init AppGroupPushNotificationKeyHandler. The UserDefaults doesn't exist for the app group ID."
"Failed to init AppGroupPushNotificationKeyStorage. The UserDefaults doesn't exist for the app group ID."
)
}
self.sharedDefault = shareDefault
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public class PushNotificationChatMessageEvent {
/// Chat thread id.
public var threadId: String
/// Sender Id
public var senderId: String
public var sender: CommunicationIdentifier
/// Recipient Id
public var recipientId: String
public var recipient: CommunicationIdentifier
/// Sender display name.
public var senderDisplayName: String?
/// The timestamp when the message arrived at the server. The timestamp is in RFC3339 format: `yyyy-MM-ddTHH:mm:ssZ`.
Expand All @@ -66,17 +66,17 @@ public class PushNotificationChatMessageEvent {
messageId: String,
type: ChatMessageType,
threadId: String,
senderId: String,
recipientId: String,
sender: CommunicationIdentifier,
recipient: CommunicationIdentifier,
senderDisplayName: String? = nil,
originalArrivalTime: Iso8601Date? = nil,
version: String
) {
self.messageId = messageId
self.type = type
self.threadId = threadId
self.senderId = senderId
self.recipientId = recipientId
self.sender = sender
self.recipient = recipient
self.senderDisplayName = senderDisplayName
self.originalArrivalTime = originalArrivalTime
self.version = version
Expand Down Expand Up @@ -113,8 +113,8 @@ public class PushNotificationChatMessageReceivedEvent: PushNotificationChatMessa
messageId: pushNotificationMessageReceivedPayload.messageId,
type: ChatMessageType(pushNotificationMessageReceivedPayload.messageType),
threadId: pushNotificationMessageReceivedPayload.groupId,
senderId: pushNotificationMessageReceivedPayload.senderId,
recipientId: pushNotificationMessageReceivedPayload.recipientId,
sender: getIdentifier(from: pushNotificationMessageReceivedPayload.senderId),
recipient: getIdentifier(from: pushNotificationMessageReceivedPayload.recipientId),
senderDisplayName: pushNotificationMessageReceivedPayload.senderDisplayName,
originalArrivalTime: Iso8601Date(string: pushNotificationMessageReceivedPayload.originalArrivalTime),
version: pushNotificationMessageReceivedPayload.version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ import AzureCore
import Foundation

/*
PushNotificationKeyHandler is used to manage encryption keys which are used to encrypt and decrypt the notification payload.
If you want to implement the advanced version of Push Notification and customize the content in alert banner, please create a concrete class which conform to this protocol. Before you call chatClient.startPushNotifications(), please create a concrete key handler and assign it to chatClient.pushNotificationKeyHandler.
Chat SDK provides a default implementation AppGroup PushNotificationKeyHandler for you to use. You can also create your own if you want.
PushNotificationKeyStorage is used to manage encryption keys which are used to encrypt and decrypt the notification payload.
If you want to implement the advanced version of Push Notification and customize the content in alert banner, please create a concrete class which conform to this protocol. Before you call chatClient.startPushNotifications(), please create a concrete key handler and assign it to chatClient.pushNotificationKeyStorage.
Chat SDK provides a default implementation AppGroup PushNotificationKeyStorage for you to use. You can also create your own if you want.
*/
public protocol PushNotificationKeyHandler: AnyObject {
public protocol PushNotificationKeyStorage: AnyObject {
/// Persist the encryption key in local persistent storage.
/// - Parameters:
/// - encryptionKey: a string which contains two keys. The first half is the key for authorization and the second half is the key for encryption. It is generated by chat sdk.
Expand All @@ -43,7 +43,7 @@ public protocol PushNotificationKeyHandler: AnyObject {
func onRetrieveKeys() throws -> [String]
}

extension PushNotificationKeyHandler {
extension PushNotificationKeyStorage {
/// This method is used to decrypt notification payload in notification service extension.
/// - Parameters:
/// - notification: The APNS push notification payload ( including "aps" and "data" )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ public class ChatMessageReceivedEvent: BaseChatMessageEvent {

super.init(
threadId: messageReceivedPayload.groupId,
sender: TrouterEventUtil.getIdentifier(from: messageReceivedPayload.senderId),
recipient: TrouterEventUtil.getIdentifier(from: messageReceivedPayload.recipientMri),
sender: getIdentifier(from: messageReceivedPayload.senderId),
recipient: getIdentifier(from: messageReceivedPayload.recipientMri),
id: messageReceivedPayload.messageId,
senderDisplayName: messageReceivedPayload.senderDisplayName,
createdOn: Iso8601Date(string: messageReceivedPayload.originalArrivalTime),
Expand Down Expand Up @@ -320,8 +320,8 @@ public class ChatMessageEditedEvent: BaseChatMessageEvent {

super.init(
threadId: chatMessageEditedPayload.groupId,
sender: TrouterEventUtil.getIdentifier(from: chatMessageEditedPayload.senderId),
recipient: TrouterEventUtil.getIdentifier(from: chatMessageEditedPayload.recipientMri),
sender: getIdentifier(from: chatMessageEditedPayload.senderId),
recipient: getIdentifier(from: chatMessageEditedPayload.recipientMri),
id: chatMessageEditedPayload.messageId,
senderDisplayName: chatMessageEditedPayload.senderDisplayName,
createdOn: Iso8601Date(string: chatMessageEditedPayload.originalArrivalTime),
Expand Down Expand Up @@ -388,8 +388,8 @@ public class ChatMessageDeletedEvent: BaseChatMessageEvent {
self.deletedOn = Iso8601Date(string: chatMessageDeletedPayload.deletetime)
super.init(
threadId: chatMessageDeletedPayload.groupId,
sender: TrouterEventUtil.getIdentifier(from: chatMessageDeletedPayload.senderId),
recipient: TrouterEventUtil.getIdentifier(from: chatMessageDeletedPayload.recipientMri),
sender: getIdentifier(from: chatMessageDeletedPayload.senderId),
recipient: getIdentifier(from: chatMessageDeletedPayload.recipientMri),
id: chatMessageDeletedPayload.messageId,
senderDisplayName: chatMessageDeletedPayload.senderDisplayName,
createdOn: Iso8601Date(string: chatMessageDeletedPayload.originalArrivalTime),
Expand Down Expand Up @@ -450,8 +450,8 @@ public class TypingIndicatorReceivedEvent: BaseChatEvent {
self.senderDisplayName = typingIndicatorReceivedPayload.senderDisplayName
super.init(
threadId: typingIndicatorReceivedPayload.groupId,
sender: TrouterEventUtil.getIdentifier(from: typingIndicatorReceivedPayload.senderId),
recipient: TrouterEventUtil.getIdentifier(from: typingIndicatorReceivedPayload.recipientMri)
sender: getIdentifier(from: typingIndicatorReceivedPayload.senderId),
recipient: getIdentifier(from: typingIndicatorReceivedPayload.recipientMri)
)
}
}
Expand Down Expand Up @@ -517,8 +517,8 @@ public class ReadReceiptReceivedEvent: BaseChatEvent {
self.readOn = Iso8601Date(readOnDate)
super.init(
threadId: readReceiptReceivedPayload.groupId,
sender: TrouterEventUtil.getIdentifier(from: readReceiptReceivedPayload.senderId),
recipient: TrouterEventUtil.getIdentifier(from: readReceiptReceivedPayload.recipientMri)
sender: getIdentifier(from: readReceiptReceivedPayload.senderId),
recipient: getIdentifier(from: readReceiptReceivedPayload.recipientMri)
)
}
}
Expand Down Expand Up @@ -582,7 +582,7 @@ public class ChatThreadCreatedEvent: BaseChatThreadEvent {
.decode(ChatParticipantPayload.self, from: createdByJsonData)
let createdBy =
SignalingChatParticipant(
id: TrouterEventUtil.getIdentifier(from: createdByPayload.participantId),
id: getIdentifier(from: createdByPayload.participantId),
displayName: createdByPayload.displayName
)

Expand All @@ -595,7 +595,7 @@ public class ChatThreadCreatedEvent: BaseChatThreadEvent {
let participants: [SignalingChatParticipant] = membersPayload
.map { (memberPayload: ChatParticipantPayload) -> SignalingChatParticipant in
SignalingChatParticipant(
id: TrouterEventUtil.getIdentifier(from: memberPayload.participantId),
id: getIdentifier(from: memberPayload.participantId),
displayName: memberPayload.displayName
)
}
Expand Down Expand Up @@ -670,7 +670,7 @@ public class ChatThreadPropertiesUpdatedEvent: BaseChatThreadEvent {
.decode(ChatParticipantPayload.self, from: updatedByJsonData)
let updatedBy =
SignalingChatParticipant(
id: TrouterEventUtil.getIdentifier(from: updatedByPayload.participantId),
id: getIdentifier(from: updatedByPayload.participantId),
displayName: updatedByPayload.displayName
)

Expand Down Expand Up @@ -737,7 +737,7 @@ public class ChatThreadDeletedEvent: BaseChatThreadEvent {
let deletedByPayload: ChatParticipantPayload = try JSONDecoder()
.decode(ChatParticipantPayload.self, from: deletedByJsonData)
let deletedBy = SignalingChatParticipant(
id: TrouterEventUtil.getIdentifier(from: deletedByPayload.participantId),
id: getIdentifier(from: deletedByPayload.participantId),
displayName: deletedByPayload.displayName
)

Expand Down Expand Up @@ -800,7 +800,7 @@ public class ParticipantsAddedEvent: BaseChatThreadEvent {
let addedByPayload: ChatParticipantPayload = try JSONDecoder()
.decode(ChatParticipantPayload.self, from: addeddByJsonData)
let addedBy = SignalingChatParticipant(
id: TrouterEventUtil.getIdentifier(from: addedByPayload.participantId),
id: getIdentifier(from: addedByPayload.participantId),
displayName: addedByPayload.displayName
)

Expand All @@ -814,7 +814,7 @@ public class ParticipantsAddedEvent: BaseChatThreadEvent {
let participants: [SignalingChatParticipant] = participantsPayload
.map { (memberPayload: ChatParticipantPayload) -> SignalingChatParticipant in
SignalingChatParticipant(
id: TrouterEventUtil.getIdentifier(from: memberPayload.participantId),
id: getIdentifier(from: memberPayload.participantId),
displayName: memberPayload.displayName,
shareHistoryTime: Iso8601Date(
string: TrouterEventUtil
Expand Down Expand Up @@ -883,7 +883,7 @@ public class ParticipantsRemovedEvent: BaseChatThreadEvent {
let removedByPayload: ChatParticipantPayload = try JSONDecoder()
.decode(ChatParticipantPayload.self, from: removedByJsonData)
let removedBy = SignalingChatParticipant(
id: TrouterEventUtil.getIdentifier(from: removedByPayload.participantId),
id: getIdentifier(from: removedByPayload.participantId),
displayName: removedByPayload.displayName
)

Expand All @@ -896,7 +896,7 @@ public class ParticipantsRemovedEvent: BaseChatThreadEvent {
let participants: [SignalingChatParticipant] = participantsPayload
.map { (memberPayload: ChatParticipantPayload) -> SignalingChatParticipant in
SignalingChatParticipant(
id: TrouterEventUtil.getIdentifier(from: memberPayload.participantId),
id: getIdentifier(from: memberPayload.participantId),
displayName: memberPayload.displayName,
shareHistoryTime: Iso8601Date(
string: TrouterEventUtil
Expand Down
Loading