|
| 1 | +--- |
| 2 | +title: Modify behavior of Firebase In-App Messaging messages |
| 3 | +sidebar_label: Modify message behavior |
| 4 | +--- |
| 5 | + |
| 6 | +With little to no coding effort, Firebase In-App Messaging allows you to create, |
| 7 | +configure and target rich user interactions, leveraging |
| 8 | +the capabilities of Google Analytics out of the box |
| 9 | +to tie messaging events to actual user characteristics, activities, and choices. |
| 10 | +With some additional Firebase In-App Messaging SDK integration, you can tailor |
| 11 | +the behavior of in-app messages even further, responding when users interact |
| 12 | +with messages, triggering message events outside the Analytics |
| 13 | +framework, and allowing users to control sharing of their personal data related |
| 14 | +to messaging interactions. |
| 15 | + |
| 16 | +## Respond when users interact with in-app messages |
| 17 | + |
| 18 | +With actions you can use your in-app messages to direct users to a |
| 19 | +website or a specific screen in your app. |
| 20 | + |
| 21 | +Your code can respond to basic interactions (clicks and dismissals), to |
| 22 | +impressions (verified views of your messages), and to display errors logged and |
| 23 | +confirmed by the SDK. For example, when your message is composed as a Card |
| 24 | +modal, you might want to track and follow-up on which of two URLs the user |
| 25 | +clicked on the Card. |
| 26 | + |
| 27 | +To do so, you will have to use the platform-native APIs. |
| 28 | +See the documentation for [iOS](https://firebase.google.com/docs/in-app-messaging/modify-message-behavior?platform=ios#respond_when_users_interact_with_in-app_messages) |
| 29 | +and [Android](https://firebase.google.com/docs/in-app-messaging/modify-message-behavior?platform=android#respond_when_users_interact_with_in-app_messages). |
| 30 | + |
| 31 | +## Trigger in-app messages programmatically |
| 32 | + |
| 33 | +Firebase In-App Messaging by default allows you to trigger in-app messages with |
| 34 | +Google Analytics for Firebase events, with no additional integration. You can |
| 35 | +also manually trigger events programmatically with the Firebase In-App Messaging SDK's |
| 36 | +programmatic triggers. |
| 37 | + |
| 38 | +In the In-App Messaging campaign composer, create a new campaign or select an |
| 39 | +existing campaign, and in the Scheduling step of the composer workflow, note the |
| 40 | +event ID of a newly-created or existing messaging event. Once noted, instrument |
| 41 | +your app to trigger the event by its ID. |
| 42 | + |
| 43 | +```dart |
| 44 | +FirebaseInAppMessaging.instance.triggerEvent("eventName"); |
| 45 | +``` |
| 46 | + |
| 47 | +## Use campaign custom metadata |
| 48 | + |
| 49 | +In your campaigns, you can specify custom data in a series of key/value pairs. |
| 50 | +When users interact with messages, this data is available for you to, for example, |
| 51 | +display a promo code. |
| 52 | + |
| 53 | +To do so, you will have to use the platform-native APIs. |
| 54 | +See the documentation for [iOS](https://firebase.google.com/docs/in-app-messaging/modify-message-behavior?platform=ios#use_campaign_custom_metadata) |
| 55 | +and [Android](https://firebase.google.com/docs/in-app-messaging/modify-message-behavior?platform=android#use_campaign_custom_metadata). |
| 56 | + |
| 57 | + |
| 58 | +## Temporarily disable in-app messages |
| 59 | + |
| 60 | +By default, Firebase In-App Messaging renders messages whenever a triggering |
| 61 | +condition is satisfied, regardless of an app's current state. If you'd like to |
| 62 | +suppress message displays for any reason, for example to avoid interrupting a |
| 63 | +sequence of payment processing screens, you can do that with the SDK's |
| 64 | +`setMessagesSuppressed()` method: |
| 65 | + |
| 66 | +```dart |
| 67 | +FirebaseInAppMessaging.instance.setMessagesSuppressed(true); |
| 68 | +``` |
| 69 | + |
| 70 | +Passing `true` to the method prevents Firebase In-App Messaging from displaying |
| 71 | +messages, while `false` reenables message display. The SDK turns off message |
| 72 | +suppression on app restart. Suppressed messages are ignored by the SDK. Their |
| 73 | +trigger conditions must be met again while suppression is off before Firebase |
| 74 | +In-App Messaging can display them. |
| 75 | + |
| 76 | +## Enable opt-out message delivery |
| 77 | + |
| 78 | +By default, Firebase In-App Messaging automatically delivers messages to all app users you target |
| 79 | +in messaging campaigns. To deliver those messages, the Firebase In-App Messaging SDK uses |
| 80 | +installation IDs to identify each user's app. This means |
| 81 | +that In-App Messaging has to send client data, linked to the |
| 82 | +installation ID, to Firebase servers. If you'd like to give users |
| 83 | +more control over the data they send, disable automatic data collection and give |
| 84 | +them a chance to approve data sharing. |
| 85 | + |
| 86 | +To do that, you have to disable automatic initialization for Firebase In-App Messaging, and |
| 87 | +initialize the service manually for opt-in users: |
| 88 | + |
| 89 | +1. Turn off automatic initialization. |
| 90 | + |
| 91 | + **Apple platforms**: Add a new key to your `Info.plist` file: |
| 92 | + |
| 93 | + - Key: `FirebaseInAppMessagingAutomaticDataCollectionEnabled` |
| 94 | + - Value: `NO` |
| 95 | + |
| 96 | + **Android**: Add a `meta-data` tag to your `AndroidManifest.xml` file: |
| 97 | + |
| 98 | + ```xml |
| 99 | + <meta-data |
| 100 | + android:name="firebase_inapp_messaging_auto_data_collection_enabled" |
| 101 | + android:value="false" /> |
| 102 | + ``` |
| 103 | + |
| 104 | +1. Initialize Firebase In-App Messaging for selected users manually: |
| 105 | + |
| 106 | + ```dart |
| 107 | + FirebaseInAppMessaging.instance.setAutomaticDataCollectionEnabled(true); |
| 108 | + ``` |
| 109 | + |
| 110 | + Once you set a data collection preference manually, the value persists |
| 111 | + through app restarts, overriding the value in your `Info.plist` or |
| 112 | + `AndroidManifest.xml`. If you'd like to disable initialization again, for |
| 113 | + example if a user opts out of collection later, pass `false` to the |
| 114 | + `setAutomaticDataCollectionEnabled()` method. |
0 commit comments