Skip to content

Commit 7e5cec0

Browse files
authored
Merge pull request #275929 from angellan-msft/angellan/AddPushNotificationRegistrationRenew
Acs Chat Push Notification Registration Auto Renew
2 parents 162ce35 + 302bfee commit 7e5cec0

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

articles/communication-services/tutorials/add-chat-push-notifications.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,55 @@ In protocol extension, chat SDK provides the implementation of `decryptPayload(n
146146
5. Plug the IOS device into your mac, run the program and click “allow” when asked to authorize push notification on device.
147147

148148
6. As User B, send a chat message. You (User A) should be able to receive a push notification in your IOS device.
149+
150+
## Registration Auto-Renew
151+
152+
For Microsoft to provide secure chat service, the registration for push notifications on iOS devices remains valid for only 45 minutes. To maintain the functionality of push notifications, you need to implement registration renewal in the client app.
153+
154+
Here we propose two solutions aligned with Apple's official guidelines. It is important to note that the effectiveness of either solution may be influenced by various factors such as the device's battery status, network conditions, and iOS-specific restrictions that could affect the app's ability to execute background tasks, which is stated in [Apple WWDC Video](https://developer.apple.com/videos/play/wwdc2019/707) and [Apple official wiki](https://developer.apple.com/documentation/usernotifications/pushing-background-updates-to-your-app). We recommend that you implement both solutions together to increase their effectiveness.
155+
156+
### Solution 1. Background Tasks
157+
Background Tasks offer a way to perform activities even when the app is not in the foreground. By implementing a background task, your app can request additional time to complete a specific task, such as renewing the push notification registration. Here’s how you can leverage background tasks for registration renewal:<br>
158+
159+
**Step 1. Configure User Access Token Auto Refresh**<br><br>
160+
To ensure uninterrupted access to chat services, you need to maintain valid user access tokens. Tokens typically have a default validity period (24 hours), after which they expire, requiring renewal. Implementing an auto-refresh mechanism guarantees that the token is always valid whenever the chat application is activated.<br>
161+
The Chat SDK streamlines token management by automating the refresh process when a custom access token refresher is implemented. Complete the following steps to configure your chat application to support token auto-refresh:<br>
162+
163+
1.1 Establish a Service Tier for Token Issuance<br><br>
164+
To ensure your chat application maintains continuous and secure user access, you need to implement a service tier dedicated to issuing tokens. One option is to leverage Azure Functions for this purpose.
165+
166+
To create your Azure Function, see [Build a trusted user access service using Azure Functions](../tutorials/trusted-service-tutorial.md). This guide describes how to set up your function app and deploy the code needed to issue tokens.
167+
168+
1.2 Implement the Token Refresher in Your Chat Application<br><br>
169+
Once your Azure Function is configured: <br>
170+
Obtain the Token Issuer URL from the Azure Portal, which your chat application uses to request new tokens.<br>
171+
Create and Integrate the Token Refresher within your chat application. This implements a token refresher component that requests fresh tokens and initializes the Chat Client with this component for seamless automatic token renewal.
172+
173+
For easy reference and implementation, sample code for the token refresher and its integration with the Chat Client is available in our [Sample App Repository](https://github.com/Azure-Samples/communication-services-ios-quickstarts/tree/main/add-chat-push-notifications-auto-renew) .<br><br>
174+
175+
**Step 2. Follow Apple Instruction to enable and schedule Background tasks**<br><br>
176+
To activate and schedule background tasks in your application, adhere to Apple's guidelines as outlined in their documentation on [Using Background Tasks to Update Your App](https://developer.apple.com/documentation/uikit/app_and_environment/scenes/preparing_your_ui_to_run_in_the_background/using_background_tasks_to_update_your_app). This guide provides comprehensive steps for integrating background task capabilities into your iOS app.
177+
178+
For practical implementation, consult our [Sample App Repository](https://github.com/Azure-Samples/communication-services-ios-quickstarts/tree/main/add-chat-push-notifications-auto-renew). In the provided sample, we use BGProcessingTask configured to initiate no sooner than 1 minute into the future, demonstrating how to efficiently fetch data in the background.
179+
180+
### Solution 2. Remote Notification
181+
[Remote notification](https://developer.apple.com/documentation/usernotifications/setting-up-a-remote-notification-server) is the mechanism for iOS applications to execute background tasks in response to external triggers. They can be used for tasks like refreshing registrations without user intervention. Here's how to use remote notification to run a background task:
182+
183+
1.Implement a Triggering Mechanism<br>
184+
For example, you can use an Azure Function App as the triggering mechanism. It enables you to run code in response to various triggers, including HTTP requests, making it ideal for initiating silent push notifications. Remember to implement the device registry on your end so that the function app knows where to deliver the notifications.
185+
186+
187+
2.Set Up an Azure Notification Hub<br>
188+
Azure Notification Hub provides a scalable, easy-to-use push notification infrastructure that can send notifications to any platform (iOS, Android, Windows, and so on) from any backend (cloud or on-premises). It's essential for managing and pushing notifications to devices. You can reuse the same notification hub that you use for regular push notifications. If you want to set up a new one, see [Azure Notification Hubs Documentation](../../notification-hubs/notification-hubs-push-notification-overview.md).
189+
190+
3.Configure Azure Function App for Regular Notifications<br>
191+
Adjust the Azure Function App to periodically send remote notifications via the Azure Notification Hub. These notifications are forwarded to the Apple Push Notification Service (APNS) and directed to the specified device. For more instructions, see [Functions-bindings-notification-hubs](../../azure-functions/functions-bindings-notification-hubs.md).
192+
193+
4.Handle Notifications in Your App<br>
194+
In your iOS app, implement [the application instance method](https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623013-application) to trigger a registration auto-refresh upon receiving a silent notification.
195+
196+
## Related articles
197+
198+
- [Build a trusted user access service using Azure Functions](../tutorials/trusted-service-tutorial.md)
199+
- [Chat Quickstart](../quickstarts/chat/get-started.md?pivots=programming-language-swift)
200+
- [UI Library Overview](../concepts/ui-library/ui-library-overview.md)

0 commit comments

Comments
 (0)