Listen and Respond to Notifications

Goal

Handle notifications when they are received while the app is running or when the user taps a notification to open the app.

Actions Required

Action
Purpose

NotificationServicesOnNotificationReceived

Listen for notification events

NotificationServicesGetReceivedNotificationInfo

Extract notification details

Variables Needed

  • notificationId (String)

  • notificationTitle (String)

  • notificationBody (String)

  • wasLaunchedFromNotification (Bool)

  • userInfoKeys/userInfoValues (String arrays, optional)

Implementation Steps

1. State: ListenForNotifications

Action: NotificationServicesOnNotificationReceived

  • Outputs:

    • notificationId β†’ notificationId variable

    • title β†’ notificationTitle

    • body β†’ notificationBody

    • wasLaunchedFromNotification β†’ wasLaunchedFromNotification

  • Events:

    • receivedEvent β†’ HandleNotification

Note: This is a persistent listener state - it remains active and fires the event each time a notification is received.

2. State: HandleNotification

Logic: Check wasLaunchedFromNotification

  • If true: User tapped notification β†’ Navigate to relevant content

  • If false: Notification received in foreground β†’ Show in-app banner

Optional: Extract payload Use NotificationServicesGetReceivedNotificationInfo to read userInfoKeys / userInfoValues (and other fields like subtitle/badge/sound).

3. State: NavigateToContent

Based on extracted payload (for example, contentId), navigate to the appropriate scene or UI element.

4. Return to ListenForNotifications

The listener continues to monitor for new notifications.

Listener Pattern

Common Issues

  • Listener Not Firing: Ensure the state with OnNotificationReceived is active when notification arrives

  • Multiple Triggers: Each notification fires the event once; handle event properly to avoid duplicate actions

  • Background vs Foreground: wasLaunchedFromNotification distinguishes between tapped (true) and foreground (false)

  • UserInfo Parsing: Ensure JSON format is valid when building notifications

Use Cases

Foreground Notification:

  • App is open β†’ Notification arrives β†’ Show in-app toast/banner

Background/Closed Tap:

  • App closed β†’ User taps notification β†’ App launches β†’ Navigate directly to content

Performance Tip

Keep the HandleNotification state logic simple and fast. Heavy operations should be deferred to avoid blocking the notification callback.

Last updated

Was this helpful?