<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Nitesh Agarwal on Medium]]></title>
        <description><![CDATA[Stories by Nitesh Agarwal on Medium]]></description>
        <link>https://medium.com/@agarwalnitesh59?source=rss-5c0611342ef5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*_1RTZyLfBEOjjC_r</url>
            <title>Stories by Nitesh Agarwal on Medium</title>
            <link>https://medium.com/@agarwalnitesh59?source=rss-5c0611342ef5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 12:18:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@agarwalnitesh59/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Supercharge User Experience: Integrating Live Tracking Notifications in React Native!]]></title>
            <link>https://medium.com/cars24/supercharge-user-experience-integrating-live-tracking-notifications-in-react-native-17347ad8faf4?source=rss-5c0611342ef5------2</link>
            <guid isPermaLink="false">https://medium.com/p/17347ad8faf4</guid>
            <dc:creator><![CDATA[Nitesh Agarwal]]></dc:creator>
            <pubDate>Tue, 30 Jan 2024 09:32:20 GMT</pubDate>
            <atom:updated>2024-01-30T09:36:09.980Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Collaborated by — </em><a href="https://medium.com/u/e77024d7fd90"><em>Ankit Bhalla</em></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*YO-Fg-n1gJ9w0UER.png" /></figure><p>In the revolutionary Autopilot app, we’re not merely facilitating users to book on-the-go drivers; we’re unleashing a seamless, turbocharged experience! Recognizing the essential need for real-time updates on your designated drivers’ arrival status, we’ve elevated the game by seamlessly integrating the iOS Live Activity feature. Hot off Apple’s innovation oven in iOS 16, this feature propels the user experience to the next level. Imagine this: live updates booming directly to your notifications, keeping you in the loop without ever needing to tap that app refresh button! Autopilot — where every booking is an exhilarating joyride! 🚗✨</p><p>Buckle up for a journey into the technical nuances that will elevate our app to new heights, ensuring a smooth and enriching experience📱</p><p><strong>Why dive headfirst into Live Activity?</strong></p><p>The main objective of integrating Live Activity is to provide users with swift access to vital information without constantly opening the app. Picture this scenario: you’ve booked a driver, and the Live Activity feature pops up a widget on your lock screen with key details such as the driver’s name, rating, estimated time of arrival (ETA), and real-time progress. This upgrade simplifies the user experience, delivering pertinent information in a single glance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/626/0*p4HXAqR9ZtknxgDw.png" /></figure><p><em>Dynamic Island feature is accessible for iPhone 14 Pro and above, presenting an extra layer of interaction for users.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/296/0*C0jJiAz6Rqy8TyGG.png" /></figure><p>Engaging this feature unfolds when you tap on the widget, unveiling the present ETA and progress.</p><p>Embedding Live Activity into our Codebase entailed a series of steps:</p><ol><li>Introducing WidgetKit Extension</li><li>Integrating SwiftUI to craft the UI for LiveActivity and Dynamic Island</li><li>Developing a Native Module to initiate live activity from the React Native codebase.</li><li>Gaining a comprehensive understanding of how Push Notifications collaborate with the Live Activity feature.</li><li>Most importantly, rigorously testing the end-to-end flow.</li></ol><p>Let’s decode the intricacies of each of these steps!</p><ol><li><strong>Adding WidgetKit Extension</strong></li></ol><p>Start by seamlessly integrating the WidgetKit extension into your app. Consult the <a href="https://developer.apple.com/documentation/WidgetKit/Creating-a-Widget-Extension">Apple Documentation</a> for a comprehensive guide with detailed instructions.</p><p><strong>2. Updating Info.plist</strong></p><p>Include the NSSupportsLiveActivities key in your info.plist, enabling the app to initiate Live Activity. Set NSSupportsLiveActivities to true.</p><p>Following the addition of the extension and the key mentioned above, defining the attributes accessed by the live activity is essential. Here are the specific attributes tailored for our use case.</p><pre>struct LiveActivityAttributes: ActivityAttributes {<br>    public struct ContentState: Codable, Hashable {<br>        var driverName: String<br>        var rating: String<br>        var progress: Double<br>        var duration: Int<br>    }<br>}</pre><p>Once this is prepared, leverage SwiftUI to design the user interface. The Widgets kit empowers us with the capability to govern various aspects:</p><ol><li>Live Activity that appears on the lock screen</li><li>Dynamic Island Compact view: Visible when the device runs only one live activity.</li><li>Dynamic Island Expanded view: Unveiled with a long press on the dynamic island.</li><li>Dynamic Island Minimum view: This is seen when the device hosts more than one live activity in the dynamic island.</li></ol><pre>struct LiveActivity: Widget {<br>    var body: some WidgetConfiguration {<br>        ActivityConfiguration(for: LiveActivityAttributes.self) { context in<br>               // Lock screen UI goes<br>        } dynamicIsland: { context in<br>            DynamicIsland {<br>                // Expanded view UI goes here.  Compose the expanded UI through<br>                // various regions, like leading/trailing/center/bottom<br>                DynamicIslandExpandedRegion(.leading) {<br>                }<br>                DynamicIslandExpandedRegion(.trailing) {<br>                }<br>                DynamicIslandExpandedRegion(.bottom) {<br>                }<br>            } compactLeading: {<br>               //Compact view leading content goes here<br>            } compactTrailing: {<br>              //Compact view trailing content goes here<br>            } minimal: {<br>              // Minimal view content goes here<br>            }<br>    }<br>}</pre><p>Following the UI creation, it is imperative to craft a module that will initiate, update, and conclude the live activity seamlessly with the designed user interface.</p><pre>import Foundation<br>import React<br>import ActivityKit<br><br>class LiveActivity: NSObject {<br>  <br>  @objc static let sharedActivity = LiveActivity()<br>  @objc(startActivity:rating:duration:progress:)<br>  func startActivity(driverName: String, rating:String, duration:Int, progress: Double) {<br>    do {<br>      if #available(iOS 16.1, *){<br>        let liveActivityAttributes = LiveActivityAttributes(name: &quot;Live Activity&quot;)<br>        let liveActivityContentState = LiveActivityAttributes.ContentState(driverName:driverName, rating: rating, progress:progress, duration:duration)<br>        try Activity&lt;LiveActivityAttributes&gt;.request(attributes: liveActivityAttributes, contentState: liveActivityContentState,  pushType: .token)<br>      }else{<br>        print(&quot;Dynamic Island and live activies not supported&quot;)<br>      }<br>    } catch (_) {<br>      print(&quot;there is some error&quot;)<br>    }<br>  }<br>  @objc(updateActivity:rating:duration:progress:)<br>  func updateActivity(driverName: String, rating:String, duration:Int, progress: Double){<br>    do {<br>        if #available(iOS 16.1, *) {<br>           Task  {<br>                  let liveActivityContentState = LiveActivityAttributes.ContentState(driverName: driverName, rating: rating, progress: progress, duration: duration)<br>                    for activity in Activity&lt;LiveActivityAttributes&gt;.activities {<br>                        await activity.update(using: liveActivityContentState)<br>                    }<br>           }<br>        }<br>    } catch {<br>        print(&quot;Some error&quot;)<br>    }<br>  }<br>  @objc(endActivity)<br>  func endActivity(){<br>     Task{<br>       for activity in Activity&lt;LiveActivityAttributes&gt;.activities {<br>         await activity.end()<br>       }<br>     }<br>   }<br>}</pre><p>We must ensure these functions are accessible to the React Native layer through Native modules.</p><pre>#import &lt;React/RCTBridgeModule.h&gt;<br><br>@interface RCT_EXTERN_MODULE(LiveActivities, NSObject)<br>RCT_EXTERN_METHOD(startActivity:(NSString *)driverName rating:(NSString *)rating duration:(NSInteger)duration progress:(double)progress)<br>RCT_EXTERN_METHOD(updateActivity:(NSString *)driverName rating:(NSString *)rating duration:(NSInteger)duration progress:(double)progress)<br>RCT_EXTERN_METHOD(endActivity)<br>@end<br>@implementation LiveActivities<br>RCT_EXTERN_METHOD(startActivity:(NSString *)driverName rating:(NSString *)rating duration:(Int)duration progress:(double)progress)<br>- (void)startActivity:(NSString *)driverName rating:(NSString *)rating duration:(NSInteger)duration progress:(double)progress {<br>  LiveActivity *liveActivity = [LiveActivity sharedActivity];<br>  [liveActivity startActivity:driverName rating:rating duration:duration progress:progress];<br>}<br>RCT_EXTERN_METHOD(updateActivity:(NSString *)driverName rating:(NSString *)rating duration:(Int)duration progress:(double)progress) <br>- (void)updateActivity:(NSString *)driverName rating:(NSString *)rating duration:(NSInteger)duration progress:(double)progress {<br>  LiveActivity *liveActivity = [LiveActivity sharedActivity];<br>  [liveActivity updateActivity:driverName rating:rating duration:duration progress:progress];<br>}<br>RCT_EXTERN_METHOD(endActivity)<br>- (void)endActivity {<br>  LiveActivity *liveActivity = [LiveActivity sharedActivity];<br>  [liveActivity endActivity];<br>}<br>@end</pre><p><strong>Boom!</strong>💥 With these conquered steps, we’re now geared up to kickstart, update, and wrap up the live activity directly from the local React Native code. But hold on, before we dive deeper, let’s treat ourselves to a demo showcasing how we roll with live Activity and Dynamic Island in our app!🚀</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/294/0*h1hFO7mlthD39cxU.gif" /></figure><h4>Now, let’s dive deep into the mechanics of how Push Notifications collaborate with the Live Activity feature:</h4><p>The initial step involves capturing the Live Activity push token.</p><p>At the start of each Live Activity, we obtain a unique push token asynchronously. Below is the code snippet to retrieve this push token.</p><pre>Task{<br>   for await pushToken in activity.pushTokenUpdates {<br>     let pushTokenString = pushToken.reduce(&quot;&quot;) {<br>       $0 + String(format: &quot;%02x&quot;, $1) <br>     }<br>     Logger().log(&quot;New push token: \(pushTokenString)&quot;)<br>   }<br> }</pre><p>Moreover, it’s crucial to dispatch this Push Token to our server. This process needs to be encapsulated in a Task since it operates asynchronously. In certain scenarios, the push token might change the lifecycle of LiveActivity. In such cases, it becomes imperative to update the push token on the server and invalidate the previously stored push token.</p><p>Once the server acquires the push token, it initiates an APN (Apple Push Notification) request with the anticipated payload. We gain control over updating and concluding the live Activity from the server side.</p><p><strong>The payload for updating data in LiveActivity</strong></p><pre>curl -v \<br>--header &quot;apns-topic:{App Bundle Id}.push-type.liveactivity&quot; \<br>--header &quot;apns-push-type:liveactivity&quot; \<br>--header &quot;apns-priority: 10&quot; \<br>--header &quot;authorization: bearer $AUTHENTICATION_TOKEN&quot; \<br>--data \<br>&#39;{&quot;aps&quot;: {<br>   &quot;timestamp&quot;:&#39;$(date +%s)&#39;,<br>   &quot;event&quot;: &quot;update&quot;,<br>   &quot;content-state&quot;: {<br>      &quot;driverName&quot;:{Driver Name},<br>      &quot;rating&quot;: {rating},<br>      &quot;progress&quot;: {progress},<br>      &quot;duration&quot;: {duration}<br>   }<br>}}&#39; \<br>--http2 \<br>https://${APNS_HOST_NAME}/3/device/$PUSH_TOKEN</pre><p>For updating data in LiveActivity, the header apns-push-type must be set to liveactivity. In the payload, we define the following key elements:</p><p>1. <strong>Timestamp</strong>: This key ensures Live Activity displays the most recently updated data.</p><p>2. <strong>Event</strong>: This could be either updateor end.</p><p>3. <strong>Content-state:</strong> This contains the key-value pairs defined in the ContentState Attributes expected by Live Activity.</p><p>Here’s the fascinating part: once the server triggers the update request through curl, Apple, under the hood, comprehends the incoming notification based on the push token. It then updates the content of the desired live activity based on the incoming content state.</p><p>With this, we’re all set to update the tracking states on Live Activity, delivering a seamless tracking experience.</p><p>Once the update part is completed, we conclude this live activity at the desired time. To achieve that, we need to initiate an APN request with the event type end.</p><p><strong>Payload for Ending the live activity</strong></p><pre>curl -v \<br>--header &quot;apns-topic:com.cars24.sellerengine.push-type.liveactivity&quot; \<br>--header &quot;apns-push-type:liveactivity&quot; \<br>--header &quot;apns-priority: 10&quot; \<br>--header &quot;authorization: bearer $AUTHENTICATION_TOKEN&quot; \<br>--data \<br>&#39;{&quot;aps&quot;: {<br>   &quot;timestamp&quot;:&#39;$(date +%s)&#39;,<br>   &quot;event&quot;: &quot;end&quot;,<br>   &quot;content-state&quot;: {<br>      &quot;driverName&quot;:{Driver Name},<br>      &quot;rating&quot;: {rating},<br>      &quot;progress&quot;: {progress},<br>      &quot;duration&quot;: {duration}<br>   }<br>}}&#39; \<br>--http2 \<br>https://${APNS_HOST_NAME}/3/device/$DEVICE_TOKEN</pre><p>After the server triggers this curl, the live activity concludes.</p><p>But here’s a crucial point: after ending the live activity, Apple retains this activity on the lock screen for 4 hours, which isn’t optimal for user experience.<strong> <em>To address this and remove the Live Activity from the lock screen at the desired time after the cycle ends, we must include an additional payload, namely </em></strong><strong><em>dismissal-date</em></strong><em>. </em>When added to the payload, this ensures the removal of Live Activity from the lock screen 30 seconds after the activity concludes.</p><pre>{<br>  &quot;aps&quot;: {<br>     &quot;timestamp&quot;:&#39;$(date +%s)&#39;,<br>     &quot;dismissal-date&quot;:&#39;$(($(date +%s) + 30))&#39;,<br>     &quot;event&quot;: &quot;end&quot;,<br>     &quot;content-state&quot;: {<br>        &quot;driverName&quot;:{Driver Name},<br>        &quot;rating&quot;: {rating},<br>        &quot;progress&quot;: {progress},<br>        &quot;duration&quot;: {duration}<br>     }<br>  }<br>}</pre><p>Bravo! With the infusion of Live Activity into our React Native app, we’ve turbocharged the user experience for Autopilot. Now, customers can effortlessly track the arrival of their booked drivers, waving goodbye to the days of constant app navigation. As technology accelerates, delivering such seamless and convenient features is paramount for a top-notch user experience.</p><p>Hold tight because this article is just the beginning — a thrilling introduction packed with insights! Stay tuned for the next edition, where we’ll dive into:</p><p>1. Unleashing the power of testing Live Activity Push Notifications on your local machine.<br>2. Unraveling the secrets behind achieving a similar live tracking marvel on Android.</p><p>Thanks for being on this tech journey with us. Share your thoughts, feedback, or any challenges you’ve conquered during development. We’re all ears! 🚀🌟</p><p><em>If you enjoyed reading the blog and are interested in contributing to exciting projects like these, job opportunities are available! Join us and be a part of innovative implementations. We are HIRING!</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=17347ad8faf4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cars24/supercharge-user-experience-integrating-live-tracking-notifications-in-react-native-17347ad8faf4">Supercharge User Experience: Integrating Live Tracking Notifications in React Native!</a> was originally published in <a href="https://medium.com/cars24">Cars24 Engineering Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>