braze-inc / braze-swift-sdk

Braze SDK for the Apple ecosystem, including: iOS, macOS, iPadOS, visionOS, tvOS
https://www.braze.com
Other
52 stars 19 forks source link

[Bug]: Registering live activity with launchActivity is not working #91

Closed aaronspooky closed 11 months ago

aaronspooky commented 1 year ago

Platform

iOS

Platform Version

17.0

Braze SDK Version

7.1.0

Xcode Version

15.0

Computer Processor

Apple (M1)

Repro Rate

100%

Steps To Reproduce

Using the following implementation: BrazeManager.braze?.liveActivities.launchActivity(pushTokenTag: pushTokenString, activity: liveActivity)

Expected Behavior

The first time I implemented this line I used to see the push tag and it was sending in the following endpoint: https://sdk.iad-03.braze.com/api/v3/push_token_tag

Actual Incorrect Behavior

The push tag is not sending and no error is showing.

Verbose Logs

No logs are showing.

Additional Information

The logs are active in debug mode. brazeConfig.logger.level = .debug

hokstuff commented 1 year ago

Hi @aaronspooky,

Have you already followed all of the setup instructions here for Live Activities, and if so, what changed since your last update? Also, are you able to get verbose logs for other parts of the Braze SDK? I also noticed the BrazeManager.braze instance is nullable, so it would be helpful to double check that the instance is successfully allocated prior to calling launchActivity.

If you can email the answers along with your integration code to support@braze.com, we can help debug the issue further.

Thanks!

aaronspooky commented 1 year ago

Thanks @hokstuff for your quick response.

Yes, I've already followed the instructions, also I put a wrapper to be sure that nullability was not the reason and the issue persist, no log and warning is showing.

aaronspooky commented 12 months ago

Talking with braze team we encountered the following issues:

  1. The maximum length for push tag is 256 bytes. However, if send a tag with that lenght is truncating at 255 byte. Evidence:
    /// Starts a Live Activity with a given push token tag.
    ///
    /// The push token tag provided will be used by the push server to track and designate which Live Activity to update.
    ///
    /// - Important: Push token tags must have a maximum length of 256 bytes.
    ///
    /// - Parameter pushTokenTag: A unique tag you create to map to the Live Activity.
    /// - Parameter activity: The Live Activity you want Braze to track.
    final public func launchActivity<BrazeActivityAttributes>(pushTokenTag: String, activity: Activity<BrazeActivityAttributes>, fileID: StaticString = #fileID, line: UInt = #line) where BrazeActivityAttributes : ActivityAttributes
[http] request POST sdk.iad-03.braze.com/api/v3/push_token_tag:
"push_token" : "906d533c852d300bb88b0e7312cfe43f263b43a5fa3f917543d3885946b1b1f6e9393c36ace9ccb9c40e752861a6bbf62c007597bde5e7ad244df73dacb12768d03c31b66e59f121d43c1b506ff10250c5fa1e2bb73ee132d3d8e23e0c5f3d23b9663ac57faa0fe8cbece33a236f85f09754bdeaf93nc0af7605e126dc0672f1",
  "sdk_version" : "7.3.0",
  "tag" : "906d533c852d300bb88b0e7312cfe43f263b43a5fa3f917543d3885946b1b1f6e9393c36ace9ccb9c40e752861a6bbf62c007597bde5e7ad244df73dacb12768d03c31b66e59f121d43c1b506ff10250c5fa1e2bb73ee132d3d8e23e0c5f3d23b9663ac57faa0fe8cbece33a236f85f09754bdeaf93nc0af7605e126dc0672f",
  1. If staleDate from live activity is null the push tag never is sent.
  2. The endpoint to update live activities is outdated in the braze documentation, it should be https://rest.iad-03.braze.com/messages/live_activity/update
jerielng commented 12 months ago

Hey @aaronspooky,

  1. Thanks for raising this - we'll update it to validate against 256 bytes instead of 255.
  2. Could you provide a little more details on this? If I understand correctly, do you mean that if the staleDate property is nil, the /api/v3/push_token_tag call is never made? The stale date should not be a blocking factor for the push_token_tag API call, and I am not able to reproduce this scenario, unfortunately. Let us know if you have a reproducible example that we can try!
  3. Are you referring to these docs? It looks like the update endpoint matches what you have. Could you specify where you are seeing an outdated endpoint?
aaronspooky commented 12 months ago

Thanks @jerielng ,

  1. I really appreciate this improvement.
  2. Of course, when I create a live activity with staleDate property with nil value, the endpoint is never called. I'm using Proxyman to inspect all the traffic and no request is sent but, when I send a value the endpoint is calling again. My implementation (I can't post my whole content-state implementation but it holds 19 dynamic values):
    
    let activityContent = ActivityContent(state: initialContentState, staleDate: nil)

