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

[SDK-2825] InAppMessages using Flutter Streams #3829

Merged
merged 17 commits into from
Oct 6, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,40 @@ For an example, see [AppDelegate.swift](https://github.com/braze-inc/braze-flutt
{% endtab %}
{% endtabs %}

## In-app message data callback
## Receiving in-app message data

To receive in-app message data in your Flutter app, the `BrazePlugin` supports sending in-app message data using [Dart Streams](https://dart.dev/tutorials/language/streams) (recommended) or by using a data callback (legacy).

The `BrazeInAppMessage` object supports a subset of fields available in the native model objects, including `uri`, `message`, `header`, `buttons`, `extras`, and more.

{% alert note %} The legacy data callback method will soon be deprecated. In-app messages can be added to both data streams and data callbacks. If you have already integrated data callbacks and wish to use data streams, remove any callback logic to ensure that in-app messages are processed exactly once. {% endalert %}

### Method 1: In-app message data streams (recommended)

You can set a data stream listener in Dart to receive in-app message data in your Flutter app.

To begin listening to the stream, use the code below to create a `StreamSubscription` in your Flutter app and call the `subscribeToInAppMessages()` method with a function that takes a `BrazeInAppMessage` instance. Remember to `cancel()` the stream subscription when it is no longer needed.

```dart
hokstuff marked this conversation as resolved.
Show resolved Hide resolved
// Create stream subscription
StreamSubscription inAppMessageStreamSubscription;

inAppMessageStreamSubscription = _braze.subscribeToInAppMessages((BrazeInAppMessage inAppMessage) {
// Function to handle in-app messages
}

// Cancel stream subscription
inAppMessageStreamSubscription.cancel();
```

For an example, see [main.dart](https://github.com/Appboy/flutter-sdk/blob/develop/braze_plugin/example/lib/main.dart) in our sample app.

### Method 2: In-app message data callback (Legacy)

You can set a callback in Dart to receive Braze in-app message data in the Flutter host app.

To set the callback, call `BrazePlugin.setBrazeInAppMessageCallback()` from your Flutter app with a function that takes a `BrazeInAppMessage` instance.

The `BrazeInAppMessage` object supports a subset of fields available in the native model objects, including `uri`, `message`, `header`, `buttons`, `extras`, and more.

{% tabs %}
{% tab Android %}

Expand All @@ -81,7 +107,7 @@ For an example, see [AppDelegate.swift](https://github.com/braze-inc/braze-flutt
{% endtab %}
{% endtabs %}

### Replaying the callback for in-app messages
#### Replaying the callback for in-app messages

To store any in-app messages triggered before the callback is available and replay them once it is set, add the following entry to the `customConfigs` map when intializing the `BrazePlugin`:
```dart
Expand Down