Skip to content

Commit 0f7659b

Browse files
authored
Merge pull request #206987 from angellan-msft/main
Enable AzureCommunicationChat Push Notification in IOS SDK
2 parents 1d8a445 + b5eca2f commit 0f7659b

File tree

19 files changed

+168
-6
lines changed

19 files changed

+168
-6
lines changed

articles/communication-services/concepts/chat/concepts.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,18 @@ Some SDKs (like the JavaScript Chat SDK) support real-time notifications. This f
8383
- `chatThreadPropertiesUpdated` - when chat thread properties are updated; currently, only updating the topic for the thread is supported.
8484
- `participantsAdded` - when a user is added as a chat thread participant.
8585
- `participantsRemoved` - when an existing participant is removed from the chat thread.
86-
- `realTimeNotificationConnected` - when real time notifiation is connected.
87-
- `realTimeNotificationDisconnected` -when real time notifiation is disconnected.
86+
- `realTimeNotificationConnected` - when real time notification is connected.
87+
- `realTimeNotificationDisconnected` -when real time notification is disconnected.
8888

8989
## Push notifications
9090
To send push notifications for messages missed by your users while they were away, Communication Services provides two different ways to integrate:
9191
- Use an Event Grid resource to subscribe to chat related events (post operation) which can be plugged into your custom app notification service. For more details, see [Server Events](../../../event-grid/event-schema-communication-services.md?bc=https%3a%2f%2fdocs.microsoft.com%2fen-us%2fazure%2fbread%2ftoc.json&toc=https%3a%2f%2fdocs.microsoft.com%2fen-us%2fazure%2fcommunication-services%2ftoc.json).
92-
- Connect a Notification Hub resource with Communication Services resource to send push notifications and notify your application users about incoming chats and messages when the mobile app is not running in the foreground. The client app can subscribe to following chat events:
93-
- `chatMessageReceived` - when a new message is sent to a chat thread by a participant.
92+
- Connect a Notification Hub resource with Communication Services resource to send push notifications and notify your application users about incoming chats and messages when the mobile app is not running in the foreground.
93+
94+
IOS and Android SDK can support the below event:
95+
- `chatMessageReceived` - when a new message is sent to a chat thread by a participant.
96+
97+
Android SDK can support the below additional events:
9498
- `chatMessageEdited` - when a message is edited in a chat thread.
9599
- `chatMessageDeleted` - when a message is deleted in a chat thread.
96100
- `chatThreadCreated` - when a chat thread is created by a Communication Services user.
@@ -102,7 +106,7 @@ To send push notifications for messages missed by your users while they were awa
102106
For more details, see [Push Notifications](../notifications.md).
103107

104108
> [!NOTE]
105-
> Currently sending chat push notifications with Notification Hub is only supported for Android SDK in version 1.1.0-beta.4.
109+
> Currently sending chat push notifications with Notification Hub is generally available in Android version 1.1.0 and in public preview for iOS version 1.3.0-beta.1.
106110
107111
## Build intelligent, AI powered chat experiences
108112

