PWA Wrapper: Push Notification Setup
Introduction
This guide explains how to set up push notifications for a Mendix PWA packaged with PWA Wrapper. Unlike standard PWAs, wrapped PWAs behave like native applications, which changes how you integrate push notifications.
In a wrapped PWA, push notifications are handled natively through the device's operating system (Android or iOS) rather than through the browser. This guide takes you through Firebase configuration, PWA Wrapper integration, Push Notifications Connector setup, and app-level implementation.
Prerequisites
Before starting this guide, make sure you have completed the following:
- Install PWA Wrapper
- Have a Firebase account (https://firebase.google.com)
Setting Up Firebase and PWA Wrapper
Follow these steps to configure Firebase and integrate it with PWA Wrapper:
Step 1: Create and Configure Firebase Project
- Follow the steps in Setting Up the Google Firebase Cloud Messaging Server to create a Firebase project and set up a service account.
- In Firebase, add your Android and iOS apps to the project by following the same guide.
- Download and securely store the configuration files:
- For Android:
google-services.json - For iOS:
GoogleService-Info.plist
- For Android:
Step 2: Configure PWA Wrapper for Push Notifications
When building your wrapped app with PWA Wrapper, enable push notification infrastructure:
- Open the PWA Wrapper builder and select Build for your target platform (Android or iOS).
- Navigate to the App Packing section.
- Enable Enable push notification infrastructure.
- Upload the appropriate Firebase configuration file:
- For Android: select and upload
google-services.json - For iOS: select and upload
GoogleService-Info.plist
- For Android: select and upload
- Complete the remaining builder steps as described in Build PWA Wrapper Apps.
This configures the native layer to receive push notifications from Firebase Cloud Messaging (FCM).
Installing Push Notifications Connector
Add the Push Notifications Connector module to your app:
- In Mendix Studio Pro, download the Push Notifications Connector from the Marketplace.
- Add it to your app following the standard module import process.
- Complete the steps in Adding Module Dependencies and Implementing the Push Notifications Module.
Implementing Push Notification Registration
In your wrapped PWA application, create a nanoflow to handle device registration for push notifications. This nanoflow uses AppBuilderClient JavaScript actions to request permissions and retrieve device information.
Creating the Registration Nanoflow
Create a new nanoflow in your app (for example, ACT_RequestPushNotifications) and add the following steps:
Step 1: Request Permission
- Add a Call JavaScript Action activity.
- Select the RequestPermission action from AppBuilderClient.
- Set the parameter:
- Permission:
notifications
- Permission:
- Assign the return value to a variable (for example,
$PermissionState).
Step 2: Check Permission Status
- Add a Decision activity to check if permission was granted.
- Verify:
$PermissionState = 'granted' - If the condition is false (permission denied), end the flow and optionally show a message to the user.
Step 3: Retrieve Device Information
Add three Call JavaScript Action activities to retrieve device details:
-
GetDeviceInfo from AppBuilderClient
- Assign return value to
$DeviceInfo
- Assign return value to
-
GetPushToken from AppBuilderClient
- Assign return value to
$PushToken
- Assign return value to
-
GetOperatingSystem from AppBuilderClient
- Assign return value to
$OperatingSystem
- Assign return value to
Step 4: Create Device Registration Object
-
Add a Create Object activity to create a new
PushNotifications.DeviceRegistrationobject. -
Assign the following attributes:
- DeviceType: Use a decision based on the operating system:
if $OperatingSystem = getKey(PushNotifications.DeviceSystemType.Android) then PushNotifications.DeviceType.Android else if $OperatingSystem = getKey(PushNotifications.DeviceSystemType.iOS) then PushNotifications.DeviceType.iOS else if $OperatingSystem = getKey(PushNotifications.DeviceSystemType.iPadOS) then PushNotifications.DeviceType.iOS else empty - RegistrationID:
$PushToken - DeviceId:
$DeviceInfo/UniqueId
- DeviceType: Use a decision based on the operating system:
-
Assign the created object to a variable (for example,
$NewDeviceRegistration).
Step 5: Synchronize Device Registration
- Add a Synchronize activity to commit the device registration object to the database.
- Select
$NewDeviceRegistrationas the object to synchronize.
The complete nanoflow flow is shown below:
notifications permission, retrieves the device token and unique ID, and registers the device in the Push Notifications Connector database. This allows the runtime to send push notifications to this device.
Calling the Registration Nanoflow
The registration nanoflow should run automatically each time the app opens — both on first launch and when the user returns to the app from the background. This ensures the FCM token stays current. The next section explains how to implement this using app lifecycle events.
Running Push Notification Registration on App Lifecycle Events
To keep device registration up to date, run the registration nanoflow automatically whenever the app becomes active. Use the Events module from the Mendix Marketplace together with a custom JavaScript action that listens for app lifecycle changes.
Step 1: Install the Events Module
Download and install the Events module from the Mendix Marketplace.
Step 2: Create the ListenToAppLifecycle JavaScript Action
Create a new JavaScript action named ListenToAppLifecycle in your app with the following configuration:
- Add one parameter of type Nanoflow:
onResume
Use the following as the action's implementation:
// BEGIN EXTRA CODE
let appLifecycleListenerInitialized = false;
// END EXTRA CODE
export async function ListenToAppLifecycle(onResume) {
// BEGIN USER CODE
if (appLifecycleListenerInitialized) {
return;
}
appLifecycleListenerInitialized = true;
const handleVisibilityChange = async () => {
try {
if (document.visibilityState === "visible") {
if (onResume) {
await onResume();
}
}
} catch (error) {
console.error("Failed to handle app lifecycle event:", error);
}
};
document.addEventListener("visibilitychange", handleVisibilityChange);
if (document.visibilityState === "visible" && onResume) {
await onResume();
}
// END USER CODE
}The appLifecycleListenerInitialized guard prevents duplicate event listeners from being registered if the action is called more than once.
Step 3: Create the Lifecycle Nanoflow
Create a new nanoflow (for example, ACT_InitAppLifecycle) and add the following:
- Add a Call JavaScript Action activity.
- Select the ListenToAppLifecycle action you created.
- Set the parameters:
- onResume: select the
ACT_RequestPushNotificationsnanoflow you created earlier
- onResume: select the
Step 4: Add the Events Widget to the Home Page
- Open your app's default home page in Studio Pro.
- Add the Events widget from the installed Events module to the page.
- In the widget properties, locate the Component Load event.
- Set the action to Call a nanoflow.
- Select the
ACT_InitAppLifecyclenanoflow.
This ensures that every time the page loads — which happens when the app opens — the lifecycle listener is registered and the push notification registration nanoflow runs immediately. If the FCM token has changed since the last session, the updated token is saved automatically.
Sending Push Notifications
After you have built and deployed your wrapped app with device registrations in place, use the standard Push Notifications Connector workflow to send notifications:
- Follow Configuring Push Notifications to configure Firebase credentials in your app's administration page.
- Use Sending Your First Test Push Notification to test sending a notification to a registered device.
When testing, verify behavior on physical Android and iOS devices to ensure notifications are received correctly.
Read More
- Push Notifications
- PWA Wrapper
- PWA Wrapper Capabilities
- PWA Wrapper Limitations
- PWA Wrapper Deep Linking