Learn how to test push notifications and notification interactions with Patrol
Notifications are a critical part of many mobile apps. Patrol provides native automation to test notification display, interaction, and handling across Android and iOS.
import 'package:patrol/patrol.dart';void main() { patrolTest('taps on notification', ($) async { await $.pumpWidgetAndSettle(const MyApp()); await $('Open notifications screen').scrollTo().tap(); // Grant notification permission if requested if (await $.platform.mobile.isPermissionDialogVisible()) { await $.platform.mobile.grantPermissionWhenInUse(); } });}
2
Trigger a notification
Use your app to schedule or trigger a notification:
// Trigger a notification in your appawait $('Send notification').tap();
3
Open notification shade and interact
Open the notification shade and interact with notifications:
// Go to home screenawait $.platform.mobile.pressHome();// Open notification shadeawait $.platform.mobile.openNotifications();// Wait for notification to appearawait Future<void>.delayed(const Duration(seconds: 2));// Tap on the notificationawait $.platform.mobile.tapOnNotificationBySelector( Selector(textContains: 'New message'),);
Test that tapping a notification opens the correct screen:
patrol_test/notification_tap_test.dart
import 'package:patrol/patrol.dart';import 'package:flutter_test/flutter_test.dart';void main() { patrolTest('taps on notification and opens detail screen', ($) async { await $.pumpWidgetAndSettle(const MyApp()); await $('Open notifications screen').scrollTo().tap(); // Grant notification permission if (await $.platform.mobile.isPermissionDialogVisible()) { await $.platform.mobile.grantPermissionWhenInUse(); } // Schedule a notification to appear in 3 seconds await $('Schedule notification').tap(); // Go to home screen await $.platform.mobile.pressHome(); // Open notification shade await $.platform.mobile.openNotifications(); // Wait for notification to show up await Future<void>.delayed(const Duration(seconds: 5)); // Tap on the notification await $.platform.mobile.tapOnNotificationBySelector( Selector(textContains: 'Someone liked'), ); // Verify the app opened to the correct screen await $('Notification Detail Screen').waitUntilVisible(); });}
The Notification object returned by Patrol contains:
class Notification { final String title; // Notification title final String content; // Notification body/content final String? appName; // App that sent the notification final String? raw; // Raw notification data}
Example usage:
final notification = await $.platform.mobile.getFirstNotification();print('Title: ${notification.title}');print('Content: ${notification.content}');print('From: ${notification.appName}');
Notification testing requires the app to be running in the background or on the home screen. Notifications won’t appear in the shade while your app is in the foreground on most devices.
Check if app is in background (call pressHome() first)
Add sufficient delay after triggering notification
Verify notification channels are properly configured (Android)
Can't tap on notification
Verify the selector matches the notification content
Use getNotifications() to inspect available notifications
Ensure notification shade is open
Check for device-specific notification layouts
Test flaky on CI
Increase wait times for notification appearance
Ensure stable network connection for push notifications
Use waitUntilVisible() instead of fixed delays
Consider using local notifications for testing instead of push
For more reliable tests, use local scheduled notifications instead of remote push notifications. This eliminates network dependencies and makes tests more deterministic.