articles/communication-services/concepts/chat/sdk-features.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ The following list presents the set of features which are currently available in
4141
| | Add metadata to chat messages | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
4242
| | Add display name to typing indicator notification | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
4343
|Real-time notifications (enabled by proprietary signaling package**)| Chat clients can subscribe to get real-time updates for incoming messages and other operations occurring in a chat thread. To see a list of supported updates for real-time notifications, see [Chat concepts](concepts.md#real-time-notifications) | ✔️ |||| ✔️ | ✔️ |
44-
|Mobile push notifications with Notification Hub | The Chat SDK provides APIs allowing clients to be notified for incoming messages and other operations occurring in a chat thread by connecting an Azure Notification Hub to your Communication Services resource. In situations where your mobile app is not running in the foreground, patterns are available to [fire pop-up notifications](../notifications.md) ("toasts") to inform end-users, see [Chat concepts](concepts.md#push-notifications). ||||| | ✔️ |
44+
|Mobile push notifications with Notification Hub | The Chat SDK provides APIs allowing clients to be notified for incoming messages and other operations occurring in a chat thread by connecting an Azure Notification Hub to your Communication Services resource. In situations where your mobile app is not running in the foreground, patterns are available to [fire pop-up notifications](../notifications.md) ("toasts") to inform end-users, see [Chat concepts](concepts.md#push-notifications). ||||| ✔️ | ✔️ |
4545
| Server Events with Event Grid | Use the chat events available in Azure Event Grid to plug custom notification services or post that event to a webhook to execute business logic like updating CRM records after a chat is finished | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
4646
| Reporting </br>(This info is available under Monitoring tab for your Communication Services resource on Azure portal) | Understand API traffic from your chat app by monitoring the published metrics in Azure Metrics Explorer and set alerts to detect abnormalities | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
4747
| | Monitor and debug your Communication Services solution by enabling diagnostic logging for your resource | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |

articles/communication-services/quickstarts/chat/includes/chat-swift.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,11 @@ chatThreadClient.listParticipants { result, _ in
386386
}
387387
semaphore.wait()
388388
```
389+
## Push notifications
390+
391+
Push notifications notify clients of incoming messages in a chat thread in situations where the mobile app is not running in the foreground.
392+
Currently sending chat push notifications with Notification Hub is supported for IOS SDK in version 1.3.0-beta.1.
393+
Please refer to the article [Enable Push Notification in your chat app](../../../tutorials/add-chat-push-notifications.md) for details.
389394

390395
## Run the code
391396

articles/communication-services/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ items:
130130
href: quickstarts/telemetry-application-insights.md
131131
- name: Add file sharing to your application with UI Library
132132
href: tutorials/file-sharing-tutorial.md
133+
- name: Enable Push Notifications in your chat app
134+
href: tutorials/add-chat-push-notifications.md
133135
- name: Concepts
134136
expanded: false
135137
items:
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
---
2+
title: Enable push notifications in your chat app
3+
titleSuffix: An Azure Communication Services tutorial
4+
description: Learn how to enable push notification in IOS App by using Azure Communication Chat SDK
5+
author: angellan
6+
services: azure-communication-services
7+
8+
ms.author: angellan
9+
ms.date: 08/09/2022
10+
ms.topic: tutorial
11+
ms.service: azure-communication-services
12+
---
13+
14+
# Enable Push Notifications in your chat app
15+
16+
>[!IMPORTANT]
17+
>This Push Notification feature is currently in public preview. Preview APIs and SDKs are provided without a service-level agreement, and aren't recommended for production workloads. Certain features might not be supported or might have constrained capabilities.
18+
19+
This tutorial will guide you to enable Push Notification in your IOS App by using Azure Communication Chat SDK.
20+
Push notifications alert clients of incoming messages in a chat thread in situations where the mobile app isn't running in the foreground. Azure Communication Services supports two versions of push notifications.
21+
22+
- `Basic Version` : The user will be able to see a badge number of 1 on the app’s icon, receive a notification sound and see a pop-up alert banner.
23+
- `Advanced Version`: Except for the features supported in basic version, the Contoso will be able to customize the title & message preview section in alert banner.
24+
25+
<img src="./media/add-chat-push-notification/basic-version.png" width="340" height="270" alt="Screenshot of basic version of push notification.">
26+
27+
[Basic Version]
28+
29+
<img src="./media/add-chat-push-notification/advanced-version.png" width="340" height="270" alt="Screenshot of advanced version of push notification.">
30+
31+
[Advanced Version]
32+
33+
34+
## Download code
35+
36+
Access the sample code for this tutorial on [GitHub](https://github.com/Azure-Samples/communication-services-ios-quickstarts/tree/main/add-chat-push-notifications).
37+
38+
## Prerequisites
39+
40+
1. Finish all the prerequisite steps in [Chat Quickstart](https://docs.microsoft.com/azure/communication-services/quickstarts/chat/get-started?pivots=programming-language-swift)
41+
42+
2. ANH Setup
43+
Create an Azure Notification Hub within the same subscription as your Communication Services resource and link the Notification Hub to your Communication Services resource. See [Notification Hub provisioning](../concepts/notifications.md#notification-hub-provisioning).
44+
45+
3. APNS Cert Configuration
46+
Here we recommend creating a .p12 APNS cert and set it in Notification Hub.
47+
48+
`If you are not a Microsoft internal client`, please follow step 1 to step 9.
49+
`If you are a Microsoft internal client`, please submit a ticket [here](https://aka.ms/mapsupport) and provide the bundle ID of your app to get a .p12 cert. Once you get a valid certificate issued, please execute the step 9.
50+
51+
* Step 1: Log in to the Apple Developer Portal. Navigate to `Certificates, IDs & Profiles > Identifiers > App IDs` and click the App ID associated with your app.
52+
53+
<img src="./media/add-chat-push-notification/cert-1.png" width="700" height="230" alt="Screenshot of APNS Cert Configuration step 1.">
54+
55+
* Step 2: On the screen for your App ID, check  `Capabilities > Push Notifications`. Click Save and respond “Confirm” to the Modify App Capabilities dialog box that appears.
56+
57+
<img src="./media/add-chat-push-notification/cert-2.png" width="700" height="350" alt="Screenshot of APNS Cert Configuration step 2-1.">
58+
59+
<img src="./media/add-chat-push-notification/cert-3.png" width="700" height="210" alt="Screenshot of APNS Cert Configuration step 2-2.">
60+
61+
* Step 3: In the same page, click `Capabilities > Push Notifications > Configure`. Click one of the following buttons:
62+
* Development SSL Certificate > Create Certificate (for testing push notifications while developing an iOS app)
63+
* Production SSL Certificate > Create Certificate (for sending push notifications in production)
64+
65+
<img src="./media/add-chat-push-notification/cert-4.png" width="700" height="320" alt="Screenshot of APNS Cert Configuration step 3.">
66+
67+
* Step 4: Then you're navigated to the below page. Here, you'll upload a Certificate Signing Request (CSR). Follow the next step to create a CSR.
68+
69+
<img src="./media/add-chat-push-notification/cert-5.png" width="700" height="400" alt="Screenshot of APNS Cert Configuration step 4.">
70+
71+
* Step 5: In a new browser tab, follow this [help page](https://help.apple.com/developer-account/#/devbfa00fef7) to create a CSR and save the file as “App name.cer”.
72+
73+
<img src="./media/add-chat-push-notification/cert-6.png" width="700" height="360" alt="Screenshot of APNS Cert Configuration step 5 - 1.">
74+
<img src="./media/add-chat-push-notification/cert-7.png" width="700" height="500" alt="Screenshot of APNS Cert Configuration step 5 - 2.">
75+
76+
* Step 6: Drag the .cer file to “Choose File” area. Then hit “continue” on the right top corner.
77+
78+
<img src="./media/add-chat-push-notification/cert-8.png" width="880" height="400" alt="Screenshot of APNS Cert Configuration step 6.">
79+
80+
* Step 7: Click “Download” and save the file to local disk.
81+
82+
<img src="./media/add-chat-push-notification/cert-9.png" width="700" height="220" alt="Screenshot of APNS Cert Configuration step 7.">
83+
84+
* Step 8: Open the .cer file you downloaded; it will open Keychain Access. Select your certificate, right-click, and export your certificate in .p12 format.
85+
86+
<img src="./media/add-chat-push-notification/cert-11.png" width="700" height="400" alt="Screenshot of APNS Cert Configuration step 8.">
87+
88+
* Step 9: Go to your notification hub, click “Apple (APNS)” under Settings and select “Certificate” under Authentication Mode. Also select the Application Mode based on your need. Then upload the .p12 file you just created.
89+
90+
<img src="./media/add-chat-push-notification/cert-12.png" width="700" height="360" alt="Screenshot of APNS Cert Configuration step 9.">
91+
92+
4. XCode Configuration
93+
* In XCode, go to  `Signing & Capabilities`. Add a capability by selecting "+ Capability", and then select “Push Notifications”.
94+
95+
* Add another capability by selecting “+ Capability”, and then select “Background Modes”. Also select “Remote Notifications” under Background Modes.
96+
97+
<img src="./media/add-chat-push-notification/xcode-config.png" width="730" height="500" alt="Screenshot of Enable Push Notifications and Background modes in Xcode.">
98+
99+
## Implementation
100+
101+
102+
### 1 - Basic Version
103+
If you want to implement a basic version of Push Notification, you need to register for remote notifications with APNS (Apple Push Notification Service). Refer to [sample code](https://github.com/Azure-Samples/communication-services-ios-quickstarts/blob/main/add-chat-push-notifications/SwiftPushTest/AppDelegate.swift) to see the related implementation in `AppDelegate.swift`.
104+
105+
### 2 - Advanced Version
106+
If you want to implement an advanced version of Push Notification, you need to include the following items in your app. The reason is that we encrypt customer content (e.g. chat message content, sender display name, etc.) in push notification payload and it requires some workaround on your side.
107+
108+
* Item 1: Data Storage for encryption keys
109+
110+
First, you should create a persistent data storage in IOS device. This data storage should be able to share data between Main App and App Extensions (Refer to Item 2 for more information about App Extension – Notification Service Extension).
111+
112+
In our sample code, we'll choose “App Group” as the data storage. Below are the suggested steps to create and use “App Group”:
113+
114+
Follow the steps in [Add a capability](https://developer.apple.com/documentation/xcode/adding-capabilities-to-your-app?changes=latest_minor#Add-a-capability) to add the Apps Groups capability to your app’s targets – both Main App and Notification Service Extension (Refer to Item 2 on how to create a Notification Service Extension).
115+
116+
Also follow the steps in this [Apple official doc](https://developer.apple.com/documentation/xcode/configuring-app-groups?changes=latest_minor) to configure App Group. Make sure your Main App and App Extension have the same container name.
117+
118+
* Item 2: Notification Service Extension
119+
120+
Second, you should implement a “Notification Service Extension” bundled with main app. This is used for decrypting the push notification payload when receiving it.
121+
122+
Go to this [Apple official doc](https://developer.apple.com/documentation/usernotifications/modifying_content_in_newly_delivered_notifications). Follow the step “Add a Service App Extension to your project” and “Implement Your Extension’s Handler Methods”.
123+
124+
Notice that in the step “Implement Your Extension’s Handler Methods,” Apple provides the sample code to decrypt data and we'll follow the overall structure. However, since we use chat SDK for decryption, we need to replace the part starting from `“// Try to decode the encrypted message data.”` with our customized logic. Refer to the [sample code](https://github.com/Azure-Samples/communication-services-ios-quickstarts/blob/main/add-chat-push-notifications/SwiftPushTestNotificationExtension/NotificationService.swift) to see the related implementation in `NotificationService.swift`.
125+
126+
* Item 3: Implementation of PushNotificationKeyHandler Protocol
127+
128+
Third, `PushNotificationKeyHandler` is required for advanced version. As the SDK user, you could use the default `AppGroupPushNotificationKeyHandler` class provided by chat SDK to generate a key handler. If you don’t use `App Group` as the key storage or would like to customize key handling methods, create your own class which conforms to PushNotificationKeyHandler protocol.
129+
130+
For PushNotificationKeyHandler, it defines two methods: `onPersistKey(encryptionKey:expiryTime)` and `onRetrieveKeys() -> [String]`.
131+
132+
The first method is used to persist the encryption key in the storage of user’s IOS device. Chat SDK will set 45 minutes as the expiry time of the encryption key. If you want Push Notification to be effect for more than 45 minutes, you need to schedule to call `chatClient.startPushNotifications(deviceToken:)` on a comparatively frequent basis (eg. every 15 minutes) so a new encryption key could be registered before the old key expires.
133+
134+
The second method is used to retrieve the valid keys previously stored. You have the flexibility to provide the customization based on the data storage (item 1) you choose.
135+
136+
In protocol extension, chat SDK provides the implementation of `decryptPayload(notification:) -> PushNotificationEvent` method which you can take advantage of. Refer to the [sample code](https://github.com/Azure-Samples/communication-services-ios-quickstarts/blob/main/add-chat-push-notifications/SwiftPushTestNotificationExtension/NotificationService.swift) to see the related implementation in `NotificationService.swift`.
137+
138+
## Testing
139+
1. Create a Chat Thread with User A and User B.
140+
141+
2. Download the Sample App Repo and follow the above steps in the prerequisites and implementation section.
142+
143+
3. Put User A’s <ACESS_TOEKN> and <ACS_RESOURCE_ENDPOINT> into `AppSettings.plist`.
144+
145+
4. Set “Enable Bitcode” to “No” for two Pods targets – AzureCommunicationChat and Trouter.
146+
147+
5. Plug the IOS device into your mac, run the program and click “allow” when asked to authorize push notification on device.
148+
149+
6. As User B, send a chat message. You (User A) should be able to receive a push notification in your IOS device.
150+
151+
480 KB
Loading
471 KB
Loading
83.7 KB
Loading
151 KB
Loading
163 KB
Loading

0 commit comments

Comments
 (0)