Firebase in app messaging

From evanca/flutter-ai-rules · View interactive version

Firebase in app messaging is an AI agent skill from the GitHub repository evanca/flutter-ai-rules. Use when running in-app message campaigns, triggering/suppressing messages, configuring opt-in data collection, testing on specific devices, rendering campaigns with custom Flutter widgets, or handling message callbacks.

How to use this skill

Skill file

Firebase In-App Messaging Skill

This skill defines how to correctly implement Firebase In-App Messaging in Flutter applications, covering setup, programmatic triggers, privacy controls, testing workflows, and campaign management.

When to Use

Use this skill when:


1. Setup and Configuration

flutter pub add firebase_in_app_messaging
import 'package:firebase_in_app_messaging/firebase_in_app_messaging.dart';

Setup Checklist

  1. Confirm Firebase.initializeApp() completes before accessing In-App Messaging.
  2. Create a test campaign in the Firebase console to verify the integration.
  3. Locate the Installation ID for device-specific testing (see Testing section).

2. Message Triggering and Display

Use Google Analytics events to trigger in-app messages without additional code. For programmatic triggering:

// Trigger a message tied to a custom event
await FirebaseInAppMessaging.instance.triggerEvent("purchase_complete");

Suppress Messages During Critical Flows

class PaymentScreen extends StatefulWidget {
  @override
  State<PaymentScreen> createState() => _PaymentScreenState();
}

class _PaymentScreenState extends State<PaymentScreen> {
  @override
  void initState() {
    super.initState();
    // Suppress messages during payment
    FirebaseInAppMessaging.instance.setMessagesSuppressed(true);
  }

  @override
  void dispose() {
    // Re-enable messages when leaving payment flow
    FirebaseInAppMessaging.instance.setMessagesSuppressed(false);
    super.dispose();
  }
}

3. User Privacy and Opt-In Data Collection

By default, In-App Messaging automatically delivers messages to all targeted users.

Disable automatic collection — iOS (Info.plist):

<key>FirebaseInAppMessagingAutomaticDataCollectionEnabled</key>
<false/>

Disable automatic collection — Android (AndroidManifest.xml):

<meta-data
    android:name="firebase_inapp_messaging_auto_data_collection_enabled"
    android:value="false" />

Enable for users who opt in at runtime:

// Call after user consents to data collection
Future<void> enableMessaging() async {
  await FirebaseInAppMessaging.instance.setAutomaticDataCollectionEnabled(true);
}

// Call if user revokes consent
Future<void> disableMessaging() async {
  await FirebaseInAppMessaging.instance.setAutomaticDataCollectionEnabled(false);
}

4. Testing and Debugging

Find the Installation ID

Test a Campaign

  1. In the Firebase console, open the campaign and select Test on your Device.
  2. Enter the Installation ID from the step above.
  3. The test message appears on the next app launch or foreground event.

5. Campaign Management

Campaign Configuration Workflow

  1. Design — Select the message type and customize appearance (title, body, image, button).
  2. Target — Define the audience by app version, language, user segment, or Analytics-based conditions.
  3. Schedule — Set start/end dates and frequency caps (once, once per session, etc.).
  4. Trigger — Choose the Analytics event that displays the message (e.g., app_open, purchase_complete).
  5. Test — Use device testing before publishing.
  6. Publish — Launch the campaign and monitor performance.

6. Custom Flutter Display

By default, In-App Messaging draws native templates. To render campaigns with your own Flutter widgets, listen to onMessageDisplay, report the lifecycle back to the SDK, and opt in with setCustomDisplayEnabled(true):

FirebaseInAppMessaging.instance.onMessageDisplay.listen((message) async {
  await message.impress();

  // Both are null for campaigns published without an action (image-only
  // layouts, action-less banners), so resolve the action before using it.
  final action = message.primaryAction ?? message.action;

  // showCampaign() is your own widget; draw it from message.title, body,
  // imageUrl, actions and data, and complete when the user closes it.
  await showCampaign(
    message,
    onActionTap: action == null
        ? null
        : () async {
            await message.click(action);
            // The plugin does not open action URLs — your app should.
          },
  );

  await message.dismiss();
});

await FirebaseInAppMessaging.instance.setCustomDisplayEnabled(true);

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.