do { let liveActivity = try Activity.request(attributes: attributes, content: activityContent, pushType: .token)

            Task {
                for await pushToken in liveActivity.pushTokenUpdates {
                    let pushTokenString = pushToken.reduce("") {
                        $0 + String(format: "%02x", $1)
                    }

                    Logger().log("New push token: \(pushTokenString)")
                   BrazeManager.braze?.liveActivities.launchActivity(pushTokenTag: pushTokenString, activity: liveActivity)
                }
            }

        } catch {
            log.error("Error starting live activity: \(error.localizedDescription)")
        }

3. Yes, in the documentation the endpoint is: `https://rest.iad-01.braze.com/messages/live_activity/update` and should be `https://rest.iad-03.braze.com/messages/live_activity/update` a little bit tricky. Instead `-01` is `-03`.
jerielng commented 12 months ago

Hmm, looking at your sample integration, it seems you are trying to perform some actions that the Braze SDK already handles for you. You shouldn't need to observe for the pushTokenUpdates in this portion here:

for await pushToken in liveActivity.pushTokenUpdates {
  let pushTokenString = pushToken.reduce("") {
    $0 + String(format: "%02x", $1)
  }

  Logger().log("New push token: \(pushTokenString)")
  BrazeManager.braze?.liveActivities.launchActivity(pushTokenTag: pushTokenString, activity: liveActivity)
}

The Braze SDK observes these push token updates under the hood, so you only need to pass in the liveActivity object and our SDK will extract the push tokens from that.

Since we extract the push tokens for you, the pushTokenTag should not be the push token. From our docs, it should be: A unique tag you create to map to the Live Activity. This value is defined by you for each new activity you create and is used simply to allow our backend to associate push tokens from the Live Activity with that activity. So for example, say you are creating a Live Activity to track food deliveries, you could use the order number as the push token tag.

I believe you can simplify your integration to just be:

let activityContent = ActivityContent(state: initialContentState, staleDate: nil)

do {
  let liveActivity = try Activity.request(attributes: attributes, content: activityContent, pushType: .token)
  BrazeManager.braze?.liveActivities.launchActivity(pushTokenTag: "<some-value-defined-by-you>", activity: liveActivity)
} catch {
  log.error("Error starting live activity: \(error.localizedDescription)")
}

You can also refer to our complete tutorial here, which describes some sample code for how your integration should look.


As for the link in the documentation, to clarify, -03 refers to the cluster that your API key is tied to. There are other server clusters, so -01 is a valid suffix as well. It was provided in the documentation as an example, but when using the REST API, you should use the suffix that matches your Braze endpoint.

aaronspooky commented 12 months ago

Excellent! Now I'm able to send the request every time when live activity has been created. Yeah, the main issue was that. I didn't know braze was handling the token by itself.

Ok, I didn't know that, thanks for clarify this point. If you're ok with this, we can close the issue only if you see the maximum length of bytes as a problem.

jerielng commented 12 months ago

Sure thing @aaronspooky, that's great to hear! We've filed a ticket internally to track this, but we can keep this thread open to update you when this improvement has been released.

jerielng commented 11 months ago

Hi @aaronspooky, we've released version 7.4.0 of the Braze Swift SDK, which updates the validation to 256 bytes.

I'll close out this issue. Feel free to open a new one if you have any further questions. Thank you!